winForm learning portal

Windows Forms
  1. Attributes:

    1. name: name of the object
    2. windowsState: the size of the initialization of the form, Normal, Minimized, Maximized
    3. StartPosition: Form the starting position, Manual (determined by the Location property), CenterScreen (centered), WindowsDefaultLocation (Windows default position), WindowsDefaultBounds (Windows default position, the boundary is determined by Windows), CenterParent (parent window centered)
    4. Text: caption of the form to the user to see
    5. MaximizeBox, MinimizeBox: Is there a minimize button maximize
    6. BackColor: background color
    7. BackgroundImage: Background image
    8. BackgroundImageLayout: background image layout, None (Left display), Tile (duplicate image, the default value), the Stretch (stretching), Center (middle), Zoom (scaled to the appropriate size)
    9. Enabled: Forms are available
    10. Font: Set the font on the form
    11. ForeColor: Set text color on the form
    12. Icon: Icon Set Form
  2. event

    1. load: form load event
    2. mouseClick: mouse click event
    3. mouseDoubleClick: double click event
    4. mouseMove: mouse motion events
    5. keyDown: Keyboard down events
    6. keyUp: keyboard release event
    7. FormClosing: form closes event
    8. FormClosed: form is closed after the incident
  3. The message box (DialogResult MessageBox.Show (prompt, name of the form, select the item button MessageBoxButtons, prompt icon MessageBoxIcon)

    1. Option buttons: OK, OKCancel, AbortRetryIgnore (Abort, Retry, Ignore), YesNoCancel, YesNo, RetryCancel (retry, cancel)
      2. prompt icon: None, (Hand, Stop, Error) red x, Question question mark ( Exclamation, warning) warning, (Asterisk, Information) Tip
      3. return value: None, Ok, Cancel, abort ( abort), Retry (retry), ignore (ignore), Yes, No
  4. Label and LinkLabel: Label control

    • Name: name tag, a unique identification
    • Text: Content
    • Font: text style
    • FontColor: Text Color
    • BackColor: background color
    • Image: Background image
    • AutoSize: whether to automatically adjust the size of the label true, false
    • Size: size of the label
    • Visible: the label is visible
      5.TextBox: Text Box
    • Text: text
    • MaxLength: text box to enter the maximum number of characters
    • WordWrap: Wrap
    • PasswordChar: Password characters are replaced
    • Multiline: Multiline Text Box
    • ReadOnly: Read-only
    • Lines: Lines of text
    • ScrollBars: scrollbar
  5. Button: Button control

  6. RadioButton: radio button

  7. CheckBox: checkboxes

  8. CheckedListBox: multiple choice collection

  9. ListBox: List

    • MultiColumn: whether to support multi-column list
    • Items: Value list box
    • SelectedItems: a collection of selected items
    • SelectedItem: Select items
    • SelectedIndex: index of the selected item
    • SelectionMode: List selection mode
      • One: a single selection
      • MultiSimple: choose more than one
      • None: not selected
      • MultiExtended: choose more than one but press the shift
    • method:

      • Add: Add Item
      • Insert: add items specified location
      • Remove: Remove Items

         /// <summary>
         /// 添加按钮
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void Button1_Click(object sender,EventArgs e) {
         if(textBox1.Text != null && textBox1.Text != "") {
         listBox1.Items.Add(textBox1.Text);
         textBox1.Text = "";
         } else {
         MessageBox.Show("不得为空");
         }
        
         }
         /// <summary>
         /// 查看选中
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void Button2_Click(object sender,EventArgs e) {
         string s = "";
         for(int i = 0;i < listBox1.SelectedItems.Count;i++) {
         s = s + "   " + listBox1.SelectedItems[i].ToString();
         }
         MessageBox.Show("选中项为" + s);
         }
        
         /// <summary>
         /// 删除
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void Button3_Click(object sender,EventArgs e) {
         List<string> list = new List<string>();
         for(int i = 0;i < listBox1.SelectedItems.Count;i++) {
         list.Add(listBox1.SelectedItems[i].ToString());
         }
         foreach(string s in list) {
         listBox1.Items.Remove(s);
         }
         }
        
  10. ComboBox: Combo Box
    • DropDownStyle: Appearance Simple (text and list boxes, text boxes can be edited), DropDown (display only text box, the text box with the mouse to start editable), DropDownList (display only text boxes, text boxes can be expanded by a mouse, Not editable)
    • Items: Gets or sets combination
    • Text: Gets or sets a combination of text box
    • Gets or sets a maximum number of items displayed: MaxDropDownItems
    • Stored: Whether ordering
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_Load(object sender,EventArgs e) {
        comboBox1.Items.Add("计算机网络技术");
        comboBox1.Items.Add("软件工程");
        comboBox1.Items.Add("生物制药");
        comboBox1.Items.Add("会计");
        comboBox1.Items.Add("平面设计");
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender,EventArgs e) {
        if (textBox1.Text == "") {
        MessageBox.Show("不得为空");
        return;
        }
        if (comboBox1.Items.Contains(textBox1.Text)) {
        MessageBox.Show("当前专业已存在");
        return;
        }
        comboBox1.Items.Add(textBox1.Text);
        textBox1.Text = "";
        }
        /// <summary>
        /// combox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox1_SelectedIndexChanged(object sender,EventArgs e) {
        DialogResult dialogResult = MessageBox.Show("当前所选专业为" + comboBox1.Text + "是否删除?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
        if (dialogResult == DialogResult.Yes) {
        comboBox1.Items.Remove(comboBox1.Text);
        MessageBox.Show("删除完成");
        }
        }
      
  11. PictureBox: Picture Control

    • Image: Gets or sets the display of pictures
    • ImageLocation: Gets or sets the image path
    • SizeMode: set the image display size and position Normal (displayed in the upper left), Stretchimage (to adapt to the size of the controls), AutoSize (size adapted to control the image size), Centerimage (picture in picture control center), Zoom (The picture is automatically scaled to meet the picture control the size of)
  12. Timer: Timer control

        /// <summary>
        /// 当窗体最小化时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SizeChange(object sender,EventArgs e) {
        if(WindowState == FormWindowState.Minimized) {
        this.Hide();
        notifyIcon1.Visible = true;
        notifyIcon1.ShowBalloonTip(20,"demo","this is a demo",ToolTipIcon.Warning);
        }
        }
    
  13. DateTime date time control

    • Short: Short date format, for example 2019/1/1
    • Long: long date format, such as January 1, 2019
    • Time: displays only the time, 22:00:00 e.g.
    • Custom: user-defined display format
        /// <summary>
        /// 定时器设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DateTime_Load(object sender,EventArgs e) {
        dateTimePicker1.Format = DateTimePickerFormat.Time;
        timer1.Interval = 1000;
        timer1.Start();
        }
        /// <summary>
        /// 定时器触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer1_Tick(object sender,EventArgs e) {
        dateTimePicker1.ResetText();
        }
      
  14. ContextMeanStrip: Right-click menu control

  15. MonthCalendar: Time Control
  16. treeView
        private void TreeView_Load(object sender,EventArgs e) {
        treeView1.Nodes.Add("全部信息");
        }
        /// <summary>
        /// 添加下级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender,EventArgs e) {
        if(treeView1.SelectedNode == null) {
        MessageBox.Show("请选择节点!!!!");
        } else if(textBox1.Text != "") {
        TreeNode treeNode = new TreeNode(textBox1.Text);
        treeView1.SelectedNode.Nodes.Add(treeNode);
        } else {
        MessageBox.Show("节点信息不得为空!");
        }
        }
        /// <summary>
        /// 添加同级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender,EventArgs e) {
        if(treeView1.SelectedNode.Parent == null) {
        MessageBox.Show("请添加下级");
        return;
        }
        if(treeView1.SelectedNode == null) {
        MessageBox.Show("请选择节点!!!!");
        } else if(textBox1.Text != "") {
        TreeNode treeNode = new TreeNode(textBox1.Text);
        treeView1.SelectedNode.Parent.Nodes.Add(treeNode);
        treeView1.ExpandAll();
        } else {
        MessageBox.Show("节点信息不得为空!");
        }
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender,EventArgs e) {
        if(treeView1.SelectedNode == null) {
        MessageBox.Show("请选择节点!!!!");
        } else if(treeView1.SelectedNode.Nodes.Count == 0) {
        treeView1.SelectedNode.Remove();
        } else {
        MessageBox.Show("请先删除子节点");
        }
        }
    
  17. listView

     /// <summary>
        /// 初始化列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListView_Load(object sender,EventArgs e) {
        //标题
        ColumnHeader c1 = new ColumnHeader();
        c1.Width = 100;
        c1.Text = "姓名";
        ColumnHeader c2 = new ColumnHeader();
        c2.Width = 50;
        c2.Text = "年龄";
        ColumnHeader c3 = new ColumnHeader();
        c3.Width = 100;
        c3.Text = "手机号";
        //显示网格线
        listView1.GridLines = true;
        //显示全行
        listView1.FullRowSelect = true;
        //设置只能单选
        listView1.MultiSelect = false;
        //显示详细信息
        listView1.View = View.Details;
        //添加标题
        listView1.Columns.Add(c1);
        listView1.Columns.Add(c2);
        listView1.Columns.Add(c3);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender,EventArgs e) {
        if(label1.Text.Equals("") && label2.Text.Equals("") && label3.Text.Equals("")) {
        ListViewItem item = new ListViewItem();
        //第一列的数据
        item.Text = textBox1.Text;
        //第二列的数据
        item.SubItems.Add(textBox2.Text);
        //第三列数据
        item.SubItems.Add(textBox3.Text);
        listView1.Items.Add(item);
        MessageBox.Show("添加成功");
        //添加后把文本输入框清空
        foreach(Control c in Controls) {
        if(c is TextBox) {
        c.Text = "";
        }
        }
        } else {
        MessageBox.Show("请填写内容");
        }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender,EventArgs e) {
    
        List<ListViewItem> list = new List<ListViewItem>();
        for(int i = 0;i < listView1.SelectedItems.Count;i++) {
    
        list.Add(listView1.SelectedItems[i]);
        }
        foreach(ListViewItem item in list) {
    
        listView1.Items.Remove(item);
        }
        }
    
  18. notifyIcon: Tray Control

        /// <summary>
        /// 当窗体最小化时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SizeChange(object sender,EventArgs e) {
        if(WindowState == FormWindowState.Minimized) {
        this.Hide();
        notifyIcon1.Visible = true;
        notifyIcon1.ShowBalloonTip(20,"demo","this is a demo",ToolTipIcon.Warning);
        }
        }
    
  19. toolTip: Bubble Controls
              /// <summary>
        /// 设置气泡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenAndSaveFile_Load(object sender,EventArgs e) {
        toolTip1.ToolTipTitle = "提示";
        toolTip1.ToolTipIcon = ToolTipIcon.Info;
        toolTip1.SetToolTip(this.button1,"打开文件");
        }
    
  20. MDI form: Form window vivo
  21. progressBar progress bar control
        /// <summary>
        /// 开始跑进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click_1(object sender,EventArgs e) {
        progressBar1.Maximum = 1000;
        progressBar1.Value = i;
        label1.Show();
        while (progressBar1.Value!=progressBar1.Maximum){
        label1.Text = "进度:"+progressBar1.Value*100 / progressBar1.Maximum+"%";
        label1.Refresh();
        i++;
        progressBar1.Value = i;
        Thread.Sleep(100);
        }
    
  22. panel container
        /// <summary>
        /// 控制panel容器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender,EventArgs e) {
        //清空容器容器中的控件
        this.panel1.Controls.Clear();
        //new一个进度条控件
        progressBar progress = new progressBar();
        //不设置或出现System.ArgumentException:“无法将顶级控件添加到控件。”
        progress.TopLevel = false;
        //让进度条控件的大小以容器大小为主
        progress.Dock = DockStyle.Fill;
        //去除窗口边界
        progress.FormBorderStyle = FormBorderStyle.None;
        //设置最大化
        progress.WindowState = FormWindowState.Maximized;
        //隐藏工具栏
        progress.Visible = false;
        //容器大小设置为窗口大小
        panel1.Height = Screen.PrimaryScreen.Bounds.Size.Height;
        panel1.Width = Screen.PrimaryScreen.Bounds.Size.Width;
        this.panel1.Controls.Add(progress);
        progress.Show();
        }
    
    The relevant code in GitHub address

Guess you like

Origin www.cnblogs.com/JaminYe/p/11256450.html