根据选择或输入的学生学号查找对应学生姓名的程序。程序的设计界面如图1所示。程序运行时,若在学号后面的组合框中选择一个学号,则对应的学生姓名将显示姓名后面的组合框中;若在学号后面的组合框中输入了一个学号后按回车键,程序将自动查找有无该学号的学生,如果没有将出现一个提示框询问是否添加该学生,如果选择“是”,则要求输入学生的姓名,并把输入的学号和姓名分别添加到相应的列表框中。不知道这里是什么原因,输入后下拉列表中显示为空白
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace StudenNoAndName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.SelectedIndex = comboBox1.SelectedIndex;
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedIndex = comboBox2.SelectedIndex;
} private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int sno = comboBox1.FindStringExact(Text);
if (e.KeyChar == 13)
{
if (sno == -1)
{
DialogResult r1 = MessageBox.Show("没有这个学生。是否添加该学生", "查找失败", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
int r = (int)r1;
if (r == 6)
{
comboBox1.Items.Add(comboBox1.Text);
comboBox2.Items.Add(comboBox2.Text); //运行时宿儒一个名字后,下拉列表总为空白
}
}
} }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace StudenNoAndName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.SelectedIndex = comboBox1.SelectedIndex;
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedIndex = comboBox2.SelectedIndex;
} private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int sno = comboBox1.FindStringExact(Text);
if (e.KeyChar == 13)
{
if (sno == -1)
{
DialogResult r1 = MessageBox.Show("没有这个学生。是否添加该学生", "查找失败", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
int r = (int)r1;
if (r == 6)
{
comboBox1.Items.Add(comboBox1.Text);
comboBox2.Items.Add(comboBox2.Text); //运行时宿儒一个名字后,下拉列表总为空白
}
}
} }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货