Unable to Fetch Textbox values before ShowDialog in winforms
-
2012年5月1日 上午 10:45
//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
- 已移動 Reed KimbleMicrosoft Community Contributor 2012年5月1日 下午 04:24 C# in VB forum (From:Visual Basic Language)
所有回覆
-
2012年5月1日 下午 12:14
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.
-
2012年5月1日 下午 02:54
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.- 已標示為解答 Bob ShenMicrosoft Contingent Staff, Moderator 2012年5月9日 上午 02:28
-
2012年5月1日 下午 04:24
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"
- 已編輯 Reed KimbleMicrosoft Community Contributor 2012年5月1日 下午 10:11
- 已標示為解答 Bob ShenMicrosoft Contingent Staff, Moderator 2012年5月9日 上午 02:28
-
2012年5月1日 下午 05:15
In order to enable checkbox, try this:
checkBox1.Enabled = ! string.IsNullOrWhiteSpace( textBox1.Text );
- 已標示為解答 Bob ShenMicrosoft Contingent Staff, Moderator 2012年5月9日 上午 02:28

