<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Mono Winforms 中的基本控件 > 原文: [http://zetcode.com/gui/csharpwinforms/controls/](http://zetcode.com/gui/csharpwinforms/controls/) Mono Winforms 編程教程的這一部分將介紹基本控件。 Winforms 控件是應用的基本構建塊。 Winforms 具有各種各樣的控件。按鈕,復選框,滑塊,列表框等。程序員完成工作所需的一切。 在本教程的這一部分中,我們將描述幾個有用的控件。 ## `Label`控件 `Label`是用于顯示文本或圖像的簡單控件。 它沒有得到關注。 `label.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { string text = @"Sometimes I feel I've got to Run away I've got to Get away From the pain that you drive into the heart of me The love we share Seems to go nowhere I've lost my lights I toss and turn I can't sleep at night Once I ran to you (I ran) Now I'll run from you This tainted love you've given I give you all a boy could give you Take my tears and that's not nearly all Tainted love Tainted love"; public MForm() { Text = "Tainted Love"; Font font = new Font("Serif", 10); Label lyrics = new Label(); lyrics.Parent = this; lyrics.Text = text; lyrics.Font = font; lyrics.Location = new Point(10, 10); lyrics.Size = new Size (290, 290); CenterToScreen(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 在我們的示例中,我們顯示了 Tainted Love 歌曲的歌詞。 ```cs Label lyrics = new Label(); ``` `Label`控件已創建。 ```cs string text = @"Sometimes I feel I've got ... ``` @字符用于表示多行字符串。 ```cs Font font = new Font("Serif", 10); ... lyrics.Font = font; ``` 標簽文本的字體設置為 Serif,10px。 ![Label](https://img.kancloud.cn/d7/d3/d7d3748af2f5701b634a86bc1e375a4c_300x301.jpg) 圖:`Label` ## `CheckBox` `CheckBox`是具有兩個狀態的控件:開和關。 它是帶有標簽或圖像的盒子。 如果選中了`CheckBox`,則在方框中用勾號表示。 `CheckBox`可用于在啟動時顯示/隱藏啟動屏幕,切換工具欄的可見性等。 `checkbox.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private CheckBox cb; public MForm() { Text = "CheckBox"; Size = new Size(220, 170); cb = new CheckBox(); cb.Parent = this; cb.Location = new Point(30, 30); cb.Text = "Show Title"; cb.Checked = true; cb.CheckedChanged += new EventHandler(OnChanged); CenterToScreen(); } void OnChanged(object sender, EventArgs e) { if (cb.Checked) { Text = "CheckBox"; } else { Text = ""; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們的代碼示例根據窗口的狀態顯示或隱藏窗口的標題。 ```cs cb = new CheckBox(); ``` `CheckBox`控件已創建。 ```cs cb.Text = "Show Title"; cb.Checked = true; ``` 當應用啟動時,我們顯示標題。 然后將`CheckBox`控件設置為選中狀態。 ```cs cb.CheckedChanged += new EventHandler(OnChanged); ``` 當我們單擊`CheckBox`控件時,將觸發`CheckedChanged`事件。 ```cs if (cb.Checked) { Text = "CheckBox"; } else { Text = ""; } ``` 在這里,我們切換窗口的標題。 ![CheckBox](https://img.kancloud.cn/9c/03/9c03fff56b27f629944c297bbd1a33ed_220x171.jpg) 圖:`CheckBox` ## `TrackBar` `TrackBar`是一個組件,使用戶可以通過在有限的間隔內滑動旋鈕來以圖形方式選擇一個值。 我們的示例將顯示音量控制。 `trackbar.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { PictureBox pb; TrackBar tb; Bitmap mute, min, med, max; public MForm() { Text = "TrackBar"; Size = new Size(260, 190); tb = new TrackBar(); tb.Parent = this; tb.Size = new Size(160, 30); tb.Location = new Point(20, 40); tb.TickStyle = TickStyle.None; tb.ValueChanged += new EventHandler(OnChanged); LoadImages(); pb = new PictureBox(); pb.Parent = this; pb.Location = new Point(210, 50); pb.Image = mute; CenterToScreen(); } void LoadImages() { mute = new Bitmap("mute.png"); min = new Bitmap("min.png"); med = new Bitmap("med.png"); max = new Bitmap("max.png"); } void OnChanged(object sender, EventArgs e) { int val = tb.Value; if (val == 0) { pb.Image = mute; } else if (val > 0 && val <= 3) { pb.Image = min; } else if (val > 3 && val < 8) { pb.Image = med; } else { pb.Image = max; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 在代碼示例中,我們顯示了`TrackBar`和`PictureBox`。 通過拖動軌跡欄,我們可以在`PictureBox`控件上更改圖像。 ```cs tb = new TrackBar(); ``` `TrackBar`控件已創建。 ```cs tb.TickStyle = TickStyle.None; ``` 我們對此`TrackBar`沒有顯示任何報價。 ```cs pb = new PictureBox(); ... pb.Image = mute; ``` `PictureBox`控件已創建。 它用于顯示圖像。 開始時,它會顯示靜音圖像。 ```cs void LoadImages() { mute = new Bitmap("mute.png"); min = new Bitmap("min.png"); med = new Bitmap("med.png"); max = new Bitmap("max.png"); } ``` 在這里,我們將加載四個將要使用的圖像。 ```cs int val = tb.Value; if (val == 0) { pb.Image = mute; } else if (val > 0 && val <= 3) { pb.Image = min; } else if (val > 3 && val < 8) { pb.Image = med; } else { pb.Image = max; } ``` 我們確定`TrackBar`的值。 根據其值,我們更新`PictureBox`控件。 ![TrackBar](https://img.kancloud.cn/9a/c8/9ac8d88ed2415cfe24d2d90453bcfa6f_260x191.jpg) 圖:`TrackBar` ## `ComboBox` `ComboBox`是一個組合了按鈕或可編輯字段和下拉列表的控件。 用戶可以從下拉列表中選擇一個值,該列表應用戶的要求出現。 如果使組合框可編輯,則組合框將包含一個可編輯字段,用戶可以在其中輸入值。 `combobox.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private ComboBox cb; private Label label; public MForm() { Text = "ComboBox"; Size = new Size(240, 240); cb = new ComboBox(); cb.Parent = this; cb.Location = new Point(50, 30); cb.Items.AddRange(new object[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo"}); cb.SelectionChangeCommitted += new EventHandler(OnChanged); label = new Label(); label.Location = new Point(50, 140); label.Parent = this; label.Text = "..."; CenterToScreen(); } void OnChanged(object sender, EventArgs e) { ComboBox combo = (ComboBox) sender; label.Text = combo.Text; } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們的代碼編程示例顯示了一個包含五個項目的組合框。 所選項目顯示在標簽控件中。 ```cs cb = new ComboBox(); ``` `ComboBox`控件已創建。 ```cs cb.Items.AddRange(new object[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo"}); ``` `ComboBox`控件中充滿了項目。 ```cs cb.SelectionChangeCommitted += new EventHandler(OnChanged); ``` 如果我們從組合框中選擇一個項目,則會觸發`SelectionChangeCommitted`事件。 ```cs void OnChanged(object sender, EventArgs e) { ComboBox combo = (ComboBox) sender; label.Text = combo.Text; } ``` 在這里,將從組合框中選擇的文本復制到標簽。 ![ComboBox](https://img.kancloud.cn/de/c5/dec54b9d7a99aee63e1d0fa9dd974c23_240x241.jpg) 圖:`ComboBox` ## `MonthCalendar` 在下一個示例中,我們將顯示`MonthCalendar`控件。 `MonthCalendar`控件允許用戶使用視覺顯示選擇日期。 `monthcalendar.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private MonthCalendar calendar; private Label date; public MForm() { Text = "Month Calendar"; Size = new Size(240, 240); calendar = new MonthCalendar(); calendar.Parent = this; calendar.Location = new Point(20, 20); calendar.DateSelected += new DateRangeEventHandler(OnSelected); date = new Label(); date.Location = new Point(40, 170); date.Parent = this; DateTime dt = calendar.SelectionStart; date.Text = dt.Month + "/" + dt.Day + "/" + dt.Year; CenterToScreen(); } void OnSelected(object sender, EventArgs e) { DateTime dt = calendar.SelectionStart; date.Text = dt.Month + "/" + dt.Day + "/" + dt.Year; } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 在示例中,我們顯示了`MonthCalendar`和`Label`。 ```cs private MonthCalendar calendar; private Label date; ``` 我們有兩個控件。 一個`MonthCalendar`和一個`Label`。 后者顯示當前選擇的日期。 ```cs void OnSelected(object sender, EventArgs e) { DateTime dt = calendar.SelectionStart; date.Text = dt.Month + "/" + dt.Day + "/" + dt.Year; } ``` 當我們從`MonthCalendar`中選擇一個日期時,就會調用`OnSelected()`方法。 `SelectionStart`屬性獲取所選日期范圍的開始日期。 ![MonthCalendar](https://img.kancloud.cn/62/20/6220e017d0d4d0f18037eb11929def9e_240x241.jpg) 圖:`MonthCalendar` ## `TextBox` `TextBox`控件用于顯示或接受某些文本。 文本可以是單行或多行。 此控件還可以進行密碼屏蔽。 `textbox.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private Label text; public MForm() { Text = "TextBox"; Size = new Size(250, 200); CenterToScreen(); text = new Label(); text.Parent = this; text.Text = "..."; text.Location = new Point(60, 40); text.AutoSize = true; TextBox tbox = new TextBox(); tbox.Parent = this; tbox.Location = new Point(60, 100); tbox.KeyUp += new KeyEventHandler(OnKeyUp); } void OnKeyUp(object sender, KeyEventArgs e) { TextBox tb = (TextBox) sender; this.text.Text = tb.Text; } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 本示例顯示一個文本框和一個標簽。 我們在文本框中鍵入的文本將立即顯示在標簽控件中。 ```cs text = new Label(); ... text.AutoSize = true; ``` `Label`控件已創建。 `AutoSize`屬性可確保`Label`增長以顯示文本。 ```cs TextBox tbox = new TextBox(); ... tbox.KeyUp += new KeyEventHandler(OnKeyUp); ``` 我們插入`KeyUp`事件。 釋放按鍵時,將調用`OnKeyUp()`方法。 ```cs void OnKeyUp(object sender, KeyEventArgs e) { TextBox tb = (TextBox) sender; this.text.Text = tb.Text; } ``` 在`OnKeyUp()`方法中,我們使用文本框控件中的文本更新了標簽控件。 ![TextBox](https://img.kancloud.cn/70/8e/708e25952ac77d838f07c6b8f166e77c_250x201.jpg) 圖:`TextBox` 我們已經完成了 Mono Winforms 教程的這一章,專門介紹基本控件。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看