C# ComboBox and TextBox dynamic value - How can I access a TextBox value from another function or event?

Muhammed

l write some code under page. l created 2 object. (staff combobox and tcnu textbox). When l created staff Combobox l can access from staff_SelectedValueChanged() but l want to that: When l change value of staff_SelectedValueChange write new value to tckNu(textbox). How can l access if l creat a dynamic value from other funcion or event? l hope l can explain my problem. Sorry for my poor english. Thanks everybody..

void personalCall()
        {            
            try
            {
                SQLiteConnection conn= new SQLiteConnection("Data Source=vt/aksehirmeb.db");
                conn.Open();

                SQLiteCommand kmt = new SQLiteCommand("Select * from Personel", conn);
                kmt.ExecuteNonQuery();

                DataTable dt = new DataTable();
                SQLiteDataAdapter adp = new SQLiteDataAdapter(kmt);
                adp.Fill(dt);
                
                for (int increase = 0; increase < Convert.ToInt32(personelNumber.Text); increase++)
                {
                    //Creat Combobox
                    ComboBox staff = new ComboBox();
                    staff.Name= "staff" + increase;
                    staff.DropDownStyle = ComboBoxStyle.DropDownList;
                    //Creat Textbox  
                    TextBox tckNu = new TextBox();
                    tckNu.Name = "tcknu" + increase;                    

                    foreach (DataRow dr in dt.Rows) // Fill combobox with data 
                    {
                     staff.Items.Add(dr["staffName"].ToString() + "-" + dr["staffSurname"].ToString());                        
                    }

                    //Position Combobox
                    staff.Left = startPosition.Left + 50;
                    staff.Top = startPosition.Top + increase * 25;
                    staff.SelectedValueChanged += staff_SelectedValueChanged;

                    //Position Textbox
                    tckNu.Left= startPosition.Left + 175;
                    tckNu.Top= startPosition.Top + increase * 25;
                    tckNu.ReadOnly = true;

                    panel1.Controls.Add(staff); //                                                      
                    panel1.Controls.Add(tckNu); //
                }
                conn.Close(); 
            }            
        }



 private void staff_SelectedValueChanged(object sender, EventArgs e)
        {
            //l can access combobox value 
            MessageBox.Show("ComboBoxsenderText= " + ((ComboBox)sender).Text);
            //l want write this value textbox(tckNu.text). But l dont know how can l access tckNu.text 
            
        }  

i tried some things but realy l didnt find fix problem.

LolghElmo

You can try the following to access the TextBox and update its value based on the selected value of the ComboBox:

   private void staff_SelectedValueChanged(object sender, EventArgs e)
{
    ComboBox cmbStaff = sender as ComboBox;
    string tckNuName = "tcknu" + cmbStaff.Name.Replace("staff", "");
    TextBox txtTckNu = this.Controls.Find(tckNuName, true).FirstOrDefault() as TextBox;
    if (txtTckNu != null)
    {
        txtTckNu.Text = cmbStaff.SelectedItem.ToString();
    }
}

This code retrieves the ComboBox that triggered the event, and uses its name to construct the name of the associated TextBox. It then uses the Find` method to retrieve the TextBox control, and sets its text to the selected value of the ComboBox.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert textbox value to double in c#?

How i can get value from Binded Textbox in ListView?? (C# UWP)

How can I pass value to my component on `onBlur` event of PrimeNG autocomplete textbox?

How to pass textbox value from a page into a textbox in another page?

How to give a dynamic textbox a default value based on another dynamic dropdown and textbox?

Textbox event binding and textbox value

How to get value from dynamic textbox with dynamic id

How to get dynamic textbox value c#?

how can i get value of textbox on radio button's change event in cloning?

Adding numeric value from textbox into another textbox

Textbox value from dynamic ul on button click event

How can i get value from textbox?

How to populate textbox A with a value calculated from the values of textbox B and textbox C?

How can i check a value is pasted in Textbox?

How do I return a value from a function in JavaScript to a textbox in HTML

Update textbox value $on event

how to automatically set value of textbox according to value of another textbox

How to set a TextBox value when another TextBox value is changed

How to Bind Value from 1 textbox to another textbox in C# using AngularJS

How to capture the value of a textbox from an OnChange event

how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value

Access: trying to open a form and populate a textbox with the value from another form

How can I use a textbox value to set a value on a radiobutton

How to get value from ComboBox and TextBox into Argument line?

Dynamic value from TextBox to DataGridView

How can I display value in textbox from database that value based on selected value from combobox in php?

C# WPF : how can i access page controls from another page to get textbox.text

How can i use the value from a textbox component in blazor?

How can I fill in a value from one TextBox to another TextBox in Blazor Server?