asp.net WinForm under ComboBox setting SelectedValue

Source: Internet
Author: 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 < i++)
{
Area A = new Area ();
a.area_id = i.ToString ();
A.area_name = "First" + i.tostring () + "name";
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 = "8th place";

            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 = "8th place";

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 = "8th place";

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 = "8th place";

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
& nbsp;   {
        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 {m_area_name;}
            set {m_area_name = value;}
       }
        private Double m_area_order;
        public double area_order
         {
            get {m_area_order;}
            set {m_area_order = value;}
       }
   }
    #endregion

    [Serializable]
    public class Arealists:list<area>
     {
        private int _maxitems = 0;
     & nbsp;  public int Maxitems {get {return this._maxitems;} set {this._maxitems = value;}}
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.