---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
计算两个数的和、累加器
private void button1_Click(object sender, EventArgs e)
{
string str1 = textBox1.Text;
string str2 = textBox2.Text;
int i1, i2;
if(!int.TryParse(str1,out i1))
{
MessageBox.Show("第一个数不是合法的整数");
return;//不要忘了return
}
if (int.TryParse(str2, out i2) == false)
{
MessageBox.Show("第而个数不是合法的整数");
return;
}
int i3 = i1 + i2;
textBox3.Text = Convert.ToString(i3);
}
private void button2_Click(object sender, EventArgs e)
{
string str1 = textBox4.Text;
string str2 = textBox5.Text;
int i1, i2;
if (!int.TryParse(str1, out i1))//尝试转换 将str1尝试转换成 i1,转换成功返回值为true 失败返回值为false
{
MessageBox.Show("数字1格式错误!");
return;//不能丢 错误了就不它他再继续往下执行
}
if (!int.TryParse(str2, out i2))
{
MessageBox.Show("数组2格式错误!");
return;
}
if (i1 >= i2)
{
MessageBox.Show("第二个数要大于第一个数!");
return;
}
int sum = 0;
for (int i = i1; i <= i2; i++)
{
sum += i;
}
textBox6.Text = Convert.ToString(sum);
}
ComboBox
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//序号 从0开始
MessageBox.Show(Convert.ToString(comboBox1.SelectedValue));//数据库中用到
MessageBox.Show(Convert.ToString(comboBox1.SelectedText));//数据库中用到
MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));//选中的 项
}
private void btnResult_Click(object sender, EventArgs e)
{
string str1 = txtNum1.Text;
string str2 = txtNum2.Text;
int i1 = Convert.ToInt32(str1);
int i2 = Convert.ToInt32(str2);
int result;
switch (cbx运算符.SelectedIndex)
{
case 0:
result = i1 + i2;
break;
case 1:
result = i1 - i2;
break;
case 2:
result = i1 * i2;
break;
case 3:
if (i2 == 0)//细节决定成败!
{
MessageBox.Show("0不能当除数!");
return;
}
result = i1 / i2;
break;
default://防患于未然!
throw new Exception("未知的运算符!");
}
txtResult.Text = Convert.ToString(result);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(Convert.ToString(comboBox1.SelectedItem));
}
private void cbxProvince_SelectedIndexChanged(object sender, EventArgs e)
{
cbxCity.Items.Clear();//清空旧数据
string prov = Convert.ToString(cbxProvince.SelectedItem);
if (prov == "山东")//1.选择了一个省,选择对应的市,再换个省的时候刚才选得 市,并没有删除。怎么删除 方法:换成DropDownList样式
{
cbxCity.Items.Add("潍坊");
cbxCity.Items.Add("临沂");
cbxCity.Items.Add("青岛");
}
if (prov == "河南")
{
cbxCity.Items.Add("郑州");
cbxCity.Items.Add("三门峡");
cbxCity.Items.Add("洛阳");
}
if (prov == "辽宁")
{
cbxCity.Items.Add("沈阳");
cbxCity.Items.Add("鞍山");
cbxCity.Items.Add("本溪");
}
}
//只能选不能填 属性DropDownStyle设置成 DropDownList
邮箱分析:
private void button1_Click(object sender, EventArgs e)
{
string email = textBox1.Text;
string[] strs = email.Split('@');
if (strs.Length != 2)
{
MessageBox.Show("非法的Email地址!");
return;
}
textBox2.Text = strs[0];
textBox3.Text = strs[1];
}
多行文本框练习:
private void btnResult_Click(object sender, EventArgs e)
{
//string s = txtScore.Text; //方法1:按照\r\n进行split
string[] lines = txtScore.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));
}
根据年龄(身份证)显示图片:
private void button1_Click(object sender, EventArgs e)
{
string identity = textBox1.Text;
pictureBox1.Visible = true;//不管用 在属性中设置
string strYear = identity.Substring(6, 4);
int Year = Convert.ToInt32(strYear);
if ((DateTime.Now.Year - Year) > 18)
{
pictureBox1.Visible = true;
}
else
{
pictureBox1.Visible = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
//向左滚动
string str = textBox2.Text;
char first = str[0];
string last = str.Substring(1);
textBox2.Text = last + first;
}
private void button3_Click(object sender, EventArgs e)
{
//向又滚动
string str = textBox2.Text;
char lastC = str.Substring(str.Length - 1)[0];
string last = str.Substring(0, str.Length - 1);
textBox2.Text = lastC + last;
}
文本框练习:
private void button1_Click(object sender, EventArgs e)
{
textBox1.AppendText(DateTime.Now.ToString() + "\n");//给多行文本附加文本
//textBox1.Text += DateTime.Now.ToString() + "\n"; //效率低()
}
用户登录练习:
//类字段
private int ErrorTimes = 0;
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string pwd = txtPwd.Text;
if (userName.Equals("admin", StringComparison.OrdinalIgnoreCase) && pwd == "888")//忽略大小写。
{
MessageBox.Show("登陆成功!");
}
else
{
//int i = 0;
//i++;
//if (i >= 3)
//{
// MessageBox.Show("错误次数过多!程序即将退出!");
// Application.Exit();
//}
ErrorTimes++;//局部变量每次运行完毕变量的值都会被销毁,下次再运行,会重新初始化。而类字段,只要是一个对象,那么只要对象不销毁,就会一直保持对象的字段值。
if (ErrorTimes >= 3)
{
MessageBox.Show("错误次数过多!程序即将退出!");
Application.Exit();
}
MessageBox.Show("登录失败");
}
}
private void btnModify_Click(object sender, EventArgs e)
{
string oldPwd = txtOldPwd.Text;
string newPwd = txtNewPwd.Text;
string newPwd1 = txtNewPwd1.Text;
if (oldPwd != "888")
{
MessageBox.Show("旧密码错误!");
return;
}
if (newPwd != newPwd1)
{
MessageBox.Show("两次输入密码不一致!");
return;
}
if (newPwd == oldPwd)
{
MessageBox.Show("旧密码和新密码不能一样!");
return;
}
MessageBox.Show("修改成功!");
}
---------------------- Windows Phone 7手机开发 、 .Net培训 、期待与您交流! ----------------------