C#自學之路25

來源:互聯網
上載者:User

標籤:public   文字框   

25.下拉式方塊控制項

  組合框(comboBox)控制項是一個文字框和一個列表框的組合。


  1.常用的屬性。

a.DropDownStyle,設定組合框的樣式,有三種可選值:

Simple:沒有下拉框,所以不能選擇,可以輸入,與Text控制項類似;

DropDown:具有下拉框,可以選擇和輸入;

DropDownList:具有下拉框,只能選擇,不能輸入。

b.MaxDropDownItems屬性,用於顯示清單項目的個數。


  2.常用的事件。

a.Click和SelectedIndexChanged事件。



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace WindowsFormsApplication10

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            switch (comboBox1.SelectedIndex)

            {

                case 0:

                    comboBox2.Items.Clear();

                    comboBox2.Items.Add( "北京" );

                    comboBox2.Items.Add( "上海" );

                    comboBox2.Items.Add( "武漢" );

                    comboBox2.SelectedIndex = 0;

                    break;

                case 1:

                    comboBox2.Items.Clear();

                    comboBox2.Items.Add("華盛頓");

                    comboBox2.Items.Add("紐約");

                    comboBox2.Items.Add("芝加哥");

                    comboBox2.SelectedIndex = 0;

                    break;

                case 2:

                    comboBox2.Items.Clear();

                    comboBox2.Items.Add("倫敦");

                    comboBox2.Items.Add("愛爾蘭");

                    comboBox2.Items.Add("考文垂");

                    comboBox2.SelectedIndex = 0;

                    break;

                default:

                    comboBox2.Items.Clear();

                    break;

            }

        }


        private void button1_Click(object sender, EventArgs e)

        {

            string str = comboBox1.SelectedItem.ToString() + ":" + comboBox2.SelectedItem.ToString();

            MessageBox.Show( str,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information );

        }

    }

}



650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6A/45/wKioL1UrezWzvM4pAADRWZC3jj0948.jpg" title=")N33]I`}(X3]~`E@SV8@SJH.png " alt="wKioL1UrezWzvM4pAADRWZC3jj0948.jpg" />


本文出自 “郭俊的部落格” 部落格,轉載請與作者聯絡!

C#自學之路25

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.