您的位置:首页 > 编程语言 > C#

c#windows窗体程序设计

2013-04-12 12:08 441 查看
一:windows窗体程序设计

(1)窗体创建的几种方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//  Application.Run(new Form1());
Form1 f1 = new Form1();

Form2 f2 = new Form2();
f2.Show();
Form3 f3 = new Form3();
f3.Show();

Application.Run(f1);
}
}
}




(2)编写事件处理程序

1、选中控件
2、在“属性”窗口中单击“闪电”图标
3、找到要处理的事件
4、生成事件处理方法
5、编写处理代码
例:随鼠标移动,显示鼠标当前坐标
点击选中窗体
属性窗体中找到MouseMove事件
双击事件生成事件处理程序框架
编写代码
private voidForm1_MouseMove(object sender, MouseEventArgs e ){
this.Text = String.Format(“鼠标位置({0},{1})", e.X,e.Y ); }


常用窗体事件

Load :窗体加载事件(即将显示尚未显示),常用作初始化功能
Click事件:鼠标单击事件
DoubleClick事件:鼠标双击事件
MouseMove:鼠标移动事件
KeyDown:键盘按下事件
KeyUp:键盘释放事件
小例子:单击窗体计1分,双击窗体计10分,编写程序,随时显示得分总数。
定义变量n存储分数,初值为0
单击事件click中n+1,并刷新
双击事件DoubleClick中n+10,并刷新

private void Form1_Click(object sender, EventArgs e)
{
n += 1;
string s = string.Format("得分{0}", n);
this.Text = s;

}

private void Form1_DoubleClick(object sender, EventArgs e)
{
n += 10;
string s = string.Format("得分{0}", n);
this.Text = s;

}




(3)菜单控件

设计学生管理菜单,注意菜单分隔条的设计
实现“退出”功能
关闭窗体,调用窗体类的Close方法
关闭当前窗体 this.Close( );





private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}




(4)消息框



// 最简单的消息框
MessageBox.Show( “消息内容" );
// 带标题的消息框
MessageBox.Show(“消息内容”, “标题");
// 带标题、指定按钮的消息框
MessageBox.Show( “消息内容”,
“标题",
MessageBoxButtons.OKCancel );
例:窗体上放一个按钮“发射核弹”,点此按钮后,访问用户,你确实要发射核弹吗?显示两个选项按钮:是、否。
如果用户选择是,则提示:核弹已经发射,你将为你所作所为付出代价。
如果用户选择否,则提示:你在最后关键时刻做出正确的抉择,避免了巨大损失。

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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DialogResult r= MessageBox.Show("你确实要发射核弹吗?","请确认",MessageBoxButtons.YesNo);
if (r == DialogResult.Yes)
{
MessageBox.Show("核弹已经发射,你将为你所作所为付出代价","请确认");

}
else
MessageBox.Show("你在最后关键时刻做出正确的抉择,避免了巨大损失。","请确认");
}

private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Top -= 5;
}

private void button5_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void button4_Click(object sender, EventArgs e)
{
pictureBox1.Left -= 5;
}

private void button5_Click_1(object sender, EventArgs e)
{
pictureBox1.Left += 5;
}

private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Top += 5;
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

private void 加数_TextChanged(object sender, EventArgs e)
{

}

private void button6_Click(object sender, EventArgs e)
{
double a, b, c;
a = double.Parse(加数.Text);
b = double.Parse(被加数.Text);
c = a + b;
和.Text = c.ToString();
}
}
}


运行图如下:这是多个实验的代码





(5)使用窗体控件

目录

文本框TextBox
单选框RadioButton和复选框CheckBox
分组框GroupBox
工具栏ToolStript和状态栏StatusStrip
图片框PictureBox
定时器Timer
多文档用户界面MDI

控件公用属性

Visible:是否可见
Enabled:是否可用
Left:横坐标
Top: 纵坐标
Width:宽度
Height:高度
---------------------------------------------------------------------

文本框TextBox

属性窗口中为控件命名(变量名)
读取文本
String s =textBox1.Text;
显示文本
textBox1.Text=“要显示的文字”;
单选框RadioButton

多选一,互斥
是否选中:Checked属性
如果选中则做某项工作
if(radioButton1.Checked) …
设置选中RadioButton
radioButton1.Checked= true;



private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
MessageBox.Show("恭喜你答对了", "请确定");
else
MessageBox.Show("很遗憾你答错啦", "正确答案是鲸鱼");








复选框CheckBox

多个选项多选多
是否选中:Checked属性
如果选中则做某项工作
if(checkBox1.Checked) …
设置选中CheckBox
checkBox1.Checked =true;
例:调查业余爱好,并提示“你的业余爱好有……”



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 cho5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
/*string hobby = "";
if (checkBox1.Checked)
hobby += checkBox1.Text + ",";
if (checkBox2.Checked)
hobby += checkBox2.Text + ",";
if (checkBox3.Checked)
hobby += checkBox3.Text + ",";
if (checkBox4.Checked)
hobby += checkBox4.Text + ",";
if (checkBox5.Checked)
hobby += checkBox5.Text + ",";

MessageBox.Show("你的业余爱好有:" + hobby);
*
*/
string hobby = "";
CheckBox[] array = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
for (int i = 0; i < array.Length; i++)
{
if(array[i].Checked)
hobby+=array[i].Text+",";
}

MessageBox.Show("你的业余爱好有:" + hobby);
}
}
}


分组框GroupBox

一个容器,用于组织分散的控件,使界面整齐美观。
填写用户注册信息,分为2部分,基本信息和教育信息。
问题:如果一个窗体上既有选择性别的RadioButton,又有选择学历的RadioButton,则它们之间如何实现正确的互斥?
可用容器将不同RadioButton分组,例如GroupBox

图片框PictureBox

用于显示图片。
常用属性:
SizeMode:图片尺寸模式(缩放、拉伸等)
常用方法:
Load(“位置”)。 可从磁盘或网络上加载并显示一幅图片。

定时器Timer

用于周期性的执行任务。
重要属性:
Interval : 定时间隔,以毫秒为单位
Enabled: 是否启动计时
重要事件:
Tick : 定时间隔到达

int n = 10;
private void timer1_Tick(object sender, EventArgs e)
{
n--;
label1.Text = n.ToString();

}




定时器Timer

例:导弹发射
窗体底部,有个“发射”按钮,旁边一个PictureBox放一个导弹图片。
点击“开始”,从10开始倒计时。
倒计时到0时,导弹开始向上移动,直到移出窗体。
用到的知识:
控件的Left属性表示横坐标,Top属性表示纵坐标
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 cho5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
int n = 10;
private void timer1_Tick(object sender, EventArgs e)
{
n--;

label1.Text = n.ToString();
if (n == 0)
{
timer1.Enabled = false;
timer2.Enabled = true;
}

}

private void Form2_Load(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void label1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

private void timer2_Tick(object sender, EventArgs e)
{
pictureBox1.Top -= 10;
if (pictureBox1.Top <= 0)
timer2.Enabled = false;
}
}
}








工具条ToolStrip

用于在窗体(通常是顶部)显示工具栏。
是一个容器,其中可包含多个工具栏按钮等控件。
常用属性:
ImageScalingSize 工具条上按钮图片缩放尺寸



工具条

例:设计一个服务器工具条,上有4个按钮:启动/继续、暂停、停止、退出
按钮同时显示文字和图片
图片缩放成32个像素大小
各个按钮之间要实现互斥逻辑关系:即服务启动以后,启动按钮不可用,暂停、停止可用。停止后,停止、暂停不可用,启动可用。退出按钮永远可用。

状态条StatusStrip

状态条StatusStrip与工具条ToolStrip类似,但通常里面包含Label而非Button。
例:在状态条上显示当前时间,并实时刷新
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 cho6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
toolStripButton1.Enabled = true;
toolStripButton2.Enabled = false;
toolStripButton3.Enabled = false;

}

private void toolStripButton4_Click(object sender, EventArgs e)
{
this.Close();
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.Enabled = false;
toolStripButton2.Enabled = true;
toolStripButton3.Enabled = true;
}

private void toolStripButton2_Click(object sender, EventArgs e)
{
toolStripButton1.Enabled = true;
toolStripButton2.Enabled = false;
toolStripButton3.Enabled = false;
}

private void toolStripButton3_Click(object sender, EventArgs e)
{
toolStripButton1.Enabled = true;
toolStripButton2.Enabled = false;
toolStripButton3.Enabled = false;
}

private void firstToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.MdiParent = this;
f.Show();
}

private void secondToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
f.MdiParent = this;
f.Show();
}
}
}




创建子窗体



private void button1_Click_1(object sender, EventArgs e)
{
string s = "server=.;database=SampleDb;User Id=sa;Password=123;";
SqlConnection c = new SqlConnection(s);
c.Open();
MessageBox.Show("数据库连接成功,接下来可以写数据啦");
c.Close();

}






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: