开发者社区> 问答> 正文

要建立一个学生查询的操作页面,遇到了点小问题?报错


小弟要设计一个下面这样的界面,但是程序报错,到底那个public student[] s=new student[5]应该放哪儿啊?

各种位置不对啊..


namespace WindowsFormsApplication4

{
    public class student
    {
        public int no;
        public string name;
        public string sex;
        public int banji;
        public student(int no1, string name1, string sex1, int banji1)
        {
            no = no1; name = name1;
            sex = sex1; banji = banji1;
        }
    }


    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            student[] s = new student[5];
            s[0] = new student(1, "王华", "男", 0809);
            s[1] = new student(2, "李明", "男", 0808);
            s[2] = new student(3, "张兵", "男", 0812);
            s[3] = new student(4, "周红", "女", 0811);
            s[4] = new student(5, "刘静", "女", 0801);
            comboBox1.Items.Add(1); comboBox1.Items.Add(2);
            comboBox1.Items.Add(3); comboBox1.Items.Add(4);
            comboBox1.Items.Add(5);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int sno = comboBox1.SelectedIndex;
            textBox1.Text = Convert.ToString(sno+1);
            textBox2.Text = s[sno].name;
            textBox3.Text = s[sno].sex;
            textBox4.Text = Convert.ToString(s[sno].banji).PadLeft(4,'0');
        }
    }

}

展开
收起
爱吃鱼的程序员 2020-06-22 17:27:57 435 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">  <spanstyle="background-color:#FFFFFF;font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;">刚学C#?<preclass="brush:c#;toolbar:true;auto-links:false;">privatevoidForm1_Load(objectsender,EventArgse){//这个是局部变量,只在这个方法中有效。下面的button1_Click中不能使用s//把它变成全局变量,就可以了。student[]s=newstudent[5];}界面这么丑一看就是要做课程设计或交大作业之类的苦B大学生。哈哈哈....太感谢了~搞定搞定~谢谢大神帮忙~刚学C#,所以有些东西还是迷迷糊糊的~button1_Click的s从哪来的?谢谢帮忙~懂了,搞定了~button1_Click中的数组s就是那个student[]s=newstudent[5]定义的s啊..我把student[]s=newstudent[5]和后面的那一串学生信息都放在button1_Click中就不报错了,这个原因到底是什么呢?

    2020-06-22 17:28:14
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《开放搜索查询分析服务架构分享》 立即下载
《用管控策略设定多账号组织全局访问边界》 立即下载
阿里云数据库案例集下载 立即下载