C#UI控制元件使用總結
阿新 • • 發佈:2019-02-13
1、ImageList
ImageList imageList = new ImageList(); for (int i = 0; i < 10; i++) { if(i%2 == 0) imageList.Images.Add(Image.FromFile(@"img\add.png")); else imageList.Images.Add(Image.FromFile(@"img\com.png")); }
2、ListView
<1>在屬性欄新增選中事件和雙擊事件
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
在屬性欄雙擊這些事件,他自動新增程式碼private void listView1_SelectedIndexChanged(object sender, EventArgs e) { Debug.WriteLine("選中狀態"); } private void listView1_DoubleClick(object sender, EventArgs e) { ListView.SelectedIndexCollection c = listView1.SelectedIndices; Debug.WriteLine("當前選擇index="+c[0]); }
<2>新增內容
// 初始化imageList ImageList imageList = new ImageList(); for (int i = 0; i < 10; i++) { if(i%2 == 0) imageList.Images.Add(Image.FromFile(@"img\add.png")); else imageList.Images.Add(Image.FromFile(@"img\com.png")); } // 在這裡初始化控制元件 this.listView1.View = View.List; this.listView1.Scrollable = true; this.listView1.Height = 300; this.listView1.SmallImageList = imageList; this.listView1.BeginUpdate(); for (int i = 0; i < 10; i++) { ListViewItem item = new ListViewItem(); item.ImageIndex = i; item.Text = "item" + i; this.listView1.Items.Add(item); } this.listView1.EndUpdate();
<3>滾動條
listView只有水平滾動條,沒有垂直滾動條,下面這個方法可以新增垂直滾動條,但是還是不夠完美的,當拖動滾動條的時候,會出現空白,還不知道如何解決
在類定義的下面新增:
private const int SB_HORZ = 0; private const int SB_VERT = 1; //[System.Runtime.InteropServices.DllImport("user32.dll")] [DllImport("user32.dll")]//如果這樣寫要新增using System.Runtime,InteropServices public static extern int ShowScrollBar(IntPtr hWnd, int iBar, int bShow);在初始化listView的地方新增程式碼:
this.listView1.Scrollable = false; ShowScrollBar(this.listView1.Handle, SB_VERT, 1);
就可以了
3、DataGridView表格展示
為表格提供DataTable資料來源
DataTable dt = new DataTable(); dt.Columns.Add("姓名", System.Type.GetType("System.String")); dt.Columns.Add("年齡", System.Type.GetType("System.Int32")); for (int i = 0; i < 20; i++) { DataRow dr = dt.NewRow(); dr[0] = "小明"+i; dr[1] = 20+i; dt.Rows.Add(dr); } dataGridView1.DataSource = dt;
表格有幾個重要的回撥:
CellPainting:用於合併單元格自己畫線畫圖
CellFormatting:定義單元格的樣式(背景顏色等)
RowPostPaint:設定行標頭
這些回撥要在UI設計的屬性裡直接新增:
// 在此設定樣式+顯示行標頭
// 此函式會逐行(Row)執行
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow CurrentRow = this.dataGridView1.Rows[e.RowIndex];
Debug.WriteLine("rowIndex="+e.RowIndex);
CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex+1);// 顯示行標頭
if (e.RowIndex == 1 && this.dataGridView1.Columns[e.ColumnIndex].Name == "年齡")
{
e.CellStyle = this.dataGridViewCellStyle1;
}
}
// 在行標頭裡面繪圖
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Image RowIcon1 = TestUI.Properties.Resources.add;
Image RowIcon2 = TestUI.Properties.Resources.com;
e.Graphics.DrawImage(RowIcon1, e.RowBounds.Left + this.dataGridView1.RowHeadersWidth - 20, e.RowBounds.Top + 4, 20, 20);// 這行即把圖片繪製在第一格上面
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow CurrentRow = this.dataGridView1.Rows[e.RowIndex];
//Debug.WriteLine("rowIndex="+e.RowIndex);
CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex+1);// 顯示行標頭
// 根據index設定列樣式
this.dataGridView1.Columns[0].ReadOnly = true;
this.dataGridView1.Columns[0].DefaultCellStyle = this.dataGridViewCellStyle1;
// 根據列名設定列樣式
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "年齡")
{
this.dataGridView1.Columns[e.ColumnIndex].ReadOnly = true;
this.dataGridView1.Columns[e.ColumnIndex].DefaultCellStyle = this.dataGridViewCellStyle1;
}
// 設定行樣式
this.dataGridView1.Rows[1].ReadOnly = true;
this.dataGridView1.Rows[1].DefaultCellStyle = this.dataGridViewCellStyle1;
}
4、MDI的使用
MDI是多頁面視窗,可以建立父視窗,子視窗,然後多視窗切換,下面是建立過程
<1>新建winfrom程式(這一步跟普通winform的建立一樣)
<2>新建項->windows窗體:這是建立子窗體
<3>新建項->MDI父窗體:這是建立父窗體
<4>新增從父窗體跳到子窗體:
選單建立完畢之後,雙擊,進入回撥,在回撥裡把子窗體的初始化程式碼填進去:
進入程式碼編輯狀態:
private void child1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Child1 childForm = new Child1();
childForm.MdiParent = this;
childForm.Show();
}
<5>最後不要忘了修改winform的預設啟動視窗,在Program.cs:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
Application.Run(new MDIParent1());
}
5、TabControl
tabControl只不過是通過切換tab,將不同的tabPage上面的元素顯示或隱藏,實現切換頁面的效果。一起的元素都還是在form裡面。
事件:當tabControl的index發生變化的回撥。如果所有組建都在winform-load的時候初始化完成,一是沒有必要,二是沒有及時重新整理。有的組建需要在我們點選那個tab的時候,展示的時候及時重新整理一下,那麼就需要在檢測到點選的時候重新整理一下。 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine("selcted-index=" + tabControl1.SelectedIndex+" selected-tabPage="+tabControl1.SelectedTab.Name);
}
6、ComboBox下拉框
<1>下拉框一般不要讓使用者輸入文字,要進行設定:
<2>繫結回撥:
<3>初始化
private void InitComboBox()
{
this.comboBox1.Items.Add("全部");
this.comboBox1.Items.Add("新增");
this.comboBox1.Items.Add("修改");
this.comboBox1.Items.Add("駁回");
this.comboBox1.Items.Add("完成");
this.comboBox1.SelectedItem = "全部";
}
<4>回撥
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine("選擇=" + this.comboBox1.SelectedItem);
}
7、TextBox
TextBox是輸入框,輸入框很重要的一個是輸入提示。
這樣做:新增得到焦點和失去焦點的回撥
然後在回撥里根據情況設定顯示文字或者清空:
private void textBox_assBillNo_Enter(object sender, EventArgs e)
{
if (textBox_assBillNo.Text == "請輸入分運單號")
textBox_assBillNo.Text = "";
}
private void textBox_assBillNo_Leave(object sender, EventArgs e)
{
if (textBox_assBillNo.Text == "")
textBox_assBillNo.Text = "請輸入分運單號";
}
如果是多行的TextBox,用AppendText新增字串他會自動滾動到最下面
8、MessageBox
彈窗訊息。此彈窗不僅可以展示訊息,還可以建立選項供使用者選擇,而且彈窗是阻塞的,讓使用者選擇完了程式才會進行下一步,非常適合流程中的使用者選項配置。
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("此總運單已經提交報文,是否再次提交?", "Excel重複載入提醒", messButton);
if (dr == DialogResult.OK)
return true;
else
return false;