asp.net WinForm下ComboBox設定SelectedValue

來源:互聯網
上載者:User

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace winformcombobox
{
    public partial class frmmain : form
    {
        public frmmain()
        {
            initializecomponent();
        }
        #region members
        arealists ac;
        public arealists gettestarea()
        {
            arealists al = new arealists();
            for (int i = 1; i < 11; i++)
            {
                area a = new area();
                a.area_id = i.tostring();
                a.area_name = "第" + i.tostring() + "名";
                al.add(a);
            }
            return al;
        }

        #endregion

        #region events
        private void frmmain_load(object sender, eventargs e)
        {
            ac = gettestarea();
            cblist.datasource = ac;
            cblist.displaymember = "area_name";
            cblist.valuemember = "area_id";
        }
        private void btnsetcombobox_click(object sender, eventargs e)
        {
            ////setvalue0();
            //setvaluebyvalue();
            ////setvaluebyitem();
            setvaluebytext();

        }
        #endregion

        #region methods

        private void setvalue0()
        {
            area a = new area();
            a.area_id = "8";
            a.area_name = "第8名";

            cblist.datasource = gettestarea();
            cblist.displaymember = "area_name";
            cblist.valuemember = "area_id";
            cblist.selectedvalue = a.area_id;
            lbresult.text = cblist.selectedvalue.tostring();
        }

        private void setvaluebyvalue()
        {
            area a = new area();
            a.area_id = "8";
            a.area_name = "第8名";

            cblist.selectedvalue = a.area_id;
            lbresult.text = cblist.selectedvalue.gettype().tostring() + ":" + cblist.selectedvalue.tostring();
        }

        private void setvaluebyitem()
        {
            area a = new area();
            a.area_id = "8";
            a.area_name = "第8名";

            cblist.selecteditem = ac.findall(delegate(area ar) { return ar.area_id == a.area_id; })[0];
            lbresult.text = cblist.selecteditem.gettype().tostring() + ":" + cblist.selectedvalue.tostring();
        }

        private void setvaluebytext()
        {
            area a = new area();
            a.area_id = "8";
            a.area_name = "第8名";

            cblist.selectedindex = cblist.findstring(a.area_name);
            lbresult.text = cblist.selectedvalue.gettype().tostring() + ":" + cblist.selectedvalue.tostring();
        }
        #endregion
    }
}


using system;
using system.collections.generic;
using system.text;

namespace winformcombobox
{
    #region area
    public class area
    {
        private string m_area_id;
        public string area_id
        {
            get { return m_area_id; }
            set { m_area_id = value; }
        }

        private string m_area_name;
        public string area_name
        {
            get { return m_area_name; }
            set { m_area_name = value; }
        }
        private double m_area_order;
        public double area_order
        {
            get { return m_area_order; }
            set { m_area_order = value; }
        }
    }
    #endregion

    [serializable]
    public class arealists : list<area>
    {
        private int _maxitems = 0;
        public int maxitems { get { return this._maxitems; } set { this._maxitems = value; } }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.