Unable to Fetch Textbox values before ShowDialog in winforms
-
Tuesday, May 01, 2012 10:45 AM
//Form1 Code. That calls form2 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 objDlg = new Form2(); SqlConnection con = new SqlConnection("connectionstring"); SqlDataAdapter dap = new SqlDataAdapter("select * from employee where id =1", con); dap.Fill(objDlg.ds); objDlg.LoadData(); } }
//Form 2 Code public partial class Form2 : Form { public DataSet ds = new DataSet(); public Form2() { InitializeComponent(); } public void LoadData() { textBox1.DataBindings.Add("Text", ds.Tables[0], "FirstName"); if (textBox1.Text != "") { //I am not able to enter this block, even though i have value in my dataset. //But the text box is getting the value when it opens. i mean after showdialog box. checkBox1.Checked = true; } this.ShowDialog(); } }I have Form1 and Form2. I am calling Form2 from Form1. I am binding the controls in form2 using a datasource. I have two controls in form2, a text box and a check box. I want to enable the check box based on the content available in text box. But its not working. Look the code above to get more clarity.
Thanks in advance,
Ganesh
- Moved by Reed KimbleMicrosoft Community Contributor Tuesday, May 01, 2012 4:24 PM C# in VB forum (From:Visual Basic Language)
All Replies
-
Tuesday, May 01, 2012 12:14 PM
Hello Ganesh Kumar Nagarajan,
Welcome to Msdn Forum , this is VisualBasic Forum , try to ask in decidated Forum For C#.
http://social.msdn.microsoft.com/Forums/it-IT/csharplanguage/threads
Regards.
-
Tuesday, May 01, 2012 2:54 PM
well, first thing I would try, is adding a public property to access CheckBox1, and see if that helps. the object should exist after the InitializeComponent() call, but it may not be accessible.- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:28 AM
-
Tuesday, May 01, 2012 4:24 PM
At the point you read the textbox value, the databinding has not occured yet (the form is not yet visible).
Just read the underlying value that the textbox is bound to and don't worry about the databinding at this point in the form's life cycle.
Also, I'll move this to the C# general forum since the code is not VB...
-edit- sorry I meant at the point you read the textbox, not checkbox
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Edited by Reed KimbleMicrosoft Community Contributor Tuesday, May 01, 2012 10:11 PM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:28 AM
-
Tuesday, May 01, 2012 5:15 PM
In order to enable checkbox, try this:
checkBox1.Enabled = ! string.IsNullOrWhiteSpace( textBox1.Text );
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:28 AM

