Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a textbox, when a user input invalid data and leave the textbox, a form will be displayed by the ShowDialog method to inform that the data is invalid and ask if the user wants to re-enter. If he/she wants that, it should set FOCUS on the textbox. But it is not simple. Here is my code:
C#
private void OnLostFocus(object sender, EventArgs e)
{
   if(ReEnter) 
   {
     ReEnter = false;
     return;
   }
   if(!IsValidData()) 
   {
    MsgBox M = new MsgBox();
    M.ShowDialog();
    if(M.DialogResult = DialogResult.OK)
      {
        ReEnter = true;
        textBox1.Focus();
      }
   }
}

IsValidData() is my method to check if the data is valid, my ReEnter is to mark that the user wants to re-enter.
I think it should work, but it works a very strange way, even the ReEnter never comes true (when OK accepted to re-enter), that makes a loop of hiding and showing the dialog. I can't make it work as I want. Could you please see and check and test your own code to help me out of this problem. I want the data validity check will be fulfiled whenever the user leaves the textbox!
Thank you so much!
Posted
Updated 17-Apr-11 8:35am
v2

Call the textBox1.Focus() method on the IsValidData() method if the data is not valid instead of using the ReEnter variable and events.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Apr-11 14:39pm    
Simple as that, my 5.
--SA
Fabio V Silva 17-Apr-11 14:40pm    
Thanks :)
Albin Abel 17-Apr-11 14:48pm    
But I guess IsValidData() is a business logic, hopefully return a bool. Do you suggest mix it up with UI?. Actually the error is here if(M.DialogResult = DialogResult.OK) :)
[no name] 17-Apr-11 15:02pm    
Did you mean all the code in OnLostFocus should be placed in IsValidData()?
But I want IsValidData() to be called when the textbox loses focus, so I have to use event here! Is there another way? Haha! I have found the reason! A difference between OnLostFocus and OnLeave. Yeah, I replaced OnLostFocus by OnLeave, it works well without any loop of hiding and showing. I think that's it, could you please explain more? Thank you!
Fabio V Silva 17-Apr-11 15:06pm    
private void OnLeave(object sender, EventArgs e)
{
if(!IsValidData())
{
MsgBox M = new MsgBox();
M.ShowDialog();
if(M.DialogResult = DialogResult.OK)
{
textBox1.Focus();
}
}
}

No need to use the ReEnter variable.
Hi should be if(M.DialogResult == DialogResult.OK). Single = is an assignment operator, not compare operator. If it is a simple message box you can use inbuilt System.Windows.Forms.MessageBox. No need a separate form.

Good luck
 
Share this answer
 
Comments
[no name] 17-Apr-11 15:01pm    
Ah, I mistyped it! Thank you! But Does message box support Vietnamese? Yeah, that's why I use a form as a dialog instead! Thank you again!
[no name] 17-Apr-11 15:12pm    
I mean we only have "Yes" , "No" ... buttons but not have "Có" , "Không" (in Vietnamese). Of course the message can be written in Vietnamese. I love .NET, as before when programming with Visual basic 6.0, writing a message in Vietnamese is a very hard work!
Thank you!
Albin Abel 17-Apr-11 16:30pm    
Right. For that reason as Fabio V Silva said use the leave event handler not the lost focus..
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Leave+=new EventHandler(textBox1_Leave);
}
private void textBox1_Leave(object sender, EventArgs e)
{
Form2 testDialog = new Form2();
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
this.Focus();
}
}
Don't forgot to return correct dialog result from the dialog form. good luck
Sandeep Mewara 27-Apr-11 12:10pm    
Right! Good followup too. 5!

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