By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

checkbox eventhandler visual c# 2008

P: 69
Hello everyone,

Iam firing a event in the chechbox_checkedchanged. It works fine
Expand|Select|Wrap|Line Numbers
  1.  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  2.         {
  3.             /
  4.                 textBox1.Visible = false;
  5.                 textBox2.Visible = false;
  6.                 button2.Visible = false;
  7.  
  8.                 String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + datapath + "'";
  9.  
  10.                 using (OleDbConnection connection = new OleDbConnection(connectionString))
  11.                 {
  12.                     OleDbCommand command = new OleDbCommand("select * from userinfo WHERE ServerUrl='" + temp + "'", connection);
  13.  
  14.                     connection.Open();
  15.                     OleDbDataReader reader = command.ExecuteReader();
  16.                     if (reader.HasRows)
  17.                     {
  18.                         while (reader.Read())
  19.                         {
  20.                             String rea = reader[0].ToString();
  21.                             String reb = reader[1].ToString();
  22.                             MessageBox.Show("Username and Password are Matching Click ok to continue ..");
  23.                             groupBox2.Visible = false;
  24.                             groupBox1.Visible = false;
  25.                             groupBox3.Visible = true;
  26.                             checkBox1.Checked = false;
  27.                             newop(rea, reb, out res1, out res2);
  28.                         }
  29.                     }
  30.                     else
  31.                     {
  32.                         MessageBox.Show("No Record please provide username and pwd");
  33.                         groupBox2.Visible = true;
  34.                         textBox1.Visible = true;
  35.                         textBox2.Visible = true;
  36.                         button2.Visible = true;
  37.                         checkBox1.Checked = false;
  38.                         groupBox1.Visible = false;
  39.                     }
  40.                     reader.Close();
  41.                     connection.Close();
  42.                 }
  43.   }
  44.  
The problem after this event when i make the checkbox visible the checkbox is in checked state. so if i make the checkbox.checkedstate= false automatically the else part in the chekedchanged event is triggering i know why it is happening?.. what are the other ways i can make the checkbox in unchecked state without firing this eventhandler or should i write the code in someother handler event of checkbox ?..

Thank you.

Dinesh.
Feb 18 '09 #1
Share this Question
Share on Google+
4 Replies


P: 69
Hello everyone,

Iam firing a event in the chechbox_checkedchanged. It works fine
Expand|Select|Wrap|Line Numbers
  1.  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  2.         {
  3.  
  4.                 textBox1.Visible = false;
  5.                 textBox2.Visible = false;
  6.                 button2.Visible = false;
  7.  
  8.                 String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + datapath + "'";
  9.  
  10.                 using (OleDbConnection connection = new OleDbConnection(connectionString))
  11.                 {
  12.                     OleDbCommand command = new OleDbCommand("select * from userinfo WHERE ServerUrl='" + temp + "'", connection);
  13.  
  14.                     connection.Open();
  15.                     OleDbDataReader reader = command.ExecuteReader();
  16.                     if (reader.HasRows)
  17.                     {
  18.                         while (reader.Read())
  19.                         {
  20.                             String rea = reader[0].ToString();
  21.                             String reb = reader[1].ToString();
  22.                             MessageBox.Show("Username and Password are Matching Click ok to continue ..");
  23.                             groupBox2.Visible = false;
  24.                             groupBox1.Visible = false;
  25.                             groupBox3.Visible = true;
  26.                             checkBox1.Checked = false;
  27.                             newop(rea, reb, out res1, out res2);
  28.                         }
  29.                     }
  30.                     else
  31.                     {
  32.                         MessageBox.Show("No Record please provide username and pwd");
  33.                         groupBox2.Visible = true;
  34.                         textBox1.Visible = true;
  35.                         textBox2.Visible = true;
  36.                         button2.Visible = true;
  37.                         checkBox1.Checked = false;
  38.                         groupBox1.Visible = false;
  39.                     }
  40.                     reader.Close();
  41.                     connection.Close();
  42.                 }
  43.   }
  44.  
The problem after this event when i make the checkbox visible the checkbox is in checked state. so if i make the checkbox.checkedstate= false automatically the else part in the chekedchanged event is triggering i know why it is happening?.. what are the other ways i can make the checkbox in unchecked state without firing this eventhandler or should i write the code in someother handler event of checkbox ?..

Thank you.

Dinesh.
Feb 18 '09 #2

Expert 100+
P: 427
Hi

By reading your code it seems that you only want to execute this section if the check box is checked (in both the if and else you set the checked to false).

a work around might be to check if the textbox is checked before executing the code:

Expand|Select|Wrap|Line Numbers
  1. if(checkbox1.checked == true)
  2. {
  3.   //execute code
  4. }
  5. else
  6. {
  7.   return;
  8. }
if indeed you want the code to execute each time the checked state changes, i suggest you use a Boolean variable (not inside the event handler scope) to perform a similar task as described above. Neither of these methods is optimal as they both cause the event to fire more than once per user interaction.

Ultimately i would suggest that you use a command button to execute code that reads from the database, but that is a personal preference in design choice.

Good luck
Feb 18 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
Add an if statement around your whole code checking if the checkbox is checked before executing the call to the database.

If it's not checked, don't do anything.
If it is, execute the code.
Feb 18 '09 #4

tlhintoq
Expert 2.5K+
P: 3,525
[edit] Threads have been merged[/edit]

By the way, another technique for your need to would be to disconnect the event, change the checkbox, then reconnect the event.

.... -= new eventhandler ......
mycheckbox.checked = false;
... += new eventhandler ....
Feb 19 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.