Winform-Controls

Winform-Controls

0.General

属性名 描述
TextAlign 文本对其方式
Enabled 是否可用
visible 是否可见
Anchor 自适应窗体实现

注意事项:

当自动生成控件时候,需要设置Autozie属性为true,要不然Text显示不全。

1.Form

1.1常用属性
		// 1.启动位置
	 	this.StartPosition = FormStartPosition.CenterScreen; 
        this.ShowIcon = false; //2.不显示Icon
		//4.当敲击键盘Esc键是所触发的Button
        this.CancelButton = btnTest; 
		//5.是否显示最大化、最小化、关闭按钮
        this.ControlBox = false; 
		//6.窗体透明度,取值范围 0.0 -> 1.0
  		this.Opacity = 0.6;  
 	    //7.如果把窗体的KeyPreview属性设为True,那么窗体将比其内的控件优先获得键盘事件的激活权。
         this.KeyPreview = true;
		//8.窗体的显示方式,最大化或者最小化..
        this.WindowState = FormWindowState.Minimized; 
1.2 自定义窗体拖动标题栏

注意:代码是写在标题Panel的事件中

    int x;
    int y;
    private void pTitle_MouseDown(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
    }
    private void pTitle_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            //计算方式:当前位置加上移动距离
            this.Location = new Point(this.Location.X + (e.X - x), this.Location.Y + (e.Y - y));
        }
    }
1.3 是否退出系统提示
   private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
   {
       if (MessageBox.Show("Are you sure to exit the system?","Prompt", MessageBoxButtons.YesNo)== DialogResult.No)
       {
           //Cancel表示是否取消当前事件,当点击No时则取消
           e.Cancel = true; 
       }
   }

2.TextBox

2.1 自动补充
    string[] data = { "1", "123", "111133", "123", "1222", "12334", "2222" };
    txt.AutoCompleteSource = AutoCompleteSource.CustomSource; //设置数据源类型
    txt.AutoCompleteCustomSource.AddRange(data); //添加数据源
    txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //自动完成类型

3.

4.CheckedListBox

4.1 常用属性
   // 1.点击文本后选中
   this.clb.CheckOnClick = true; 
4.2 全选
      if (cbAll.Checked)
      {
          for (int i = 0; i < clb.Items.Count; i++)
              clb.SetItemChecked(i,true);
      }
      else
      {
          for (int i = 0; i < clb.Items.Count; i++)
              clb.SetItemChecked(i, false);
      }
4.3 反选
    for (int i = 0; i < clb.Items.Count; i++)
        clb.SetItemChecked(i, !clb.GetItemChecked(i));

4.4获取选中文本/值

  string str1 = ""; //选中文本
  string str2 = ""; //选中值
  //for (int i = 0; i < clb.Items.Count; i++) //获取选中文本
  //{
  //    if (clb.GetItemChecked(i))
  //        str += clb.GetItemText(clb.Items[i]) + ";";
  //}
  foreach (DataRowView item in clb.CheckedItems)
  {
      // item索引取决于数据源字段索引
      str1 += item[1].ToString() + ";"; //获取选中文本
      str2 += item[0].ToString() + ";"; //获取选中值
  }

5.RichTextBox

5.1 常用
// 1.加载rtf文件
this.richTextBox1.LoadFile(help_file); 
// 2.内容装换为byt数字,可以存储在数据库当中
byte[] bytes = Encoding.Default.GetBytes(richTextBox1.Rtf); // 注意是 .Rtf
5.2评论实现,插入表情,表情图片的点击事件
 private void pictureBox1_Click(object sender, EventArgs e)
 {
     Clipboard.Clear();         
     Clipboard.SetImage(Properties.Resources.emoticon_smile);
     richTextBox1.Paste();
 }
5.4 数据库保存及读取
// 1. 存储,数据库中也可以直接存储字符串使用 varchar(Max)
string str=richTextBox1.Rtf; 
// 2. 读取,直接读取字符串
richTextBox1.LoadFile(str,RichTextBoxStreamType.RichText);

6.DateTimePicker

6.1 常用属性
    // 1.自定义显示样式
    this.dtp.Format = DateTimePickerFormat.Custom;
    this.dtp.CustomFormat = "yyyy-MM-dd HH:mm:ss";
    // 2.是够显示上下修改按键
	this.dtp.ShowUpDown=true;

6.1.1 各个字母所代表的意思

1.MM:月份 2.mm:分钟 3. MMMM:文字形式月份 4.MMM:三个字母缩写的月份

4.HH:24小时制 5.hh:12小时制

6.ddd:三个字母缩写的星期 7.dddd:完整的星期

8.t: 单字母 A.M./P.M. 缩写(A.M. 将显示为“A”) 9.tt:两字母 A.M./P.M. 缩写(A.M. 将显示为“AM”)

7.ContextMenuStrip-右键菜单

7.1 添加Item
this.contextMenuStrip1.Items.Add( "Item1", Properties.Resources.nopicture); //带Icon
this.contextMenuStrip1.Items.Add( "Item2"); //不带Icon
7.2 Item点击事件 ,以及设置为选中状态,在ItemClicked点击事件中实现
  private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  {
      foreach (ToolStripMenuItem item in this.contextMenuStrip1.Items) //全部置为未选中状态
          item.Checked = false;

      ToolStripMenuItem selectedItem = (ToolStripMenuItem)e.ClickedItem;
      selectedItem.Checked = true; //设置选中项为选中状态
      if (selectedItem == this.contextMenuStrip1.Items[0])
          MessageBox.Show("Item1");
      else if(selectedItem == this.contextMenuStrip1.Items[1])
          MessageBox.Show("Item2");
  }
7.3 DataGridView 右键点击提示选中Ids

注意是CellMouseDown 事件

  private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  {
      if(e.Button== MouseButtons.Right)
      {
          selectedId = this.dgv.Rows[e.RowIndex].Cells["ids"].Value.ToString();
          this.contextMenuStrip1.Show(MousePosition.X, MousePosition.Y); //设置显示位置
      }
  }

8.Dialog

8.1 各种Dialog

1.ColorDialog 2.FontDialog 3.FolderBrowserDialog 4.OpenFileDialog 5.SaveFileDialog

8.2 FootDialog ColorDialog
  if (fontDialog1/colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
      this.label1.Font = fontDialog1.Font; // FootDialog 
      this.panel1.BackColor = colorDialog1.Color; //ColorDialog 
  }
8.3 常用 属性
    // 1.文件名筛选器
    ofd.Filter = "ALL|*.*|PNG|*.png|JGP|*.jpg";
    // 2.是否支持多选
    ofd.Multiselect = true;
    // 3.是否还原上次打开目录
    ofd.RestoreDirectory = false;

9.MenuStrip

9.1动态添加MenuItem

需要记住的属性:DropDownItems ToolStripMenuItem

   ToolStripMenuItem item = new ToolStripMenuItem("Item1");
   ToolStripMenuItem item_sub1 = new ToolStripMenuItem("Item1 sub1");
   ToolStripMenuItem item_sub2 = new ToolStripMenuItem("Item1 su21");
   item.DropDownItems.Add(item_sub1);
   item.DropDownItems.Add(item_sub2);

   item_sub1.DropDownItems.Add("Item 1 sub1 sub1");

   this.menuStrip1.Items.Add(item);
9.2 点击事件

1.通过双击MenuItem来设置单个Item 2.foreach循环 3.动态添加Item时,每个Item绑定事件

   foreach (ToolStripMenuItem menuItem in this.menuStrip1.Items)
   {
       menuItem.Click += MenuItem_Click; //显示在界面中的菜单
       foreach (ToolStripMenuItem dropItem in menuItem.DropDownItems)
       {
           dropItem.Click += MenuItem_Click; //一级菜单
       }
   }

10.NumbericUpDown

   // 1.步长
   this.numericUpDown1.Increment = Convert.ToDecimal(0.50); //可以设置或者小数
   this.numericUpDown1.DecimalPlaces = 2;//小数位个数,如果步长小于1则需要设置
   // 2.最大值,最小值
   this.numericUpDown1.Minimum = 1;
   this.numericUpDown1.Maximum = 100;            

11.TrackBar-滑动条

11.1 常用属性
  // 1.显示的刻度间隔
  this.trackBar1.TickFrequency = 5;
  // 2.显示样式
  this.trackBar1.TickStyle = TickStyle.BottomRight;
  // 3.最大值/最小值
  this.trackBar1.Minimum = 1;
  this.trackBar1.Maximum = 10;

12.Panel

12.1 属性
   // 1.边框显示样式
   this.panelContainer.BorderStyle = BorderStyle.FixedSingle;
   // 2.背景图显示样式
   this.panelContainer.BackgroundImageLayout = ImageLayout.Stretch;
12.2 设置表框样式及颜色
   private void Panel1_Paint(object sender, PainEventArgs e)
   {  	  ControlPaint.DrawBorder(e.Graphics,this.ClientRectangele,Color.Red,ButtonBorderStyle.Solid);  
   }

13.LinkLable

   // 1.显示样式
   this.linkLabel1.LinkBeha=LinkBehavior.NeverUnderline; //不显示下划线

14.TableLayoutPanel

14.1 单元格内控件居中对齐

设置单元格中控件的Anchor属性为None

14.2 常用属性
   // 1.行数/列数
   this.tableLayoutPanel1.ColumnCount = 3;
   this.tableLayoutPanel1.RowCount = 3;
   // 2.单元格样式
   this.tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;
   // 3.添加控件
   this.tableLayoutPanel1.Controls.Add(btn,0, 0);

15.ListView

15.1 常用属性
  // 1.显示样式
  this.listView1.View = View.SmallIcon;
15.2 加载数据
  ImageList ilLarge = new ImageList();
  ImageList ilSmall = new ImageList();
  ilLarge.ImageSize = new Size(128,128);
  ilSmall.ImageSize = new Size(32, 32);
  ilLarge.Images.Add(Properties.Resources.superman);
  ilSmall.Images.Add(Properties.Resources.superman);
  // 设置大小Icon
  this.listView1.LargeImageList = ilLarge;
  this.listView1.SmallImageList = ilSmall;

  //添加列
  this.listView1.Columns.Clear();
  this.listView1.Columns.Add("Id");
  this.listView1.Columns.Add("Name");
  this.listView1.Columns.Add("Age");

  Random r = new Random();
  this.listView1.Items.Clear();
  for (int i = 0; i < 10; i++)
  {
      ListViewItem lvi = new ListViewItem();
      lvi.Text = i.ToString();
      lvi.SubItems.Add("Name" + i.ToString());
      lvi.SubItems.Add(r.Next(10, 20).ToString());
      lvi.ImageIndex = 0; //重要,不设置的话不显示图片
      this.listView1.Items.Add(lvi);
  }

15.3 获取选中项

   string str = "";
   foreach (ListViewItem item in this.listView1.SelectedItems)
       str += item.Text + ";";

16.FlowLayoutPanel

16.1 设置滚动方向
  //首先设置允许滚动
  this.flp.AutoScroll = true;
  //this.flp.WrapContents = true; //垂直滚动条
  this.flp.WrapContents = false; //水平滚动条
  

16.2 设置滚动条显示位置

flp.ScrollControlIntoView(uc); //指定显示控件

======================其他知识点

1.BindingSource

BindingSource bs = new BindingSource();
bs.DataSource = dataSet.Tables[0]; //绑定数据源
this.dataGridView1.DataSource = bs; //为dataGridView设置数据源
this.txtDepid.DataBindings.Add("Text", bs, "depid"); //绑定TextBox的Text属性

2. toString() 方法

   int num = 123123;
   double d=12.126;
   double d2=0.5;
   // 1.千分位分割符,0表示小数点后几位    
   Console.WriteLine(num.ToString("N0")); // 123,123
   // 2.货币格式,默认千分位分隔符
   Console.WriteLine(num.ToString("C2")); // $123123.00
   // 3.四舍五入,保留指定小数位数
   string str= d.ToString("F2");  // str:1.13  
   string str = d.ToString("0.00");  //效果同上
   // 4.计算百分比
   string str = d2.ToString("P0"); // 50%
   string str = d2.ToString("P"); // 50.00%

3.String 类常用

  // 1.构造函数生成重复字母
  string str = new string('Q',3); // str:QQQ
  // 2.合并多个字符串
  string[] strs = { "A", "B", "C" };
  string str= string.Concat(strs);
  // 3.去掉字符串最后指定符号
  StringBuilder sb = new StringBuilder();
  sb.Append("a,b,c,d,");
  string str = sb.ToString().TrimEnd(',');
  //4.拼接指定项到指定长度
  string str2= "C"+num.ToString().PadLeft(6, '0');

4.Clipboard-剪切板

  // 1.设置剪切板内容
  //Clipboard.SetText("Tiger");
  // 2.获取剪切板内容
  string str = Clipboard.GetText(); //Type1
  Console.WriteLine(str);
  IDataObject iData = Clipboard.GetDataObject(); //Type2
  if (iData.GetDataPresent(DataFormats.Bitmap)) //也可以获取图片,不过目前只发现PrtSc键可用
  {
      Bitmap b = (Bitmap)iData.GetData(DataFormats.Bitmap);
  }

posted @ 2017-10-31 20:25  -Tiger  阅读(1215)  评论(0编辑  收藏  举报