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.
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.
Comments