My form loses focus after button click in c# - 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;
}

Related

How to set focus to a textedit on buttonclick in C# Winforms?

I need to set focus to a textedit on Button Click event ? in my Form
i tried this one but not working TXE_FlatDiscountP.Focus(); How to set focus on event ?
Your way is correct. Maybe you are missing something.To be more detailed here is an example:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
}
This is an example of setting focus to a textbox.

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();
}

Textbox losing focus after cursor selection moved to end

I have a form that loads, hits a button (to add text to a textbox), moves the cursor to the end of the textbox text, and then sets the active control to the textbox, so the user can immediately start typing. The textbox is populated fine, but the form loses its focus. This is only with the selection line in there, if I take it out, it works fine. The user has to click on the form to make it active. Any ideas?
private void createNewFolder_Load(object sender, EventArgs e)
{
addDate.PerformClick();
folderNameTextBox.Select(folderNameTextBox.Text.Length, 0);
this.ActiveControl = folderNameTextBox;
this.Focus();
}
Focusing cannot work in the Load event, the form is not yet visible. By far the simplest way is to just give the control the lowest TabIndex. Or use the Select() method:
private void createNewFolder_Load(object sender, EventArgs e)
{
addDate.PerformClick();
folderNameTextBox.Select(folderNameTextBox.Text.Length, 0);
folderNameTextBox.Select();
}
Instead of:
this.ActiveControl = folderNameTextBox;
Try:
folderNameTextBox.Focus();
If the textbox is still out of focus, try selecting it using:
folderNameTextBox.Select();

How does a Windows Forms control know when its form was (de)activated?

I have a Windows Forms application in C# .NET. It contains a user-drawn control that also handles the keyboard focus. If a part of the control has the focus, a focus highlight border is painted around it. When the form that contains the control is deactivated, the focus border must disappear from the control, obviously. But the control doesn't even get a notification about it. It only receives the "Leave" event when another control is focused, not another window. How can the control know about that?
When the Form+Control are loaded, the Control can subscribe to the Activate and DeActivated events of the Form.
If it is a UserControl, you have the Control.Load event to do this. For a CustomControl I would have to look it up.
Anyway, be sure to implement Dispose in your Control to unsubscribe to the events.
Just gave it a try:
private void UserControl1_FormActivate(object sender, EventArgs e)
{
label1.Text = "Acitve";
}
private void UserControl1_FormDeActivate(object sender, EventArgs e)
{
label1.Text = "InAcitve";
}
private void UserControl1_Load(object sender, EventArgs e)
{
this.ParentForm.Activated += UserControl1_FormActivate;
this.ParentForm.Deactivate += UserControl1_FormDeActivate;
}

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