c#面向对象程序设计—— 弹出窗体以及新建窗体类的返回值,图片导入,退出问题
新建窗体类:
在form1的加载事件中加入以下语句:
private void Form1_Load(object sender, EventArgs e)
{
Message mes = new Message();
if (mes.ShowDialog() != DialogResult.OK) Application.Exit();
}
在新建窗体类中加入以下程序
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;
namespace WindowsFormsApplication1
{
public partial class Message : Form
{
public Message()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (Nam.Text == "admin" && Password.Text == "123")
{
MessageBox.Show("Welcome","success", MessageBoxButtons.OK);
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("Gun","fale",MessageBoxButtons.OK);
this.DialogResult = DialogResult.No;
}
}
}
}
效果如下:
图片导入:
private void Picture_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
string path = op.FileName;
pictureBox1.ImageLocation = path;
FileInfo file = new FileInfo(path);
file.CopyTo("your menu", true);
}
退出键:
private void edit_Click(object sender, EventArgs e)
{
Application.Exit();
}
comboBox的选择:
if(comboBox1.SelectedIndex>=0)
emp.Marry = comboBox1.SelectedItem.ToString();
form事件的代码:
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 System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Employee emp = new Employee();
private void Save_Click(object sender, EventArgs e)
{
emp.Nam=txtName.Text;
if (rbMale.Checked)
emp.Sex = "Male";
else
emp.Sex = "Female";
emp.Birth = BirthDay.Text;
emp.Addin = Addin.Text;
if(comboBox1.SelectedIndex>=0)
emp.Marry = comboBox1.SelectedItem.ToString();
if (comboBox2.SelectedIndex >=0)
emp.Politic = comboBox2.SelectedItem.ToString();
emp.Jiguan = JiGuan.Text;
emp.Native = MinZu.Text;
if(XueLi.SelectedIndex>=0)
emp.Educate = XueLi.SelectedItem.ToString();
emp.Adress = Adress.Text;
emp.Eadress = Eadress.Text;
emp.Telnumber = TelNumber.Text;
emp.Worknumber = WorkNumber.Text;
if (comboBox3.SelectedIndex >=0)
emp.Department = comboBox3.SelectedItem.ToString();
richTextBox1.Text = emp.information();
}
private void Form1_Load(object sender, EventArgs e)
{
Message mes = new Message();
if (mes.ShowDialog() != DialogResult.OK) Application.Exit();
}
private void edit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Picture_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
string path = op.FileName;
pictureBox1.ImageLocation = path;
FileInfo file = new FileInfo(path);
file.CopyTo("your menu", true);
}
}
}
窗体界面如下: