天天看點

第三部分 Windows控件應用

1.窗體顯示和隐藏

顯示:在Form1窗體中調用Form2窗體如下

Form2 frm2=new Form2();

frm2.Show();

隐藏:接上,frm2顯示後隐藏Form1窗體:this.Hide();

2.窗體的事件:

單擊窗體的Click事件   public event EventHandler Click; 

加載窗體的Load事件    public event EventHandler Load;

關閉時的FormClosing事件 public event FormClosingEventHandler FormClosing;

private void Form1_Load(object sender, EventArgs e)

{

if (MessageBox.Show("是否檢視窗體! ","",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.OK)

{

}

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

DialogResult dr = MessageBox.Show("是否關閉窗體", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

if (dr==DialogResult.Yes)

{

e.Cancel=false;

}

else

{

e.Cancel=true;

}

}

3.MDI子窗體排列方式調整通過下列函數實作:

public void LayoutMdi(MdiLayout value);  MdiLayout有三個枚舉成員:TileHorizontal,TileVertical,Cascade.

4.繼承窗體指的是開發一個和已有視窗一樣的視窗。開發有兩種方法:程式設計方式或使用繼承選擇器。

如新增一個Form2視窗要和Form1有一樣的樣式,則這樣寫:

   public partial class Form3 : Test01.Form1

    {

    }

用繼承選擇器指的是新增視窗時選擇“繼承的窗體”,然後自己選擇。

5.要在程式運作時使視窗漸漸顯式的方法如下。

首先,添加一個Timer控件,該控件按預設的屬性設定,然後給Timer控件添加一個事件Tick,然後在該事件中添加如下代碼:

private void timer1_Tick(object sender, System.EventArgs e)

{

if (this.Opacity < 1)

{

this.Opacity = this.Opacity + 0.05;//随時間增加Opacity的值,每次增加0.05

}

else

{

this.timer1.Enabled = false;

}

}

然後在Form的Load事件中添加如下代碼即可,超級輕易。

private void Form1_Load(object sender, System.EventArgs e)

{

this.timer1.Enabled = true;//使時間控件生效

this.Opacity = 0;//設定初始透明度 Opacity:0-1.0, 0為全透明,1.0為不透明

}

6.文本類控件包括Label,Button,TextBox,RichTextBox控件。

Label控件文本顯式用Text,Visible設定顯式/隐藏。

Button控件可以通過設定AcceptButton和CancelButton屬性來将按鈕作為程式的預設“接受”“取消”按鈕。

TextBox控件隻讀屬性為ReadOnly,建立密碼文本框方法有UseSystemPasswordChar和PasswordChar,Multiline多行資料輸入,TextChanged事件響應文本框改變。

RichTextBox控件Multiline顯式多行,ScrollBars顯示滾動條樣式,可以設定字型屬性,段落格式,另外網址會自動變為超連結,如果要實作手點進去網頁,需要添加下面代碼:

private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)

{

System.Diagnostics.Process.Start(e.LinkText);

}

7.選擇類控件包括ComboBox,CheckBox,RadioButton,NumericUpDown,ListBox.

ComboBox的DropDownStyle改變風格,DropDownList不能編輯控件文本框部分,DropDown可以。SelectAll方法可以全部選中文本框部分。選項改變事件為SelectedValueChanged。

CheckBox的CheckState屬性表明是否被選中,有Checked和Unchecked兩種。事件:Click,CheckStateChanged.

RadioButton的CheckState屬性表明是否被選中,有true和false兩種。事件:Click,CheckedChanged.

NumericUpDown的Maximum和Minimum決定上下限,事件ValueChanged.

ListBox的添加listBox1.Items.Add(textBox1.Text);删除項listBox1.Items.Remove(listBox1.SelectedItem);修改SelectionMode可以選擇多項。

8.分組類控件包括Panel,GroupBox,TabControl.

Panel的Show和Hide方法用來顯示和隐藏容器。通常容器和裡面的控件是一體的。

GroupBox總是顯示邊框,可以顯示标題,但是沒有滾動條。标題為Text屬性。

TabControl的Appearance屬性可将頁籤外觀改變。頁籤的外觀圖示可以通過添加一個ImageList控件來改變,添加控件和導入相關圖像後,代碼如下:

tabControl1.ImageList = imageList1;

tabPage1.ImageIndex = 0;

tabPage1.Text = "頁籤1";

tabPage2.ImageIndex = 1;

tabPage2.Text = "頁籤2";

9.ImageList控件常與pictureBox控件一起連用。常用代碼:imageList1.Images[0];

10.ListView控件:

view屬性改變顯示方式:Details,LargeIcon,List,SmallIcon,Title.

添加項: listView1.Items.Add(textBox1.Text.Trim());

移除項: listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);    listView1.SelectedItems.Clear();

清空項: listView1.Items.Clear();

選擇中間某項:listView1.Items[2].Selected=true;

要添加圖示,則必須與ImageList控件連用。Details,List,SmallIcon顯示SmallImageList屬性中指定的圖像,LargeIcon顯示LargeImageList中的。還可以在大小圖示旁顯示StateImageList中的一組附加圖示。如以下代碼:

private void Form1_Load(object sender, EventArgs e)

{

listView1.LargeImageList = imageList1;

imageList1.Images.Add(Image.FromFile("01.png"));

imageList1.Images.Add(Image.FromFile("02.png"));

listView1.SmallImageList = imageList1;

listView1.Items.Add("用一生下載下傳你");

listView1.Items.Add("芸烨湘楓");

listView1.Items[0].ImageIndex = 0;

listView1.Items[1].ImageIndex = 1;

}

為裡面的項分組:Add方法添加組,RemoveAt和Clear方法移除組。下面是一個分組的示例:

private void Form1_Load(object sender, EventArgs e)

{

listView1.View = View.SmallIcon;

listView1.Groups.Add(new ListViewGroup("名稱",HorizontalAlignment.Left));

listView1.Groups.Add(new ListViewGroup("年齡", HorizontalAlignment.Left));

listView1.Items.Add("用一生下載下傳你");

listView1.Items.Add("芸烨湘楓");

listView1.Items.Add("一生所愛");

listView1.Items.Add("28");

listView1.Items.Add("27");

listView1.Items.Add("26");

listView1.Items[0].Group = listView1.Groups[0];

listView1.Items[1].Group = listView1.Groups[0];

listView1.Items[2].Group = listView1.Groups[0];

listView1.Items[3].Group = listView1.Groups[1];

listView1.Items[4].Group = listView1.Groups[1];

listView1.Items[5].Group = listView1.Groups[1];

}

11.TreeView控件

添加和删除節點以及為節點設定圖示。節點的ImageIndex屬性表示正常狀态下的節點顯示圖像,SelectedImageIndex屬性表示确定標明狀态下的節點顯示的圖像:

private void Form1_Load(object sender, EventArgs e)

{

TreeNode tn1 = treeView1.Nodes.Add("1");

TreeNode tn2 = treeView1.Nodes.Add("2");

TreeNode tn3 = treeView1.Nodes.Add("3");

TreeNode Ntn1 = new TreeNode("11");

TreeNode Ntn2 = new TreeNode("12");

TreeNode Ntn3 = new TreeNode("13");

tn1.Nodes.Add(Ntn1);

tn1.Nodes.Add(Ntn2);

tn1.Nodes.Add(Ntn3);

TreeNode Stn1 = new TreeNode("111");

TreeNode Stn2 = new TreeNode("112");

TreeNode Stn3 = new TreeNode("113");

Ntn1.Nodes.Add(Stn1);

Ntn1.Nodes.Add(Stn2);

Ntn1.Nodes.Add(Stn3);

//設定圖示

imageList1.Images.Add(Image.FromFile("1.png"));

imageList1.Images.Add(Image.FromFile("2.png"));

treeView1.ImageList = imageList1;

imageList1.ImageSize = new Size(16,16);

treeView1.ImageIndex = 0;

treeView1.SelectedImageIndex = 1;

}

private void button1_Click(object sender, EventArgs e)

{

this.treeView1.Nodes.Remove(treeView1.SelectedNode);

}

12.DateTimePicker控件

Format屬性有4種。隻有當為Custom時才可以自己定義。即:

dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";

獲得控件的值用value屬性:

textBox1.Text = dateTimePicker1.Text;

textBox2.Text = dateTimePicker1.Value.Year.ToString();

textBox3.Text = dateTimePicker1.Value.Month.ToString();

textBox4.Text = dateTimePicker1.Value.Day.ToString();

13.MonthCalendar控件

TitleBackColor,TitleForeColor,TrailingForeColor等用來修改配色方案,ShowWeekNumbers用來顯示周數,CalendarDimensions用來顯示多個月份

以粗體顯示特定日期方法:

DateTime myVacation1 = new DateTime(2010, 6, 26);

monthCalendar1.AddBoldedDate(myVacation1);

monthCalendar1.UpdateBoldedDates();

選擇日期範圍,必須擷取SelectionStart和SelectionEnd屬性的值:

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)

{

textBox1.Text = monthCalendar1.TodayDate.ToString();

textBox2.Text = monthCalendar1.SelectionStart.ToString();

textBox3.Text = monthCalendar1.SelectionEnd.ToString();

}

14.ErrorProvider控件用來驗證文本框輸入,通過SetError方法。如:

 //輸入不能為空

private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)

{

if (textBox1.Text == "")

{

errorProvider1.SetError(textBox1, "不能為空");

}

else

{

errorProvider1.SetError(textBox1,"");

}

 //輸入不能為空,且必須輸入一個數字

private void textBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)

{

if (textBox2.Text == "")

{

errorProvider2.SetError(textBox2, "不能為空");

}

else

{

try

{

int x = Int32.Parse(textBox2.Text);

errorProvider2.SetError(textBox2,"");

}

catch

{

errorProvider2.SetError(textBox2, "請輸入一個數");

}

}

}

15.Timer控件設定時間間隔,并用Tick事件來響應。

private void timer1_Tick(object sender, EventArgs e)

{

textBox1.Text = DateTime.Now.ToString();

}

private void button1_Click(object sender, EventArgs e)

{

timer1.Interval = 1000;

if (button1.Text == "開始")

{

timer1.Enabled = true;

button1.Text ="停止";

}

else

{

timer1.Enabled = false;

button1.Text = "開始";

}

}

16.ProgressBar控件顯示進度條。

Minimum和Maximum表示最小最大值,Value表示已完成的進度,Step表示Value遞增的值。

private void button1_Click(object sender, EventArgs e)

{

button1.Enabled = false;

progressBar1.Minimum = 0;

progressBar1.Maximum = 50000;

progressBar1.Step = 1;

for (int i = 0; i <50000; i++)

{

progressBar1.PerformStep();

}

}

繼續閱讀