程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# winform導航界面..winform 用groupbox,button跟listview

C# winform導航界面..winform 用groupbox,button跟listview

編輯:C#入門知識

winform利用button跟listview實現類似QQ設置中的導航菜單。

主要利用了DockStyle。

具體的代碼如下:
(注意)尤其要注意控件創建的順序。
我的控件的創建順序如下(在.Design.cs中):
 
[csharp]
this .groupBox1 = new System .Windows . Forms. GroupBox (); 
this .button1 = new System .Windows . Forms. Button (); 
this .button2 = new System .Windows . Forms. Button (); 
this .button3 = new System .Windows . Forms. Button (); 
this .button4 = new System .Windows . Forms. Button (); 
this .button5 = new System .Windows . Forms. Button (); 
this .button6 = new System .Windows . Forms. Button (); 
this .listView1 = new System .Windows . Forms. ListView (); 


 

主要代碼如下:
//用listview跟6個button來實現導航菜單的功|能
       
[csharp] 
private void button1_Click( object sender, EventArgs e) 
        { 
            ResetButton();  //reset button 
 
 
            listView1 . Dock = DockStyle. None; 
 
 
            button2 . Dock = DockStyle. Bottom; 
            button3 . Dock = DockStyle. Bottom; 
            button4 . Dock = DockStyle. Bottom; 
            button5 . Dock = DockStyle. Bottom; 
            button6 . Dock = DockStyle. Bottom; 
 
 
            listView1 . BringToFront(); 
            listView1 . Dock = DockStyle. Bottom; 
            listView1 . Clear(); 
        } 
 
 
        private void button2_Click( object sender, EventArgs e) 
        { 
            ResetButton();  //reset button 
            listView1 . Dock = DockStyle. None; 
 
 
            button3 . Dock = DockStyle. Bottom; 
            button4 . Dock = DockStyle. Bottom; 
            button5 . Dock = DockStyle. Bottom; 
            button6 . Dock = DockStyle. Bottom; 
 
 
 
 
            listView1 . BringToFront(); 
            listView1 . Dock = DockStyle. Bottom; 
            listView1 . Clear(); 
 
 
        } 
 
 
        private void button3_Click( object sender, EventArgs e) 
        { 
            ResetButton();  //reset button 
 
 
            listView1 . Dock = DockStyle. None; 
 
 
            button4 . Dock = DockStyle. Bottom; 
            button5 . Dock = DockStyle. Bottom; 
            button6 . Dock = DockStyle. Bottom; 
 
 
 
 
            listView1 . BringToFront(); 
            listView1 . Dock = DockStyle. Bottom; 
            listView1 . Clear(); 
        } 
 
 
        private void button4_Click( object sender, EventArgs e) 
        { 
            ResetButton();  //reset button 
 
 
            listView1 . Dock = DockStyle. None; 
 
 
            button5 . Dock = DockStyle. Bottom; 
            button6 . Dock = DockStyle. Bottom; 
 
 
 
 
            listView1 . BringToFront(); 
            listView1 . Dock = DockStyle. Bottom; 
            listView1 . Clear(); 
        } 
 
 
        private void button5_Click( object sender, EventArgs e) 
        { 
            ResetButton();  //reset button 
 
 
            listView1 . Dock = DockStyle. None; 
 
 
            button6 . Dock = DockStyle. Bottom; 
 
 
 
 
            listView1 . BringToFront(); 
            listView1 . Dock = DockStyle. Bottom; 
            listView1 . Clear(); 
        } 
 
 
        private void button6_Click( object sender, EventArgs e) 
        { 
            ResetButton();  //reset button 
 
 
        } 
 
 
        public void ResetButton()          //初始化時的設置 
        { 
 
 
            button1 . Dock = DockStyle. Bottom; 
            button2 . Dock = DockStyle. Bottom; 
            button3 . Dock = DockStyle. Bottom; 
            button4 . Dock = DockStyle. Bottom; 
            button5 . Dock = DockStyle. Bottom; 
            button6 . Dock = DockStyle. Bottom; 
            listView1 . Dock = DockStyle. Bottom; 
 
 
            listView1 . Dock = DockStyle. None; 
            button1 . Dock = DockStyle. None; 
            button2 . Dock = DockStyle. None; 
            button3 . Dock = DockStyle. None; 
            button4 . Dock = DockStyle. None; 
            button5 . Dock = DockStyle. None; 
            button6 . Dock = DockStyle. None; 
 
 
        } 


實現的效果如下:\\
                                                                                                       

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved