VS操作快捷键
(说实话,我块吐了)
这和VScode完全不在一个档次,但无奈VSCode调试Winform程序不太可能,只能凑合
- VS代码美化: ctrl + K + F
- 任何你看到的代码提示都可以双TAB输入
- 类的无参构造函数 ctor +双TAB
- 类的属性 prop + 双TAB
Tips
- 每个解决方案里可以放多个项目,但需要把一个项目设置为启动项目
- 窗体ICON图标一般大小 32*32
- 我们自己写的初始化语句最好写在窗体load方法中
- 有时候一些控件遮挡另一些控件,此时我们可以在对应位置右键,在弹出框中选择后面的控件
有些事件是有时态的,比如closing和closed;这跟英语的时态逻辑一样. 在做清理、销毁等程序时,放在closing里比较适合
使用进程管理器等外部工具关闭窗口时,是不会触发内部的closing和closed方法的
下面这些事件可以让程序互动性更好,比如添加hover等效果
- 所有的控件均被this.Controls管理着
- 如果有超链接可以用LinkLabel来做,和label一样,只是样式更像链接
一、combox下拉列表控件
1. 外观样式
combox的外观分为三种:
其中simple模式下不用下拉,直接展现:
DropDown和DropDownList的区别是 DropDown的选中文字可以修改,DropDownList的选中项目不能修改
DropDown可以修改
DropDownList不能修改
2. combox导入数据及级联小例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Dictionary<string, string> D1 = new Dictionary<string, string> { };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
D1.Add("北京", "东城,西城,故宫");
D1.Add("上海", "闵行,宝山,陆家嘴");
D1.Add("济南", "趵突泉,大明湖,英雄山");
foreach (var item in D1)
{
comboBox1.Items.Add( item.Key);
}
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
comboBox2.Items.AddRange(D1[comboBox1.SelectedItem.ToString()].Split(','));
comboBox2.SelectedIndex = 0;
}
}
}
二. CheckBox复选框
1. CheckBox可以设置三种状态
就像下面的例子. 想达到这种效果,我们只需要将需要三态的复选框的ThreeState
属性设为True
然后操作其CheckState
属性即可.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ChildCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked & checkBox3.Checked)
checkBox1.CheckState = CheckState.Checked;
else if (!checkBox2.Checked & !checkBox3.Checked)
checkBox1.CheckState = CheckState.Unchecked;
else
checkBox1.CheckState = CheckState.Indeterminate;
}
}
}