Link to home
Start Free TrialLog in
Avatar of progTiger
progTiger

asked on

UserControl Textbox

I have created a usercontrol text with some additional data binding properties. How can i get the text property of the textbox to display the displaymember whenever the selectedvalue is changed?

Matthew

namespace WindowsApplication1
{
    [System.ComponentModel.LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "LookupMember")]
    [System.ComponentModel.DefaultBindingProperty("SelectedValue")]
    public partial class UserControl1 : TextBox
    {
        private Int32 selectedValue;
        private string valueMember;
        private string displayMember;
        private object dataSource;

        public object DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }

        public string DisplayMember
        {
            get { return displayMember; }
            set { displayMember = value; }
        }

        public string ValueMember
        {
            get { return valueMember; }
            set { valueMember = value; }
        }

        public Int32 SelectedValue
        {
            get { return selectedValue; }
            set { selectedValue = value; }
        }

        public string LookupMember
        {
            get { return selectedValue.ToString(); }
            set { selectedValue = Convert.ToInt32(value); }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}
Avatar of athapa
athapa

Seems like this should work!
AT

        public string DisplayMember
        {
            get { return displayMember; }
            set { displayMember = value;
                    this.Text = displayMember;
                  }
        }
Avatar of progTiger

ASKER

that just diplyaed the name of the displaymember. I should have clearified more, I want text property to diplay the data stored in the displaymember cell.

Matthew
if you are needing to display the SelectedValue when it changes do what athapa suggested but on the SelectedValue property

        public Int32 SelectedValue
        {
            get { return selectedValue; }
            set { selectedValue = value;
                    this.Text=selectedValue;
                 }
        }
you also need to change the reference to selectedValue this property:

       public string LookupMember
        {
            get { return selectedValue.ToString(); }
            set { selectedValue = Convert.ToInt32(value); }
        }

to "SelectedValue" with a capital "S" so it will call the property and assign the Text properly in it's property call
that just displays selected value. I need the text property to display the data stored within the displaymember data column.
figured it out, here what i did.

Matthew

    public partial class LookupTextBox : TextBox
    {
        private Int32 selectedValue;
        private string valueMember;
        private string displayMember;
        private DataTable dt;

        public LookupTextBox()
        {
            InitializeComponent();
        }

        public DataTable DT
        {
            get { return dt; }
            set { dt = value; }
        }

        public string ValueMember
        {
            get { return valueMember; }
            set { valueMember = value; }
        }

        public string DisplayMember
        {
            get { return displayMember; }
            set { displayMember = value; }
        }

        public Int32 SelectedValue
        {
            get { return selectedValue; }
            set
            {
                selectedValue = value;
                if (displayMember != null)
                {
                    try { this.Text = dt.Rows.Find(selectedValue)[displayMember].ToString(); }
                    catch { }
                }
            }
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial