Winform的學習


          昨天由於一些原因沒有上課啦,雖然也看啦一些東西,但是感覺太少也就沒有來啦,嘿嘿,今天認真地了解啦winform,學習了一些控件的使用,但是感覺好多屬性知道怎么用的,卻還是記得不太清楚,感覺看到啦知道,之后可能又會全然忘記啦,雖然這個winform已經不再用啦,但是還想是想好好學習下啦,只怕那天再次遇到啦還要自己在找資料啦,所以我還是詳細的總結下啦

      一.彈窗

         在Form中彈出另外一個窗體,首先建立一個Form窗體,把Form轉變為容器改變其屬性為IsMdiContainer,改變其屬性如下:

             

    然后在工具中選取MenuStrip工具,添加其標題,建立其他子窗體:

             

      在父窗體中實現彈窗功能代碼如下:   

public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private void 彈出主窗體ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.MdiParent = this;
            Form3 f3 = new Form3();
            f3.MdiParent = this;
            Form4 f4 = new Form4();
            f4.MdiParent = this;
            Form5 f5 = new Form5();
            f5.MdiParent = this;
            Form6 f6 = new Form6();
            f6.MdiParent = this;
            Form7 f7 = new Form7();
            f7.MdiParent = this;
            Form8 f8 = new Form8();
            f8.MdiParent = this;

            f2.Show(); f3.Show(); f4.Show();
            f5.Show(); f6.Show(); f7.Show();
            f8.Show();
        }

        private void 彈出縱向主窗體ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void 彈出橫向主窗體ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }
    }

            二.圖片控件PictureBox的使用

       設計Form窗體如下所示,添加一個PictureBox控件,然后選擇添加兩個按鈕,最好下載一組圖片以便於備用,然后實現其點擊按鈕上一張可以在PictureBox中顯示上一張圖片,點擊按鈕下一張,可以再PictureBox中顯示下一張圖片,代碼如下:

               

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int i = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] arraypath = Directory.GetFiles(@"G:\image");
            this.pictureBox1.Image = Image.FromFile(arraypath[0]);  
        }   
        private void btnpre_Click(object sender, EventArgs e)
        {
            string[] arraypath = Directory.GetFiles(@"G:\image");
           
            if (i == 0)
            {
                this.pictureBox1.Image = Image.FromFile(arraypath[arraypath.Length - 1]);
                i = arraypath.Length - 1;
            }
            else
            {
                i--;
                this.pictureBox1.Image = Image.FromFile(arraypath[i]);
            }        
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string[] arraypath = Directory.GetFiles(@"G:\image");

            if (i == arraypath.Length - 1)
            {
                this.pictureBox1.Image = Image.FromFile(arraypath[0]);
                i = 0;
            }
            else
            {
                i++;
                this.pictureBox1.Image = Image.FromFile(arraypath[i]);
            }
        }
    }

               三.圖片幻燈片形式播放

           在Form窗體中選擇添加控件PictureBox,然后添加一控件Timer即可,然后設置Timer的屬性其屬性,實現代碼如下:

           

       

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] arraypath = Directory.GetFiles(@"G:\image");
            this.pictureBox1.Image = Image.FromFile(arraypath[0]);
        }
        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            string[] arraypath = Directory.GetFiles(@"G:\image");
            i++;
            if (i == arraypath.Length)
            {
                i = 0;
                this.pictureBox1.Image = Image.FromFile(arraypath[i]);
            }
            else
            {
                this.pictureBox1.Image = Image.FromFile(arraypath[i]);
            }                   
        }
    }

               四.richBox。

                簡單的說, 比普通textbox多的功能: 可以為每個字設定獨立的字體,字號,顏色等..可以添加圖片, 可以添加OLD對象.還比textbox多了很多種方法,比如直接打開文件,打印文件等。下面簡單的實現下其功能:新建一個Form窗體,然后選擇一個richBox控件,在文本框中選擇寫入一些文本,然后實現其事件,格式以及代碼如下:

      

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            this.richTextBox1.AppendText("我被你寫入了這個對話框里面");
        }

             五.comboBox的使用

               combobox在這里使用時間需要注意的是,僅僅能夠選擇單項。

                       

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] strs = {"--請選擇下拉項--", "C語言也能干大事","在校不迷茫","畢業即輝煌" };
            this.comboBox1.Items.AddRange(strs);                    
            this.comboBox1.SelectedItem = this.comboBox1.Items[0];
        }
}

             六.textbox和webBrowser的簡單使用

        private void button1_Click(object sender, EventArgs e)
        {
            string urlpath = this.textBox1.Text;
            Uri uri = new Uri("http://"+ urlpath);
            this.webBrowser1.Url = uri;
        }

         實現輸入網址可以打開其網頁的功能,例如:打開如鵬網。

               

            七.實現區域的三級分類

        在Form窗體上面選擇三個textbox即可,然后實現其功能,代碼入下:  

 public partial class Form1 : Form
    {
        string[] Sheng = { "--請選擇省份--", "河南省", "湖南省" };
        string[] Henancity = { "--請選擇城市--", "鄭州", "洛陽", "開封" };
        string[] HuNancity = { "--請選擇城市--", "長沙", "湘潭", "株洲" };

        string[] zhengzhouArea = {"高新區","中原區","金水區","管城區","二七區","惠濟區","鄭東新區","航空港區","經開區" };
        string[] changshaArea = {"雨花區","芙蓉區","高橋區","岳麓區","韶山區" };
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.cmbsheng.Items.AddRange(Sheng);
            this.cmbsheng.SelectedIndex = 0;
        }
        private void cmbsheng_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.cmbsheng.SelectedIndex;
            switch (index)
            {
                case 1:
                    this.cmbcity.Items.Clear();
                    this.cmbcity.Items.AddRange(Henancity);
                    this.cmbcity.SelectedIndex = 0;
                    
                    break;
                case 2:
                    this.cmbcity.Items.Clear();
                    this.cmbcity.Items.AddRange(HuNancity);
                    this.cmbcity.SelectedIndex = 0;
                    break;
                default:
                    break;
            }
        }
        private void cmbcity_SelectedIndexChanged(object sender, EventArgs e)
        {
            int  index=this.cmbcity.SelectedIndex;
            object selecttext = this.cmbcity.SelectedItem;

            switch (index)
            {
                case 1:
                    if (selecttext.Equals("鄭州"))
                    {
                        this.cmbarea.Items.Clear();
                        this.cmbarea.Items.AddRange(zhengzhouArea);
                        this.cmbarea.SelectedIndex = 0;
                    }
                    else if(selecttext.Equals("長沙"))
                    {
                        this.cmbarea.Items.Clear();
                        this.cmbarea.Items.AddRange(changshaArea);
                        this.cmbarea.SelectedIndex = 0;
                    }          
                    break;
                case 2:
                    this.cmbarea.Items.Clear();
                    this.cmbarea.Items.AddRange(null);
                    this.cmbarea.SelectedIndex = 0;
                    break;
                case 3:
                    this.cmbarea.Items.Clear();
                    this.cmbarea.Items.AddRange(null);
                    this.cmbarea.SelectedIndex = 0;
                    break;
            }
        }
    }

              八.用ListBox和PictureBox實現其在ListBox中選擇圖片的序列,在picturebox中顯示圖片

 private void Form1_Load(object sender, EventArgs e)
        {
            string[] imagepath = Directory.GetFiles(@"G:\image");
            string[] filename=new string[imagepath.Length];
            for (int i = 0; i < imagepath.Length; i++)
            {
                string path = Path.GetFileNameWithoutExtension(imagepath[i]);
                filename[i] = path+"\r\n";               
            }
               listBox1.Items.AddRange(filename);
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.listBox1.SelectedIndex;
            this.pictureBox1.Image = Image.FromFile(@"G:\image\" + index + ".jpg");
        }

 結果如下:

                    

               九.在messagebox消息框小時選擇的listBox的內容

              

private void Form1_Load(object sender, EventArgs e)
        {
            string[] strs = {"1","2","3" };
            listBox1.Items.AddRange(strs);
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                 var  selected= this.listBox1.SelectedItems;
                 foreach (var item in selected)
                 {
                     MessageBox.Show(item.ToString());
                 }
        }

          其運行結果如下:

               

            好啦,今天在雲和學院學習就這些啦,雖然有點多吧,但是在winform這方面也不需要過多的學習吧,所以就講啦好多啦,只是讓我們理解熟悉啦,嘿嘿,winform的學習感覺還好啦,只是在每個控件的屬性上面該設置哪個屬性估計記得是不太清楚,我想是不熟悉的原因吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM