c# 窗体开发1 基本控件的使用

 
 1 namespace firstly    #当前命名空间控件
 2 {
 3 public partial class Form1 : Form          
 4 {
 5 public Form1()  #第一个窗体
 6 {
 7 InitializeComponent();
 8 }
 9 }
10 }
winform 代码结构

1.常用控件的认识

修改winform 应用程序的入口点

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
//程序位于   Program.cs
namespace firstly
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
//决定那个窗体先被执行
            Application.Run(new Form22());   

//    Application.Run(new Form1());
        }
    }
}
修改程序入口点

可以通过修改  Form1.Designer.cs 来实现 对控件进行修改   (一般 图形进行控制)

    button1.Enabled = false;  是指不可用

    Application.Exit();  关闭总窗体   

Form.Close();    关闭当前的窗体

(1).基本控件

1.Label   标签

 功能 :显示窗体文本

常用属性和方法 事件  :Text 

Hide  即是Visible 属性设置为True   控件也不可见

Show  相当于Visible属性设置为True 

Click: 单击控件

eg:

在一个界面 ,在此打开一个新的窗体,(当前窗体隐藏)

1           //当前界面隐藏 
2             linkLabel1.LinkVisited = true;    //确认可以被访问
3             Form2 f = new Form2();
4             f1.Show();
5             this.Hide(); // 当前隐藏
实例化一个新的窗体

2.TextBox 文本框的控件和 Button 按钮

功能  接受或显示用户文本信息

常用属性和方法 事件  :

MaxLength    文本框输入最大字符

Multiline     是否可以在文本框输入多行

Passwordchar    密码输入框

Readonly   文本框中的字体为可读

Text  检索在控件输入的文本

Clear  删除现有的所有文本

Show 相当于Visible属性设置为True 

KeyPress   用户按一个键结束时将发生该事件

Botton控件  

Enabled 是否启用

PerFormClick  Button中Click时间

Click    点击触发的

eg 用户登录

 1 private void button2_Click(object sender, EventArgs e)   //取消
 2            {
 3                clear();    //调用自己构造的方法
 4 
 5              }
 6 
 7         private void button1_Click(object sender, EventArgs e)
 8         {
 9             if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
10             {
11                 MessageBox.Show("信息禁止为空!","登录提示");
12                 clear();
13                 return;
14             }
15             if (!textBox1.Text.Equals("admin") || !textBox2.Text.Equals("admin"))    # 输入比对
16             {
17                 MessageBox.Show("用户名称或密码为空!", "登录提示");
18                 clear();
19                 return;
20             }
21             else
22             {
23                 MessageBox.Show("欢迎您登录本系统!","消息提示");
24                 clear();
25             }
26         }
27 //由于每次都调用  所以生成一个方法  更有效率
28         public void clear()
29         {
30             textBox1.Clear();
31             textBox2.Clear();
32             textBox2.Focus();
33         }
用户登录
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace WindowsApplication1
10 {
11     public partial class Form1 : Form
12     {
13         public Form1()
14         {
15             InitializeComponent();
16         }
17         private bool ISLOGIN = false;
18         //就加在其构造函数的前面。这东西我们等下要用。
19         //然后加一个属性,该属性用来判别是否登录
20         public bool isLogin
21          {
22            get 
23            {
24              return this.ISLOGIN;
25            }
26          }
27         private void Form1_Load(object sender, EventArgs e)
28         {
29 
30         }
31 
32         private void button1_Click(object sender, EventArgs e)
33         {
34             if (this.textBox1.Text == "qq" && this.textBox2.Text == "qq")
35             {
36                 this.ISLOGIN = true;
37                 //frmEditor f2 = new frmEditor();
38                 //f2.ShowDialog();
39                 this.Close();//登陆成功才关闭登陆登陆窗体
40             }
41             else
42             {
43                 MessageBox.Show("非法用户名或密码,请再重试一次!");
44             }
45         }
46     }
47 }
登录成功后才会关闭

Form1.MaximizeBox=false   窗体不允许最大化

text1.PasswordChar='*';

3.ListBox 列表框 控件

功能显示多行文本信息 , 提供用户选择

常用属性和方法 事件  :

Items  :  列表框的具体内容 

SelectionMode    列表框类型   单选 ,多选, 不可选择

SelectedIndex   指定选中行索引,默认第一行为0

SelectedItem 被选择的文本内容

SelectedItems ListBox的选择列表集合

Text   默认文本内容

ClearSelected 清除当前选择

SelectedIndexChanged  一旦改变就触发

eg。部门显示

private void Form1_Load(object sender, EventArgs e)   //窗体加载
        {
            this.listBox1.Items.Add("软件部");
            this.listBox1.Items.Add("硬件部");
            this.listBox1.Items.Add("财务部");
            this.listBox1.Items.Add("人事部");   
        }
//当选中后会触发的事件
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("您选择的部门是:"+listBox1.SelectedItem.ToString()+",位列第"+listBox1.SelectedIndex.ToString(),"信息提示"); 
        }
listBox 练习
 1 private void button1_Click_1(object sender, EventArgs e)
 2         {
 3             textBox1.Enabled = true;
 4             textBox2.Enabled = true;
 5             comboBox1.Enabled = true;
 6             listBox1.Enabled = true;
 7             textBox1.Focus();
 8         }
 9 
10         private void button2_Click(object sender, EventArgs e)
11         {
12             textBox1.Text = "";
13             textBox2.Text = "";
14             listBox1.SelectedIndex = 0;
15             comboBox1.SelectedIndex = 0;
16             textBox1.Focus();
17         }
18 
19         private void button3_Click(object sender, EventArgs e)
20         {
21             string str = "";
22             for (int ctr = 0; ctr <= this.listBox1.SelectedItems.Count - 1; ctr++)
23             {
24                 str += "\n" + this.listBox1.SelectedItems[ctr].ToString();
25             }
26             //注意:此处需要将listBox1的selectionmodel设置为MultiExtended才会有效果。
27             MessageBox.Show("选定的项目是:\n" + str, "信息提示", MessageBoxButtons.OK);
28             Application.Exit();
29         }
30 
31         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
32         {
33             string str = this.comboBox1.SelectedItem.ToString();
34             MessageBox.Show("您选择的职务是:\n" + str, "信息提示", MessageBoxButtons.OK);
35         }
插入方法的实践操作

4.ComboBox  

功能 限制用户 在多个固定信息选择 唯一一个行

常用属性和方法 事件  :

DropDownStyle  :  ComboBox 控件的样式  有三种 simple 只读 显示所有 DropDown(可以读写) DropDownList(只读)

MaxDropDownItems 下拉区显示最大项目数

Select 选择指定范围的文本

 1 private void Form1_Load(object sender, EventArgs e)
 2         {
 3             this.comboBox1.Items.Add("财务部");
 4             this.comboBox1.Items.Add("产品部");
 5             this.comboBox1.Items.Add("销售部");
 6             this.comboBox1.Items.Add("生产部");
 7             //默认的选择是"产品部"
 8             this.comboBox1.SelectedIndex = 1;
 9 
10             this.comboBox2.Items.Add("财务部");
11             this.comboBox2.Items.Add("产品部");
12             this.comboBox2.Items.Add("销售部");
13             this.comboBox2.Items.Add("生产部");
14             //默认的选择是"产品部"
15             this.comboBox2.SelectedIndex = 1;
16 
17             this.comboBox3.Items.Add("财务部");
18             this.comboBox3.Items.Add("产品部");
19             this.comboBox3.Items.Add("销售部");
20             this.comboBox3.Items.Add("生产部");
21             //默认的选择是"产品部"
22             this.comboBox3.SelectedIndex = 1;
23         }
24 
25 private void com_selectIndexChanged_1(){
26 String mess=comboBox1.SelectedItem.ToString();
27 TextBox1.text=mess;
28 
29 }
comboBox练习

5.对话框窗口(非模式窗体)

重载方法

Show(string text);      

Show(string text, string caption);

Show(string text, string caption, MessageBoxButtons buttons);

Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon);     指定文本/标题/按钮/图标的消息框

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             MessageBox.Show("嘿,这是简单提示!","信息提示");
 4         }
 5 
 6 private void button2_Click(object sender, EventArgs e)
 7         {
 8             DialogResult result = MessageBox.Show("嘿,这是问询提示!","问询提示",MessageBoxButtons.YesNo);
 9             if (result == DialogResult.Yes)
10             {
11                 label1.Text = "您选择了YES";
12               
13             }
14             else
15             {
16                 label1.Text = "您选择了NO";
17             }
18         }
19 
20 
21 
22 private void button3_Click(object sender, EventArgs e)
23         {
24             DialogResult result = MessageBox.Show("嘿,这是带有图标的问询提示!", "问询提示", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button3,MessageBoxOptions.RightAlign);
25             if (result == DialogResult.Yes)
26             {
27                 label1.Text = "您选择了图标YES";
28 
29             }
30             else if(result==DialogResult.Cancel)
31             {
32                 label1.Text = "您选择了图标取消";
33             }
34             else if (result == DialogResult.No)
35             {
36                 label1.Text = "您选择了图标NO";
37             }
38         } 
提示框演示

  补充  showDialog

路径绑定show   不可以自由切换   就是指在用户没有关闭当前页的前提下,无法关闭该页面后任一页面

要等待当前窗体关闭后才能操作其他窗体,

eg

1 private void button2_Click(object sender, EventArgs e)
2         {
3             Form2 f2 = new Form2();
4             this.Visible = false;
5             f2.ShowDialog();
6          this.Visible = true; 
7         }
ShowDialog 演示
 1 private void button1_Click_1(object sender, EventArgs e)
 2         {
 3             textBox1.Enabled = true;
 4             textBox2.Enabled = true;
 5             comboBox1.Enabled = true;
 6             listBox1.Enabled = true;
 7             textBox1.Focus();
 8         }
 9 
10         private void button2_Click(object sender, EventArgs e)
11         {
12             textBox1.Text = "";
13             textBox2.Text = "";
14             listBox1.SelectedIndex = 0;
15             comboBox1.SelectedIndex = 0;
16             textBox1.Focus();
17         }
18 
19         private void button3_Click(object sender, EventArgs e)
20         {
21             string str = "";
22             for (int ctr = 0; ctr <= this.listBox1.SelectedItems.Count - 1; ctr++)
23             {
24                 str += "\n" + this.listBox1.SelectedItems[ctr].ToString();
25             }
26             //注意:此处需要将listBox1的selectionmodel设置为MultiExtended才会有效果。
27             MessageBox.Show("选定的项目是:\n" + str, "信息提示", MessageBoxButtons.OK);
28             Application.Exit();
29         }
30 
31         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
32         {
33             string str = this.comboBox1.SelectedItem.ToString();
34             MessageBox.Show("您选择的职务是:\n" + str, "信息提示", MessageBoxButtons.OK);
35         }
MessBox 加强演示

2.多文档处理界面

SDI 单文档窗口 

MDI 多文档窗口

设置MDI  *****在窗体 属性IsMdiContainer 属性设置为True

MDI 属性

StartPosition    初始窗口位置

CancelButton 按下esc键后执行那个按钮。

ControlBox    确定系统是否有图标和最大最小关闭按钮。

FormBorderStyle  指定边框和标题栏的外观和行为。

HelpButton    确定窗体的标题栏上是否有帮助按钮。

KeyPreview    确定窗体键盘事件是否已经向窗体注册。

MainMenuStrip      确定键盘激活和多文档合并。

ShowInTaskbar     确定窗体是否出现在任务栏中。

WindowState        确定窗体的初始可视状态。

方法

Activate     当窗体被激活时候发生

MdiChildActivate     当MDI子窗体被激活时候发生

事件

Activated    窗体在激活时发生

Closed   已经关闭

 Closing 正在关闭

Load

 为MDI 配置   MainMenu菜单   工具栏中默认是没有的,打开工具箱 界面,右键 选择项命令

并在扩展的选择工具箱项窗口 寻找 Mainmenu 控件

 父窗体设置

IsMdiContainer 属性设置为True

触发方法:

this.LayoutMdi(Mdilayout.Cascade) ;

Form2 f2 = new Form2();

指定父窗体
f2.MdiParent = this;
f2.Show();

 在此可以在一个界面 内打开一个子窗体

 窗体的层叠

TileHorizontal   水平

TileVertical   垂直

小技巧:

 *****窗体之间传值

实现界面

窗体2

 窗体3 接受到  

 

 1 ============form2的设计=====================
 2 private void Form2_Load(object sender, EventArgs e)
 3         {
 4             comboBox1.SelectedIndex = 0;
 5             textBox3.Text = "";
 6             textBox1.Focus();
 7         }
 8 
 9         public void button1_Click(object sender, EventArgs e)
10         {
11             if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "")
12             {
13                 MessageBox.Show("姓名,或者邮件,或者提交,信息禁止为空!", "信息提示");
14             }
15             else
16             {
17                               this.Hide();
18                 Form3 childform3 = new Form3(this.textBox1.Text,this.textBox2.Text,this.comboBox1.SelectedItem.ToString(),this.textBox3.Text);
19                 childform3.Show();               
20             }
21         }
22 private void button2_Click(object sender, EventArgs e)
23         {
24             this.Close();
25         }
26 
27 
28 ================form3的设计====================
29 对于form3窗体而言,在系统初始事件填写如下代码:
30 public partial class Form3 : Form
31     {
32         private string _name;
33         private string _emailId;
34         private string _subject;
35         private string _feedBack;
36 
37         public Form3(string varName, string varEmail, string varSubject, string varFeedBack)
38         {
39             InitializeComponent();
40             // 在 private 变量中存储值 
41             this._name = varName;
42             this._emailId = varEmail;
43             this._subject = varSubject;
44             this._feedBack = varFeedBack;
45             // 在列表框中放置值 
46             listBox1.Items.Add("姓名:" + this._name);
47             listBox1.Items.Add("邮件地址:" + this._emailId);
48             listBox1.Items.Add("信息主题:" + this._subject);
49             listBox1.Items.Add("反馈意见:" + this._feedBack);
50         }
51 
52         private void button1_Click(object sender, EventArgs e)
53         {
54             MessageBox.Show("感谢您输入的反馈!");
55             this.Close();
56         }
57     }
代码

上例子中固然很好 ,但是 2个窗体在转换过程中没有收到MDI 的控制

 1 ============form2的设计=====================
 2 private void Form2_Load(object sender, EventArgs e)
 3         {
 4             comboBox1.SelectedIndex = 0;
 5             textBox3.Text = "";
 6             textBox1.Focus();
 7         }
 8 
 9         public void button1_Click(object sender, EventArgs e)
10         {
11             if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "")
12             {
13                 MessageBox.Show("姓名,或者邮件,或者提交,信息禁止为空!", "信息提示");
14             }
15             else
16             {
17                               this.Hide();
18                 Form3 childform3 = new Form3(this.textBox1.Text,this.textBox2.Text,this.comboBox1.SelectedItem.ToString(),this.textBox3.Text);
19 //加的这一句   
20             childform3.MdiParent = this;
21 
22                 childform3.Show();               
23             }
24         }
25 private void button2_Click(object sender, EventArgs e)
26         {
27             this.Close();
28         }
29 
30 
31 ================form3的设计 
32  在 中加入返回按钮事件 ===================
33 
34 Form2 f2 = new Form2();
35             f2.MdiParent = this.MdiParent;
36             f2.Show();
37  this.close();
改进过后的代码

技巧2 放回重复打开窗口

在打开的同时进行判断

 1 建议写成一个方法 
 2 直接检测是否已经打开此MDI窗体  
 3 // 是否已经打开了?(用循环来判断) 
 4  foreach (Form childrenForm in this.MdiChildren)  {
 5   //检测是不是当前子窗体名称  
 6 if (childrenForm.Name == "子窗体名称")
 7  {  //是的话就是把他显示
 8   childrenForm.Visible = true;
 9   //并激活该窗体
10  childrenForm.Activate();
11   return;
12  }
13  }
14  //下面是打开子窗体
15   Form1 childrenForm = new Form1();
16   childrenForm.MdiParent = this;
17   childrenForm.Show();
18  }
判断是否重复打开

技巧3 通过类属性进行数据传值(方法一)

 1 1、先在Form2中定义一个成员变量和一个属性如下:
 2  private string form2zhi = null; 
 3 public string Form2ChuanZhi
 4  { get 
 5 { return form2zhi; } 
 6 } 
 7 2、再在Form3中定义一个成员变量和一个属性如下:
 8  private string form3zhi = null;
 9  public string Form3ChuanZhi 
10 { set { form3zhi = value; 
11 }
12  get
13  { return form3zhi;
14  } 
15 } 
16 
17 3、双击btn_ChuanZhi在这个事件中写入以下代码(主要是显示Form3窗体和将Form2中的值传过去):
18  Form3 form3 = new Form3();
19  form3.Form3ChuanZhi = form2zhi;
20 //将值传过去 
21 form3.Show(); 
22 
24 另一个版本
25    private string some_name;
26         public string SomeName {
27             get {
28                 return some_name;
29             }
30             set {
31                 some_name = value;
32             }
33         }
34         private void button2_Click(object sender, EventArgs e)
35         {
36             Form2 f2 = new Form2();
37             this.Visible = false;
38             f2.ShowDialog();
39          this.Visible = true; 
40         }
41         private void menuItem3_Click(object sender, EventArgs e)
42         {
43             if (TextBox1.text1 == "" || TextBox2.Text == "") { 
44             MessageBox.Show("no blank");
45         }else{
46             this.Hide();
47             Form2 fm=new Form2();
48                 fm.SomeName=text1Box1.Text;
49                 fm.MdiParent=this.MdiParent;
50                 fm.Show();
51             }
52         private void Form1_Load(object sender, EventArgs e)
53         {
54             listBox1.Items.Add(SomeName);
55         }

 实训:

 1 Form 4
 2 
 3 ————————————————————
 4 using System;
 5 using System.Collections.Generic;
 6 using System.ComponentModel;
 7 using System.Data;
 8 using System.Drawing;
 9 using System.Text;
10 using System.Windows.Forms;
11 
12 using System.Collections;
13 //注意:必须调用该命名空间,否则无法建立ArrayList对象。
14 namespace WindowsApplication1
15 {
16     public partial class Form4 : Form
17     {
18         //Form4窗体的私有变量listData1,用来保存listBox1下拉列表中的数据
19         private ArrayList listData1;
20         //Form4窗体的公共属性。该属性可以被外部所访问,访问的内容就是Form4窗体的私有变量listData1的内容。
21         //这里我们采用属性,感觉语法更灵活,清楚。
22         public ArrayList ListData2
23         {
24             get { return this.listData1; }
25         }
26 
27         //配置Form4构造函数,目的是填充listBox1中的具体数据内容
28         public Form4()
29         {
30             InitializeComponent();
31             this.listData1 = new ArrayList();
32             this.listData1.Add("DotNet");
33             this.listData1.Add("C#");
34             this.listData1.Add("Asp.net");
35             this.listData1.Add("WebService");
36             this.listData1.Add("XML");
37             this.listBox1.DataSource = this.listData1;
38         }
39 
40         private void button1_Click(object sender, EventArgs e)
41         {
42             Form5 formChild = new Form5();//实例化另外一个窗体Form5
43             formChild.Owner = this;
44             //我们设置了formChild.Owner为this,这样,子窗体和主窗体就有联系了,
45             formChild.ShowDialog();//打开子窗体
46             //当然上面两句也可以该成为:formChild.ShowDialog(this);
47             this.listBox1.DataSource = null;
48             this.listBox1.DataSource = this.listData1;
49             this.Hide();
50             this.Close();
51         }
52     }
53 }
54 
55 ————————————Form 5
56 
57 using System;
58 using System.Collections.Generic;
59 using System.ComponentModel;
60 using System.Data;
61 using System.Drawing;
62 using System.Text;
63 using System.Windows.Forms;
64 using System.Collections;
65 
66 namespace WindowsApplication1
67 {
68     public partial class Form5 : Form
69     {
70         private ArrayList listData3;
71         //建立私有变量,该变量用以接收复窗体的共有属性数据
72         public Form5()
73         {
74             InitializeComponent();
75             //千万别放到构造函数里面,因为在主窗体修改按钮被点击后,开始执行Form5 formChild = new Form5();
76             //而在Form2的实例化过程中会在构造函数中执行Form4 pareForm = (Form4)this.Owner;而这时的this.Owner是没有值的,为空引用,
77         }
78 
79         private void Form5_Load(object sender, EventArgs e)
80         {
81             Form4 pareForm = (Form4)this.Owner;//实例化父窗体,建立父子窗体之间联系
82             this.listData3 = pareForm.ListData2;//访问父窗体的公有属性给当前子窗体的私有变量
83             foreach (object o in this.listData3)//遍历后将数据显示在listBox1之中
84             {
85                 this.listBox1.Items.Add(o);
86             }
87         }
88 
89         private void button1_Click(object sender, EventArgs e)
90         {
91             Application.Exit();
92         }
93     }
94 }
两个界面之间传值

3 菜单和菜单组件

分类   菜单栏 主菜单和子菜单

MenuStrip

方法

text   文件(&N) 会产生 文件(N)   运行时Alt+N 执行

ShortcutKeys  自定义快捷键组合 ,注:至少选择一项修饰符  Alt ,shift,ctrl

Separator 分割线  

美化: *.skn   *.ssk

第三方动态链接库

猜你喜欢

转载自www.cnblogs.com/zhenqk/p/11098683.html