Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I'm new to project code and C#. I would appeciate advice on the following:

Current problem :(: I have a textbox in which a ref no. is input, the validation of this number occurs in a textbox_leave event. Problem is when user then goes to date and time picker, and if textbox isnt valid a messagebox comes up, and date and time picker just remains dropped down.

Ideal solution:): if textbox invalid, the date and time picker calender doesnt drop down and focus to be on back on textbox.

Thank you
Posted
Comments
Herman<T>.Instance 8-Feb-12 4:33am    
please show code from textbox_leave code

C#
private void textBox2_Leave(object sender, EventArgs e)
       {
           var carindex2 = (textBox2.Text.Trim()).ToUpper();

               string connectionString = "Provider= OraOLEDB.Oracle; Data Source=TSTEEL;User ID=NEW_DBA;Password=tsteel";
           string sql = "SELECT count(car_index) FROM CAR where car_index = '" + carindex2 + "'";
           OleDbConnection connection = new OleDbConnection(connectionString);
           OleDbCommand command = new OleDbCommand(sql, connection);
           connection.Open();
           int count = Convert.ToInt32(command.ExecuteScalar());
           connection.Close();

           if (count == 0)
           { ////error message if car index doesnt exist

               MessageBox.Show("Car Index does not exist, Please try again!", "Car not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
               textBox2.Clear();



           }

       }
 
Share this answer
 
If textbox is invalid then set focus to textbox again like following code

C#
textbox1.Focus();


Hope this will help you...
 
Share this answer
 
Comments
nikki88 8-Feb-12 4:43am    
Thanks i've tried it, but as weird as it is, it makes the message box come up twice !
try this code
C#
private void textBox2_Leave(object sender, EventArgs e)
        {
            var carindex2 = (textBox2.Text.Trim()).ToUpper();
 
                string connectionString = "Provider= OraOLEDB.Oracle; Data Source=TSTEEL;User ID=NEW_DBA;Password=tsteel";
            string sql = "SELECT count(car_index) FROM CAR where car_index = '" + carindex2 + "'";
            OleDbConnection connection = new OleDbConnection(connectionString);
            OleDbCommand command = new OleDbCommand(sql, connection);
            connection.Open();
            int count = Convert.ToInt32(command.ExecuteScalar());
            connection.Close();
 
            if (count == 0)
            { ////error message if car index doesnt exist
              
                MessageBox.Show("Car Index does not exist, Please try again!", "Car not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox2.Focus();
                textBox2.Clear();
            }
 
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900