-----------Windows Phone 7手机开发、.Net培训、.net学习型技术博客、期待与您交流! ------------
WinForm 应用程序和控制台应用程序( console )都是调用 .Net 框架。
WinForm包含有许多控件,控件是相似的,所以将这些类元素抽象为类。
开发WinForm程序的重点不是在界面,关键是在后台的代码。
控件有如:textbox,button,label,radioButton……
可对控件的属性值进行修改,让其在页面上显示不同效果,同时可以写控件里所包含的事件,让其在系统运行时,运算处理任务。
求两数和如图:
可在按钮button1 事件里写上如下代码:
string str1 = textBox1.Text;
string str2 = textBox2.Text;
int i1, i2;
if (!int.TryParse(str1, out i1))
{
MessageBox.Show("1非法输入");
}
if(!int.TryParse(str2,out i2))
{
MessageBox.Show("2输入非法");
}
int i3 = i1 + i2;
label1.Text = i3.ToString();
欢迎窗口例子
代码:
private void button1_Click(object sender, EventArgs e)
{
//接收页面文本框输入的值
string namg = textBox1.Text;
//this.Text = namg+",欢迎观临!";
//这是界面的标题语"XX,您好"
this.Text= string.Format("{0}您好",namg);
//将名字+欢迎的话 现实在label控件上
label1.Text = namg + ",欢迎观临!";
textBox1.Hide();
}
这里的this 代表本窗体,窗体可以看成是一个类的实例;
当用户点击“button1”时,ButtonClick方法被调用,进而执行方法里面的代码。
向左向右移动一位字符的例子:
代码:
//自动向左移动一位
private void button1_Click(object sender, EventArgs e)
{
string str=textBox1.Text;
char first=str[0];
string sx = str.Substring(1);//截取从第二位开始到结束的字符
textBox1.Text = sx + first;
}
//自动向右移动一位
private void button2_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
char last = str[str.Length-1];
string sx = str.Substring(0, str.Length - 1); //截取前str.Length-1位字符
//MessageBox.Show(sx.ToString());
textBox1.Text = last + sx;
}
文本框(textbox)的使用例子
可单行,可多行,可让系统自加信息,如AppendText:
textBox1.AppendText ( DateTime.Now.ToString()+"\n"); //自动添加了时间
注:
控件名要有意义,控件名前缀的“潜规则”。按钮buttonàbtn,文本框textbox àtxt,
复选框 Checkbox àcb.
退出程序:this.Close(); 或者 Application.Exit();
登录页面 的例子:错误超过三次就会系统退出
创建页面:
代码:
private int ErrTimes = 0;
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string passWord = txtPassword.Text;
if (userName.Equals("admin", StringComparison.OrdinalIgnoreCase) && passWord == "888888")
{
MessageBox.Show("登录成功!");
}
else
{
ErrTimes++;
if (ErrTimes >= 3)
{
MessageBox.Show("错误次数太多,程序即将关闭!");
Application.Exit();
}
MessageBox.Show("登录失败!");
}
}
密码修改例子:
源代码:
string oldpw = txtOldPW.Text;
string newpw = txtNewPW.Text;
string newpw2 = txtMPW.Text;
if (oldpw != "888888")
{
MessageBox.Show("旧密码输入有误!请重新输入");
txtOldPW.Focus(); //焦点放在有误的文本框中
return;
}
else
{
if (newpw != "" && newpw == newpw2)
{
MessageBox.Show("密码修改成功!");
}
else
{
MessageBox.Show("新密码有误");
}
}
求学生最大成绩和对应姓名的例子:
代码:
//按照\r\n进行split
string[] lines = textBox1.Lines;
string maxName = "";
int maxScore = -1;
foreach(string line in lines)
{
string[] strs = line.Split('=');
string name=strs[0];
string strScore=strs[1];
int score = Convert.ToInt32(strScore);
if(score>maxScore)
{
maxName = name;
maxScore = score;
}
}
MessageBox.Show(string.Format("{0}是第一名,成绩是{1}分",maxName,maxScore));
四则运算的例子(combox)
代码:
string shu1=textBox1.Text;
string shu2=textBox2.Text;
int i1 = Convert.ToInt32(shu1);
int i2 = Convert.ToInt32(shu2);
int rusult = 0;
switch(comboBox1.SelectedIndex)
{
case 0:
rusult = i1 + i2;
break;
case 1:
rusult = i1 - i2;
break;
case 2:
rusult = i1 * i2;
break;
case 3:
if (shu2 == "0")
{
MessageBox.Show("除数不能为0");
return;
}
rusult = i1 / i2;
break;
default:
throw new Exception("未知的运算符");
}
textBox3.Text = rusult.ToString();
省市的选择例子
代码:
comboBox2.Items.Clear();//清空旧数据
string sheng = Convert.ToString(comboBox1.SelectedItem);
if(sheng=="福建")
{
comboBox2.Items.Add("泉州");
comboBox2.Items.Add("厦门");
comboBox2.Items.Add("福州");
comboBox2.Items.Add("漳州");
comboBox2.Items.Add("莆田");
}
if(sheng =="山东")
{
comboBox2.Items.Add("潍坊");
comboBox2.Items.Add("济南");
comboBox2.Items.Add("青岛");
}
if (sheng == "河南")
{
comboBox2.Items.Add("洛阳");
comboBox2.Items.Add("郑州");
comboBox2.Items.Add("三门峡");
}
if(sheng == "河北")
{
comboBox2.Items.Add("保定");
comboBox2.Items.Add("石家庄");
comboBox2.Items.Add("廊坊");
comboBox2.Items.Add("秦皇岛");
}
-----------Windows Phone 7手机开发、.Net培训、.net学习型技术博客、期待与您交流! ------------