winform基础控件-例子学习

时间:2023-03-09 18:47:18
winform基础控件-例子学习

1、如图实现整数计算器

winform基础控件-例子学习

ComboBox控件:

Items属性:添加集合中的项。

this.comoper.Items.AddRange(new object[] {
"+",
"-",
"x",
"/"});

TextBox:

对TextBox的输入文本有限制:

1)只能输入数值型(整数和小数);

2)小数点不能开头,小数只能输入一位;

3)不满足要求的输入统一不接受。

实现方法

使用TextBox的KeyPress事件:在控件有焦点的情况下按下键时发生。

KeyChar属性获取或设置与按下的键对应的字符。

KeyPress 事件不能由非字符键引发;但是非字符键能够引发 KeyDownKeyUp 事件。

使用 KeyChar 属性采样运行时的键击,以及使用或修改常用键击的一个子集。

若要仅在窗体级别处理键盘事件而不允许其他控件接收键盘事件,请将窗体的 KeyPress 事件处理方法中的 KeyPressEventArgs. Handled 属性设置为true。

//处理键盘按键盘事件,当前时间焦点在TextBox控件级别
private void textnum1_KeyPress(object sender, KeyPressEventArgs e)
{
//通过sender得到触发该事件的控件
TextBox currtextbox = sender as TextBox; if (e.KeyChar<'' || e.KeyChar>'')
{
//用户输入的不是数字
e.Handled = true;
}
//用户是否输入了退格键
if (e.KeyChar==)
{
e.Handled = false;
}
//让用户输入小数点,判断是不是小数点
if(e.KeyChar==)
{
//只让用户输入一个小数点,检测当前文本框是否有小数点
//报告指定 Unicode 字符或字符串在此实例中的第一个匹配项的从零开始的索引。 如果未在此实例中找到该字符或字符串,则此方法返回 -1。
if (currtextbox.Text.IndexOf(".") == -)
{
  //小数点不能在第一位,注意字符是单个单个输入的
                    //SelectionStart获取或设置文本框中当前输入的文本起始点。
if(currtextbox.SelectionStart>)
{
e.Handled = false;
}
} }
}

winform基础控件-例子学习

using System;
using System.Windows.Forms; public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
} private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed. // If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
} public static void Main()
{
Application.Run(new Form1());
}
}

2、图片查看器

winform基础控件-例子学习

身份证号码的校正:

      private bool CheckCardId(string id)
{
//校验位的权值或编码值
int[] wQuan = { , , , , , , , , , , , , , , , , };
string checkWei = "10X98765432";
string number17 = id.Substring(, );
string number18 = id.Substring();
int sum=;
for(int i=;i<;i++)
{
sum+=Convert.ToInt32(number17[i].ToString())* wQuan[i];
//char类型不能直接转换成整形(实际转换成对应的ascii),先tostring在转换 }
int mod = sum % ;
string result = checkWei[mod].ToString();
if(number18.Equals(result,StringComparison.OrdinalIgnoreCase))
{
return false;
}
else
{
return true;
}
} private void btnView_Click(object sender, EventArgs e)
{
//身份证可能是15位或者18位
//15位身份证组号:省(20)市(2)县(2)年(2)月(2)日(2)+3序列号(奇男偶女)
//18位身份证:第一:出生年前加上19 第二点:第18位校验位,从前17位计算而来
string id = pidid.Text;
int age = ;
int year = ;
if (id.Length == )
{
year = Convert.ToInt32(id.Substring(, )) + ;
}
else if (id.Length == )
{
if(!this.CheckCardId(id))
{
MessageBox.Show("身份证输入有误,请检查");
return;
}
year = Convert.ToInt32(id.Substring(, ));
}
else
{
MessageBox.Show("身份证长度输入有误,请重新输入");
return;
}
age = DateTime.Now.Year - year;
if (age >= )
{
pic.Visible = true;
}
else
{
MessageBox.Show("你太小了,回家看动画片吧");
}
}

3、访问网址

WebBrowser类:使用户可以在窗体内导航网页。

winform基础控件-例子学习

private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(button1.Text);
}

winform基础控件-例子学习

4.树形控件增删该查

winform基础控件-例子学习

TreeView:

与上图对应的方法:

   private void button1_Click(object sender, EventArgs e)
{
treeView1.HideSelection = false;
MessageBox.Show(treeView1.SelectedNode.Text); } private void button2_Click(object sender, EventArgs e)
{
//MessageBox.Show("根节点节点的数量{0}", treeView1.Nodes.Count.ToString());
//treeView1.Nodes.Add("add");根节点添加
//treeView1.Nodes[0].Nodes[0].Nodes.Add("王晓东");//添加到一级节点下
if(treeView1.SelectedNode!=null)
{
treeView1.SelectedNode.Nodes.Add(textBox1.Text);
}
if(treeView1.SelectedNode==null)
{
treeView1.Nodes.Add(textBox1.Text);
} } private void TreeView_Load(object sender, EventArgs e)
{
treeView1.ExpandAll();//展开节点
treeView1.Nodes[].ImageIndex = ;
} private void button4_Click(object sender, EventArgs e)
{
if(treeView1.SelectedNode!=null)
{
//treeView1.SelectedNode.ExpandAll();//展开当前节点下的所有节点
treeView1.SelectedNode.Expand();//展开当前节点下的所子节点
}
} private void button3_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Collapse();//关闭当前节点下的所有节点
}
} private void button5_Click(object sender, EventArgs e)
{
treeView1.SelectedNode.Remove();
} private void button6_Click(object sender, EventArgs e)
{
string str="选中的语言";
foreach(TreeNode tn in treeView1.Nodes[].Nodes[].Nodes)
{
if(tn.Checked==true)
{
str = str + tn.Text;
}
}
MessageBox.Show(str);
} private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
foreach (TreeNode tn in e.Node.Nodes)
{
tn.Checked = e.Node.Checked;
}
}