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();
        }
    }
}
progTigerAsked:
Who is Participating?
 
GranModConnect With a Mentor Commented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0
 
athapaCEO / CTOCommented:
Seems like this should work!
AT

        public string DisplayMember
        {
            get { return displayMember; }
            set { displayMember = value;
                    this.Text = displayMember;
                  }
        }
0
 
progTigerAuthor Commented:
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tpwellsCommented:
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;
                 }
        }
0
 
tpwellsCommented:
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
0
 
progTigerAuthor Commented:
that just displays selected value. I need the text property to display the data stored within the displaymember data column.
0
 
progTigerAuthor Commented:
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 { }
                }
            }
        }
    }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.