c#面向物件程式設計—— 彈出窗體以及新建窗體類的返回值,圖片匯入,退出問題
阿新 • • 發佈:2018-11-27
新建窗體類:
在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); } } }
窗體介面如下: