Anas
Anas

Reputation: 67

How to display a value from a ComboBox in a TextBox?

I have a table in a SQL Server database called "course", which contain 60 courses.

The table contain two columns: COURSE_ID (PK), COURSE_NAME

I get all courses from the database using a stored procedure:

create PROC [dbo].[GET_ALL_COURSES]
AS
SELECT * FROM COURSE

And populate a ComboBox like this:

public FORM_CRS()
{
    InitializeComponent();

    CMB_COURSE.DataSource = CRS.GET_ALL_COURSES();
    CMB_COURSE.DisplayMember = "COURSE_NAME";
    CMB_COURSE.ValueMember = "COURSE_ID";
}

When I click on a course in the ComboBox I want to show the COURSE_ID of that course in a TextBox, but I don't know how.

Upvotes: 0

Views: 14992

Answers (4)

Ckrempp
Ckrempp

Reputation: 324

First, I think that you should check this link as a reference.

Next, think that this segment of code matches what you are looking for:

    private List<Course> Courses = new List<Course>();

    public Form1()
    {
        InitializeComponent();

        Courses.Add(new Course("Computer Science",1));   
        Courses.Add(new Course("English", 2));
        Courses.Add(new Course("Art", 3));
        Courses.Add(new Course("Biology", 4));
        Courses.Add(new Course("Philosophy", 5));
        Courses.Add(new Course("Humanities", 6));

        CourseCB.DataSource = Courses;
        CourseCB.SelectedText = Courses.ElementAt(0).CourseName;
        CourseCB.DisplayMember = "CourseName";
        CourseCB.ValueMember = "CourseID";
    }

    private void OnValueChanged(object sender, EventArgs e)
    {
        if (CourseCB.SelectedIndex != -1)
        {
            CourseIDTB.Text = CourseCB.SelectedValue.ToString();
        }
    }

    public class Course
    {
        private string myCourseName;
        private int myCourseID;

        public string CourseName
        {
            get { return this.myCourseName; }
            set { this.myCourseName = value; }
        }

        public int CourseID
        {
            get { return this.myCourseID; }
            set { this.myCourseID = value; }
        }

        public Course(string name, int ID)
        {
            this.CourseName = name;
            this.CourseID = ID;
        }

    }

You had the correct format, but to use it properly you needed to supply property values of an object not from an SQL reference into the DisplayMember and ValueMember values.

I recommend that you perform the query, then insert the values into a Course object from there you would be able to utilize this code.

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

Subscribe to the SelectedValueChanged event immediately after populating your ComboBox:

CMB_COURSE.DataSource = CRS.GET_ALL_COURSES();
CMB_COURSE.DisplayMember = "COURSE_NAME";
CMB_COURSE.ValueMember = "COURSE_ID";

CMB_COURSE.SelectedValueChanged += (s, e)
    => textBox1.Text = CMB_COURSE.SelectedValue.ToString();

When the selection changes in your ComboBox, your TextBox will display the current COURSE_ID value.

Upvotes: 1

Salahaldin
Salahaldin

Reputation: 92

Create an event with your comboBox such as onSelectedIndexChanged..

void cmbCourse_onSelectedIndexChanged(object sender,EventArgs e) {
yourTextBoxName.Text=CMB_COURSE.SelectedItem.ToString();}

Upvotes: 1

Marek Woźniak
Marek Woźniak

Reputation: 1776

CMB.Click(object sender, EventArgs e) //click, changedvalue or something else what you want - change it for your neccesity
{
    txt.Text = ((ComboBoxItem)CMB_COURSE.SelectedItem).Content.ToString();
    //or, i can't test it now, well do it and edit this answer
    txt.Text = CMB_COURSE.SelectedValue.ToString();
    //or:
    DataRowView row = (DataRowView)CMB_COURSE.SelectedItem;
    if (row != null)
    {
         txt.Text = row[0] + " " + row[1];
    }
}

the last solution's from here: https://stackoverflow.com/a/8518930/3810460

Upvotes: 0

Related Questions