<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Mono Winforms 中的高級控件 > 原文: [http://zetcode.com/gui/csharpwinforms/advancedcontrols/](http://zetcode.com/gui/csharpwinforms/advancedcontrols/) 在 Mono Winforms 教程的這一部分中,我們介紹一些更高級的控件。 即`ListBox`,`ListView`和`TreeView`控件。 ## `ListBox` `ListBox`控件用于顯示項目列表。 用戶可以通過單擊選擇一個或多個項目。 `listbox.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { private StatusBar sb; public MForm() { Text = "ListBox"; Size = new Size(210, 210); ListBox lb = new ListBox(); lb.Parent = this; lb.Items.Add("Jessica"); lb.Items.Add("Rachel"); lb.Items.Add("Angelina"); lb.Items.Add("Amy"); lb.Items.Add("Jennifer"); lb.Items.Add("Scarlett"); lb.Dock = DockStyle.Fill; lb.SelectedIndexChanged += new EventHandler(OnChanged); sb = new StatusBar(); sb.Parent = this; CenterToScreen(); } void OnChanged(object sender, EventArgs e) { ListBox lb = (ListBox) sender; sb.Text = lb.SelectedItem.ToString(); } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 我們的示例顯示了一個具有六個名稱的列表框。 所選項目顯示在狀態欄中。 ```cs ListBox lb = new ListBox(); lb.Parent = this; ``` `ListBox`控件已創建。 ```cs lb.Items.Add("Jessica"); ``` 這就是我們向`ListBox`控件添加新項目的方式。 該控件具有`Items`屬性。 該屬性是對列表框中項目列表的引用。 使用此引用,我們可以添加,刪除或獲取列表框中的項目數。 ```cs lb.SelectedIndexChanged += new EventHandler(OnChanged); ``` 當我們選擇一個項目時,會觸發`SelectedIndexChanged`事件。 ```cs ListBox lb = (ListBox) sender; sb.Text = lb.SelectedItem.ToString(); ``` 在`OnChange()`方法內部,我們獲得對列表框的引用,并將所選文本設置為狀態欄。 ![ListBox](https://img.kancloud.cn/bd/1d/bd1d17da86df8a6a5f36ba86cb6b79ec_210x211.jpg) 圖:`ListBox` ## `ListView` `ListView`控件用于顯示項目集合。 它是比`ListBox`控件更復雜的控件。 它可以在各種視圖中顯示數據,主要用于在多列視圖中顯示數據。 `listview.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; public class Actress { public string name; public int year; public Actress(string name, int year) { this.name = name; this.year = year; } } class MForm : Form { private StatusBar sb; public MForm() { Text = "ListView"; Size = new Size(350, 300); List<Actress> actresses = new List<Actress>(); actresses.Add(new Actress("Jessica Alba", 1981)); actresses.Add(new Actress("Angelina Jolie", 1975)); actresses.Add(new Actress("Natalie Portman", 1981)); actresses.Add(new Actress("Rachel Weiss", 1971)); actresses.Add(new Actress("Scarlett Johansson", 1984)); ColumnHeader name = new ColumnHeader(); name.Text = "Name"; name.Width = -1; ColumnHeader year = new ColumnHeader(); year.Text = "Year"; SuspendLayout(); ListView lv = new ListView(); lv.Parent = this; lv.FullRowSelect = true; lv.GridLines = true; lv.AllowColumnReorder = true; lv.Sorting = SortOrder.Ascending; lv.Columns.AddRange(new ColumnHeader[] {name, year}); lv.ColumnClick += new ColumnClickEventHandler(ColumnClick); foreach (Actress act in actresses) { ListViewItem item = new ListViewItem(); item.Text = act.name; item.SubItems.Add(act.year.ToString()); lv.Items.Add(item); } lv.Dock = DockStyle.Fill; lv.Click += new EventHandler(OnChanged); sb = new StatusBar(); sb.Parent = this; lv.View = View.Details; ResumeLayout(); CenterToScreen(); } void OnChanged(object sender, EventArgs e) { ListView lv = (ListView) sender; string name = lv.SelectedItems[0].SubItems[0].Text; string born = lv.SelectedItems[0].SubItems[1].Text; sb.Text = name + ", " + born; } void ColumnClick(object sender, ColumnClickEventArgs e) { ListView lv = (ListView) sender; if (lv.Sorting == SortOrder.Ascending) { lv.Sorting = SortOrder.Descending; } else { lv.Sorting = SortOrder.Ascending; } } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 在我們的示例中,我們有一個包含兩列的列表視圖。 在第一列中,我們顯示女演員的名字。 在第二個他們的出生日期。 數據存儲在`List`集合中。 通過選擇一行,一行中的數據將顯示在狀態欄中。 另外,通過單擊列標題,可以對數據進行排序。 ```cs public class Actress { ... } ``` 我們使用`Actress`類存儲數據。 ```cs List<Actress> actresses = new List<Actress>(); actresses.Add(new Actress("Jessica Alba", 1981)); actresses.Add(new Actress("Angelina Jolie", 1975)); ... ``` 我們創建項目并在項目中填充項目。 ```cs ColumnHeader name = new ColumnHeader(); name.Text = "Name"; name.Width = -1; ``` 對于列表視圖中的每一列,我們創建一個`ColumnHeader`。 通過將`Width`設置為-1,列的寬度等于列中最長的項目。 ```cs ListView lv = new ListView(); lv.Parent = this; ``` `ListView`控件已創建。 ```cs lv.FullRowSelect = true; lv.GridLines = true; lv.AllowColumnReorder = true; lv.Sorting = SortOrder.Ascending; ``` 在這里,我們設置控件的四個屬性。 該代碼行支持全行選擇,顯示網格線,通過拖動列對列進行重新排序并以升序對數據進行排序。 ```cs lv.Columns.AddRange(new ColumnHeader[] {name, year}); ``` 在這里,我們將兩個`ColumnHeader`添加到`ListView`控件中。 ```cs foreach (Actress act in actresses) { ListViewItem item = new ListViewItem(); item.Text = act.name; item.SubItems.Add(act.year.ToString()); lv.Items.Add(item); } ``` 此循環填充`ListView`控件。 每行都作為`ListViewItem`類添加到列表視圖。 ```cs lv.View = View.Details; ``` `ListView`控件可以具有不同的視圖。 不同的視圖以不同的方式顯示數據。 ```cs ListView lv = (ListView) sender; string name = lv.SelectedItems[0].SubItems[0].Text; string born = lv.SelectedItems[0].SubItems[1].Text; sb.Text = name + ", " + born; ``` 在`OnChanged()`方法內部,我們從選定的行中獲取數據并將其顯示在狀態欄上。 ```cs if (lv.Sorting == SortOrder.Ascending) { lv.Sorting = SortOrder.Descending; } else { lv.Sorting = SortOrder.Ascending; } ``` 在這里,我們切換列的排序順序。 ![ListView](https://img.kancloud.cn/96/db/96dbeb00ebca34f4628e3dabd1b3b7e0_350x301.jpg) 圖:`ListView` ## `TreeView` `TreeView`控件顯示項目的分層集合。 此控件中的每個項目都由`TreeNode`對象表示。 `treeview.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; class MForm : Form { StatusBar sb; public MForm() { Text = "TreeView"; Size = new Size(250, 250); TreeView tv = new TreeView(); TreeNode root = new TreeNode(); root.Text = "Languages"; TreeNode child1 = new TreeNode(); child1.Text = "Python"; TreeNode child2 = new TreeNode(); child2.Text = "Ruby"; TreeNode child3 = new TreeNode(); child3.Text = "Java"; root.Nodes.AddRange(new TreeNode[] {child1, child2, child3}); tv.Parent = this; tv.Nodes.Add(root); tv.Dock = DockStyle.Fill; tv.AfterSelect += new TreeViewEventHandler(AfterSelect); sb = new StatusBar(); sb.Parent = this; CenterToScreen(); } void AfterSelect(object sender, TreeViewEventArgs e) { sb.Text = e.Node.Text; } } class MApplication { public static void Main() { Application.Run(new MForm()); } } ``` 這是`TreeView`控件的非常簡單的演示。 我們有一個根項目和三個子項。 ```cs TreeView tv = new TreeView(); ``` 我們創建`TreeView`控件。 ```cs TreeNode root = new TreeNode(); root.Text = "Languages"; ... tv.Nodes.Add(root); ``` 在這里,我們創建一個根節點。 ```cs TreeNode child1 = new TreeNode(); child1.Text = "Python"; ``` 子節點以類似的方式創建。 ```cs root.Nodes.AddRange(new TreeNode[] {child1, child2, child3}); ``` 子節點插入到根節點的`Nodes`屬性中。 ![TreeView](https://img.kancloud.cn/53/16/5316c03905d6dcbb83c1d97628e64e32_250x251.jpg) 圖:`TreeView` ## 目錄 下面的代碼示例將更深入地研究`TreeView`控件。 `directories.cs` ```cs using System; using System.Drawing; using System.Windows.Forms; using System.IO; public class MForm : Form { private TreeView tv; private Button expand; private Button expandAll; private Button collapse; private Button collapseAll; private StatusBar sb; private const string HOME_DIR = "/home/vronskij"; public MForm() { Size = new Size(400, 400); Text = "Directories"; tv = new TreeView(); SuspendLayout(); tv.Parent = this; tv.Location = new Point(10,10); tv.Size = new Size(ClientSize.Width - 20, Height - 200); tv.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right ; tv.FullRowSelect = false; tv.ShowLines = true; tv.ShowPlusMinus = true; tv.Scrollable = true; tv.AfterSelect += new TreeViewEventHandler(AfterSelect); expand = new Button(); expand.Parent = this; expand.Location = new Point(20, tv.Bottom + 20); expand.Text = "Expand"; expand.Anchor = AnchorStyles.Left | AnchorStyles.Top; expand.Click += new EventHandler(OnExpand); expandAll = new Button(); expandAll.Parent = this; expandAll.Location = new Point(20, expand.Bottom + 5); expandAll.Text = "Expand All"; expandAll.Anchor = AnchorStyles.Left | AnchorStyles.Top; expandAll.Click += new EventHandler(OnExpandAll); collapse = new Button(); collapse.Parent = this; collapse.Location = new Point(expandAll.Right + 5, expand.Top ); collapse.Text = "Collapse"; collapse.Anchor = AnchorStyles.Left | AnchorStyles.Top; collapse.Click += new EventHandler(OnCollapse); collapseAll = new Button(); collapseAll.Parent = this; collapseAll.Location = new Point(collapse.Left, collapse.Bottom + 5); collapseAll.Text = "Collapse All"; collapseAll.Anchor = AnchorStyles.Left | AnchorStyles.Top; collapseAll.Click += new EventHandler(OnCollapseAll); sb = new StatusBar(); sb.Parent = this; ShowDirectories(tv.Nodes, HOME_DIR); ResumeLayout(); CenterToScreen(); } void AfterSelect(object sender, TreeViewEventArgs e) { sb.Text = e.Node.Text; } void ShowDirectories(TreeNodeCollection trvNode, string path) { DirectoryInfo dirInfo = new DirectoryInfo(path); if (dirInfo != null) { DirectoryInfo[] subDirs = dirInfo.GetDirectories(); TreeNode tr = new TreeNode(dirInfo.Name); if (subDirs.Length > 0) { foreach (DirectoryInfo dr in subDirs) { if (!dr.Name.StartsWith(".")) ShowDirectories(tr.Nodes, dr.FullName); } } trvNode.Add(tr); } } void OnExpand(object sender, EventArgs e) { tv.SelectedNode.Expand(); } void OnExpandAll(object sender, EventArgs e) { tv.ExpandAll(); } void OnCollapse(object sender, EventArgs e) { tv.SelectedNode.Collapse(); } void OnCollapseAll(object sender, EventArgs e) { tv.CollapseAll(); } static void Main() { Application.Run(new MForm()); } } ``` 我們的代碼示例在`TreeView`控件中顯示指定主目錄的目錄。 該應用啟動有些延遲,因為它首先讀取主目錄的目錄結構。 表單上還有四個按鈕。 這些按鈕以編程方式展開和折疊節點。 ```cs tv.Scrollable = true; ``` 我們使`TreeView`控件可滾動,因為該控件顯示了大量目錄。 ```cs ShowDirectories(tv.Nodes, HOME_DIR); ``` `ShowDirectories()`方法使用指定主目錄中可用的目錄填充`TreeView`控件的節點。 ```cs if (subDirs.Length > 0) { ... } ``` 我們檢查是否有任何子目錄。 ```cs foreach (DirectoryInfo dr in subDirs) { if (!dr.Name.StartsWith(".")) ShowDirectories(tr.Nodes, dr.FullName); } ``` 我們遍歷所有目錄。 為此,我們使用了遞歸算法。 我們還跳過隱藏的目錄。 它們以 Unix 系統上的點開頭。 ```cs trvNode.Add(tr); ``` 此代碼行實際上將目錄添加到`TreeView`控件。 ```cs void OnExpand(object sender, EventArgs e) { tv.SelectedNode.Expand(); } ``` 所有四個按鈕都將事件插入到方法中。 這是展開按鈕的方法。 它調用當前所選節點的`Expand()`方法。 ![Directories](https://img.kancloud.cn/1a/ee/1aee8db821c0523e66878ce874f5be18_400x401.jpg) 圖:`Directories` 在 Mono Winforms 教程的這一部分中,我們介紹了 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>

                              哎呀哎呀视频在线观看