Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
I have a textbox and wrote textbo1_leave event that when the textbox is empty it shows a message 'textbox is empty' and focus returns to the textbox...i had a button 'close' to close the form..for that i had wrote button_click event to close the form 'this.close();'..now my problem is that when i put the cursor on the textbox and makes the textbox empty and try to close the form by button clicking..then it shows the message 'textbox is empty' and the form is not closing...but when i click on 'x' button of the form then it is closing without asking any message..
Please help me to make my close button as efficient as windows close button in c#, winforms.


The Below Is My Code:
C#
private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("TextBox Is Empty");
                textBox1.Focus();
            }
        }
Posted
Updated 10-Dec-12 20:00pm
v5
Comments
[no name] 11-Dec-12 0:55am    
Show up your code...
CHAITANYA KIRAN KASANI 11-Dec-12 1:33am    
Rohit I Had Updated My Code...Pls See
Sergey Alexandrovich Kryukov 11-Dec-12 1:13am    
A button cannot be "inefficient" unless you screw up things badly. And you don't even understand that you don't provide relative information. With such lack judgement, I cannot imagine that some advice help you much, sorry...
--SA
CHAITANYA KIRAN KASANI 11-Dec-12 1:32am    
Who Told You TO Imagine...If You Know Or Understand My Query Reply Me..If Not, Don't..Why Are You Suffering I Don't Understand...Keep Cool Man...
Sergey Alexandrovich Kryukov 11-Dec-12 11:26am    
Your way of talking is barely comprehensible, sorry, don't really want to "parse" it.
Your problem is very simple, but the real solution is the proper UI design. Solution 2 gives you some useful ideas. I would also advise not to consider empty text box as a mistake but do some default processing for such case.

For form closing behavior, handle FormClosing event and check event arg's CloseReason; in this case, switch off Leave behavior (have a flag to check in this handler, bypass your Leave handler if the flag is not set).
--SA

The problem is that your textbox's event fires before a button click is registered on the button. This is not the case for the window's close, minimize and resize buttons, because these are part of the window border, not part of the client area. In other words, when clicking on the window's close button, focus stays on the textbox.

Your best bet would be to rethink the textbox's behavior. Do you really need it to show you a message box every time? How 'bout only the first time the user leaves it blank? Or maybe use a label for the error message instead. Or do the validation at a later stage (such as when clicking another button whose function rely on the data in the textbox).

Another (very hairy) approach is to keep track of the mouse location and not show the message box and grab focus if the mouse happens to be over the button's client area. I strongly advise against this, as it can quickly become an unmaintainable mess full of unexpected bugs.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Dec-12 11:24am    
Reasonable, my 5.
The real solution is UI redesign. What you called hairy approach did not even worth mentioning.
--SA
this is logically impossible!
you cant stop firing an event of a control from its sibling.
but if your conditions are too hard & fast that you wants only this functionality, you have to write some if else statements and also use some of the events in textbox events lifecycle!.
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 11-Dec-12 1:56am    
ya Sumit.. Its Happening For Windows Close Button But Why Can't For Custom Close Button..k any way ThanQ For Your Reply
choudhary.sumit 11-Dec-12 2:02am    
anyways i post a code for you if it can help. but it is not the proper way to do coding

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void textBox1_Leave(object sender, EventArgs e)
{
if (!button1.Focused)
{
if (textBox1.Text == string.Empty)
{
MessageBox.Show("TextBox Is Empty");
textBox1.Focus();
}
}
else
{
}

}
CHAITANYA KIRAN KASANI 12-Dec-12 4:46am    
thanQ sumit for ur suggest....it helped me to find out the solution
choudhary.sumit 12-Dec-12 4:50am    
if it realy helped you. you can mark it as Answer.
choudhary.sumit 12-Dec-12 5:11am    
its wonderful, can you please explain me what is difference between those two formats.

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