C# form validation with tooltip - c#

I am coding a C# Forms application and I have a question about validating a form.
I have a textbox called textBoxCodeToSearchFor that I want to validate when I press a button. If the textbox does not have any text in it, I need a tooltip to be shown.
Here is my code:
private void textBoxCodeToSearchFor_Validating(object sender, CancelEventArgs e)
{
if (String.IsNullOrEmpty(textBoxCodeToSearchFor.Text))
{
e.Cancel = true;
toolTip.Show("Please enter the code to search for", textBoxCodeToSearchFor);
}
}
On the button click I have the following code:
bool validated = this.Validate();
The tooltip is then shown, however, I cannot close the form when pressing a cancel button.
How can I shown a tooltip for a textbox if the textbox does not validate, but still close out of the form is wanted?
Thanks in advance.

Related

How to disable buttons in c# window form application before entering some text in textboxes

i want that when i am entering some text in my win form text boxes my buttons are disable till that time and when i finished entering my text in text boxes my buttons become enable.
i tried button.Enable=false at form load button this is not full fill my requirement
i tried button.Enable=false at form load button this is not full fill my requirement
That should work to disable the buttons (note the property is called Enabled, not Enable). Perhaps something else is enabling them after you disable them?
You can also set the initial state to disabled in the form designer, or in the Load event of the form (rather than when load buttons are clicked).
As for enabling them, you can add a TextChanged event to the text box(es) in question. When the event fires, see if all of the required text boxes have text in them, and enable/disable your button appropriately.
In page load set button.enabled = false;
private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
}
Then you can enable the button when you start typing something in the textbox.
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = true;
}
Can you tell us what is your requirements? If you can't set the button.enabled = false in the page load, you can use the designer to set the property to false.

In a textbox validating event, how to ignore clicks to a certain button?

The problem is this:
I have a form that accepts a TextBox text and 2 buttons, one to save the text into the database and the other one to cancel and close the form. The form also has an ErrorProvider that's used in the TextBox.Validating event like this:
private void txtNombre_Validating(object sender, CancelEventArgs e)
{
string errorMsg;
if (!ValidateText(txtNombre.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
txtNombre.Select(0, txtNombre.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(txtNombre, errorMsg);
}
}
It does what is expected, to not let the user to do anything in the form until the required field is filled, except for 1 thing: to let the user close the form via the Cancel button.
I tried comparing the object sender to the button cmdCancel but it's not possible because the sender is always the TextBox.
Any advice as to how to ignore the Validating event when the user clicks on cmdCancel?
Look at the property Control.CausesValidation. Set it to false for your cancel button.
Set CausesValidation to false on the Cancel button.

My form loses focus after button click in c#

I have a picturebox in my form that I can move around with my arrow keys, but when I click on a button the form loses focus. How do i get the focus back to the form?
I've already tried with this.focus(); but it didn't work.
Any suggestions?
This seems to make the Button loose the focus to the form.
private void button1_Click(object sender, EventArgs e)
{
this.ActiveControl = null;
}

Changing focus from on textbox to another, when tabpages is changed in C#?

I have several tabs in a form. Each tab has one textbox. When I enter tabpage1 I have managed to set the focus on the textBox1. When I press a button in tabpage1 I jump to a random tab in the controller. What I want now is to have the focus set on textBox in the active tabpage. I have tried using tabpage_Enter event, but it does not seem to work. My code look like this :
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox2.Select();
}
Any suggestions?
I think you need to use SelectedIndexChanged event of TabControl instead of _Enter, using Enter event, focus will change to textBox2 every time the cursor enter the tabPage control.
You can use the Focus() method set the focus on a textbox. I would probably set on the tabPage_Enter event.
private void tabPage_Enter(object sender, EventArgs e){
{
var tab = sender as tabPage;
if(!tab.Focused) tab.focus();
}

Message box causes loss of focus

I have created a tool bar which has three controls. First one being a text box , an OK button and a Clear button. Essentially I am using this toolbar to search some text. When there are no results found , I pop up a message box informing the user that no results were found. But when the user clicks "OK" button of the message box, the text box looses focus and the focus passes to the next control which is the "OK" button. What should I do to avoid the text box to loose focus. I am using c#.
You can't. Clicking on the Ok button forces it to have control (and therefore the textbox loses control).
You can, however, do this on your click event:
MessageBox.Show("asdf");
textBox1.Focus();
EDIT
In response to your comment, I don't think that there's an easy way to return focus to the last control once another control has received focus, and the search and clear buttons will have to receive focus when clicked. You can do this:
private Control _last;
private void textBox1_Leave(object sender, EventArgs e)
{
_last = (Control) sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("asdf");
_last.Focus();
}
It's not very clear your question, but you can make your control take the focus like this:
textBox1.Focus();

Categories