城市检索窗体(单链表练习)

这次窗体练习的复杂度应该是不及小闹钟的,不过时间更短、链表储存结构是新学的,以及本身写窗体的能力不是很强,还是很有挑战性的一次任务,在制作期间,请教了老师和许多同学,十分感谢他们的帮助。
在这里插入图片描述

我实现的功能是 前插 后插 根据所给的位置插入 以及 删除 查找。实现这几个功能的主要原因是,这几个功能和老师上课讲的那些相差不大,无非是把同学换成了城市,把学号换成了坐标。
总结一下遇到的一些问题和解决方案吧:
1、老师发的代码打不开
虽然老师很贴心的把上课用的代码打包发了过来,然鹅可能是因为我的版本是vs2010,比较老的版本,我无法打开,就通过回放和对着敲的办法进行了代码抄袭再次温习。
2、c#写窗体的一些格式忘记了
查小闹钟的代码以及百度。
3、类库无法直接运行
因为是对着老师的视频操作的,老师是创了一个类库,所以我也傻傻地创了一个类库,然后在里面插入了表格,画完图进行调试的时候,发现没法运行。我差点以为我得创个新文件重做了,还好vs在这个地方很合理,有一个设为启动项目的操作(当然在有些方面确实离谱)。
4、实例化失败
经过总结以后,失败的原因应该有两个。一是有些地方没有对类库里的内容进行引用,说实话我现在都还是稀里糊涂的,看到他报错并提示,然后在报错的地方点一下,好像可以选择需要引用的类库。二是在写窗体的时候,没有考虑到函数的作用域问题,因为是对着小闹钟窗体写的,有点生硬地照搬了。
5、listbox里面的同步操作
这个真的让我想了好久,到底该怎么让我的链表list和窗体控件listbox关联。结果我发现不用关联,各管各就行,因为listbox本身插入的方法就很方便,只要找准正确的位置数index就行。
6、进行查找的时候只知道城市
city的构造函数本来是需要三个变量:string城市名,int x坐标,int
y坐标。但是查找的要求是给城市的姓名,返回x y,而不是上课给姓名学号返回在不在链表中。为了实现 compareto,我重载了只有一个变量name的构造函数,然后找到链表中同样城市名的index,再分别用重载的tostring方法返回x和y。

还未完成的地方:根据距离查找城市的功能,以及一些容错的考虑(输入了重复的城市名,数字越界等) 以及 界面的美化。
这是窗体部分代码,剩下太多链表部分的就不贴了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using lym2020._3._8;

namespace Cityform
{
    public partial class Form1 : Form
    {
        private SLinkList<City> _lst = new SLinkList<City>();
        

        public Form1()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)  //窗口加载
        {
            
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.Trim();
            int x = int.Parse(textBox2.Text);
            int y = int.Parse(textBox3.Text);
            if (string.IsNullOrEmpty(name)||x == null||y==null)
            {
                MessageBox.Show("信息不全!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

                City city = new City(name, x, y);
                _lst.InserAtFirst(city);
                listBox1.Items.Insert(0, city);
                textBox1.ResetText();
                textBox2.ResetText();
                textBox3.ResetText();
                return;
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.Trim();
            int x = int.Parse(textBox2.Text);
            int y = int.Parse(textBox3.Text);
            if (string.IsNullOrEmpty(name) || x == null || y == null)
            {
                MessageBox.Show("信息不全!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            City city = new City(name, x, y);
            _lst.InsertAtRear(city);
            listBox1.Items.Insert(_lst.Length-1, city);
            textBox1.ResetText();
            textBox2.ResetText();
            textBox3.ResetText();
            return;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.Trim();
            int x = int.Parse(textBox2.Text);
            int y = int.Parse(textBox3.Text);
            if (string.IsNullOrEmpty(name) || x == null || y == null)
            {
                MessageBox.Show("信息不全!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            City city = new City(name, x, y);
            int index1 = (int)numericUpDown1.Value-1;
            _lst.Insert(index1, city);
            listBox1.Items.Insert(index1, city);
            textBox1.ResetText();
            textBox2.ResetText();
            textBox3.ResetText();
            return;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

            int index = (int)numericUpDown2.Value-1;
            listBox1.Items.RemoveAt(index);
            _lst.Remove(index);

        }

        private void button6_Click(object sender, EventArgs e)
        {
            string name = textBox4.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("城市名字为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            int index = _lst.Search(new City(name));
            if (index == -1)
            {
                MessageBox.Show("链表中无该城市!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                textBox5.Text = _lst[index].X.ToString();
                textBox6.Text = _lst[index].Y.ToString();
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值