我之前做的一个简单练习仅供参考:
主窗体主要代码:
private void button1_Click(object sender, EventArgs e)
{
//实例化子窗体
ChildForm childform = new ChildForm();
childform.Show();
//为子窗体注册Closed事件
childform.Closed += new EventHandler(form_Closed);
}
private void form_Closed(object sender, EventArgs e)
{
//通过属性获得子窗体中文本框中的值
ChildForm childform1 = (ChildForm)sender;
this.lab_nickname.Text = childform1.nickname;
this.lab_password.Text = childform1.password;
this.lab_passw.Text = childform1.password;
this.lab_sex.Text = childform1.sex;
this.lab_age.Text = childform1.age.ToString();
this.lab_location.Text = childform1.location;
}
子窗体主要代码:
public string nickname { get; set; }
public string password { get; set; }
public string sex { get; set; }
public int age { get; set; }
public string location { get; set; }
private void ChildForm_Load(object sender, EventArgs e)
{
//初始化comboBox值
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
ControlBox = false;
}
private void button1_Click(object sender, EventArgs e)
{
//为各个属性赋值
if ((textBox1.Text != "") & (textBox2.Text != "") & (textBox3.Text != "") & (textBox2.Text == textBox3.Text))
{
nickname = textBox1.Text.Trim();
password = textBox2.Text.Trim();
if (radioButton1.Checked == true)
{
sex = radioButton1.Text.Trim();
}
else if (radioButton2.Checked == true)
{
sex = radioButton2.Text.Trim();
}
age = Convert.ToInt32(numericUpDown1.Value);
location = comboBox1.Text + comboBox2.Text;
this.Close();
}
else if ((textBox1.Text != "") & (textBox2.Text != "") & (textBox3.Text != "") & (textBox2.Text != textBox3.Text))
{
MessageBox.Show("密码不一致!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox2.Text = "";
textBox3.Text = "";
textBox2.Focus();
}
else
{
MessageBox.Show("每一项都不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
C:\Users\Administrator\Desktop\001.jpg |