本文共 5359 字,大约阅读时间需要 17 分钟。
1.TextBox控件:该控件可以输入信息
WordWrap:指示控件是否可以换行 PassWrodChar:使输入框显示一个单一文本(常用于密码栏) 事件:TextChanged当文本框内容改变时触发这一事件2.Timer控件
每隔设定的时间就会触发事件(比如幻灯片)3.focus:设置焦点
4.单选和多选控件:
多选控件:checkBox 单选控件:RadioButton checked属性:指示这个控件是否被选中状态 默认状态下,在一个窗体中,所有的单选按钮只允许选中一个,可以使用groupbox分组来选择5.webrowse:浏览器控件
uri:指向的网址 属性值中自动加https://,用代码赋值时,要手动加6.ComboBox下拉框控件:
[外链图片转存失败,源站可能有防盗链机制,建议将在这里插描述]入!图片上https://传(imbQog.csdnimg.cn/2021022506099h.c//img-blog.csdnimg.cn/20210225092620995.png)] DropDownStyle:设置下拉框格式7.pictureBox控件
sizemode:设置图片格式:比如填充等 Image:图片指向的地址8.listBox控件:
SelectedIndex:当前使用项的索引10.菜单控件MenuStrip
常用的类汇总:
1.Directory可以操作文件夹(创建对象去使用) CreateDiretory 创建文件夹 Delete 删除文件夹 Move 剪切文件夹 Exist 判断是否存在 GetFiles 获得指定的目录下所有文件的全路径 GetDirectory 获得指定目录下所有文件夹的全路径2.OpenfileDialog类(打开文件对话框)
//点击弹出对话框 OpenFileDialog ofd = new OpenFileDialog(); //设置对话框的标题 ofd.Title = "请选择要打开的文件"; //设置多选 ofd.Multiselect = true; //设置初始目录 ofd.InitialDirectory = @"C:\Users\14505\Desktop"; //设置打开文件框的文件类型 ofd.Filter = "文本文件|*.txt|音乐文件|*.wav|所有文件|*.*"; //展示对话框 ofd.ShowDialog(); //获得打开的文件的路径 string str =ofd.FileName; using (FileStream fsRead = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; //实际读取到的字节数 int r = fsRead.Read(buffer, 0, buffer.Length); textBox1.Text= Encoding.Default.GetString(buffer, 0, r); }
3.SaveFileDialog类(保存对话框)
和上边使用代码基本一样4.FondDialog字体对话框
ColorDialog 颜色对话框 直接FondDialog flg = new FondDiaLog(); flg.Show(); textbox1.Text.Fond=flg.Fond;就可以了实战之简单记事本小程序:
首先添加菜单,将Form设计成这个样子Listlist = new List (); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //加载程序时,隐藏panel panel1.Visible = false; //自动换行关闭 textBox1.WordWrap = false; } private void button1_Click(object sender, EventArgs e) { //点击按钮时,同样要隐藏 panel1.Visible = false; } private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) { panel1.Visible = true; } private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e) { panel1.Visible = false; } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "请选择要打开的文本文件"; ofd.InitialDirectory = @"C:\Users\14505\Desktop"; ofd.Multiselect = true; ofd.Filter = "文本文件|*.txt|所有文件|*.*"; ofd.ShowDialog(); //获得用户选择的文件的路径 string input = ofd.FileName; //将文件的全路径存储到泛型集合当中 list.Add(input); if (input == "") { return; } using (FileStream fsRead = new FileStream(input, FileMode.OpenOrCreate, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length); textBox1.Text = Encoding.Default.GetString(buffer, 0, r); } } private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { using (FileStream fswrite = new FileStream(@"C:\Users\14505\Desktop\new1.txt", FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = Encoding.Default.GetBytes(textBox1.Text.Trim()); fswrite.Write(buffer, 0, buffer.Length); } MessageBox.Show("保存成功!"); } private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e) { if (自动换行ToolStripMenuItem.Text == "☆自动换行") { textBox1.WordWrap = true; 自动换行ToolStripMenuItem.Text = "★取消自动换行"; } else if (自动换行ToolStripMenuItem.Text == "★取消自动换行") { textBox1.WordWrap = false; 自动换行ToolStripMenuItem.Text = "☆自动换行"; } } private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) { FontDialog fd = new FontDialog(); fd.ShowDialog(); textBox1.Font = fd.Font; } private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); cd.ShowDialog(); textBox1.ForeColor = cd.Color; } private void listBox1_DoubleClick(object sender, EventArgs e) { //要获得双击文件对应的全路径 string path = list[listBox1.SelectedIndex]; using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length); textBox1.Text = Encoding.Default.GetString(buffer, 0, r); } }
转载地址:http://muvo.baihongyu.com/