Cycling through TabControl tabs while not having focus on it - c#

I started with my first WinForms project, and bumped into a small problem on the way.
I have a Form with a TabControl and some buttons outside it.
The problem is when i have a button focused and press Ctrl-Tab - nothing happens. But if you open, for instance, properties window of a file in explorer, you can cycle through tabs using Ctrl-Tab no matter which element has the focus.
So what is the right way to make such behavior? I mean, i can do Form.KeyPreview = true and write handlers myself, but is there a better way to do this?

As far as I know, the correct way is the one you mentioned yourself i.e. set Form.KeyPreview = true and write private Form1_KeyDown(object sender, KeyEventArgs e) handler to switch tabs.

Related

The difference between GotFocus and GotKeyboardFocus

What is the difference(s) between GotFocus and GotKeyboardFocus -and similarly LostFocus and LostKeyboardFocus?
Sorry for the simple question, but, I googled it and read a lot of blog posts, but I'm still confused. It seems nobody knows exactly what is the difference ):
UPDATE:
My usage:
I am creating a custom control by extending Control class. Something like ComboBox but with some other effects. I'm trying to open and close a Popup by setting a property: IsDropDownOpen just like a ComboBox through the GotFocus and LostFocus events. I don't want to Popup get closed, when I Alt+Tabed the windows, but get closed when I click on a Button for example or I go to a TextBox. I did:
private static void OnGotFocusHandler(object sender, RoutedEventArgs e) {
if (e.Handled)
return;
((SearchBox)sender).IsDropDownOpen = true;
e.Handled = true;
}
private static void OnLostFocusHandler(object sender, RoutedEventArgs e) {
if (e.Handled)
return;
((SearchBox)sender).IsDropDownOpen = false;
e.Handled = true;
}
The GotFocus works. But the Lost one didn't. If I do the Lost stuff in LostKeyboardFocus then when I Alt+Tab the windows, or Window goes to inactive, then the method get called, while I don't want. How can I solve it?
MSDN has an overview of focus, but I'll try to explain it here.
WPF has 2 concepts regarding focus. There is the physical keyboard focus, and there is logical focus. Only one element can have keyboard focus (and if the application isn't the active application, no element will have keyboard focus).
Multiple items can have logical focus. In fact, you can create new "focus scopes". As per MSDN:
When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.
You can define your own focus scope on an element (typically a Panel) by setting FocusManager.IsFocusScope="True". The controls in WPF that are focus scopes by default are Window, MenuItem, ToolBar, and ContextMenu.
This makes sense if you think about having multiple Windows in your application. When you Alt-Tab between them, you expect your keyboard focus to return to the same place it was the last time the Window had focus. By keeping keyboard focus and logical focus separate, you can achieve this.

Troubles with updating ToolstripDropdownButton after window loses focus

Let me start from a simple example to reproduce my problem:
Create WinForms project.
Drop a ToolStrip on it.
Make a ToolstripDropdownButton in it (with no children).
Add a click event-handler to the button, with something like this:
MessageBox.Show("text", "Caption");
(the point is to make the main form loose it's focus).
To make the problem more obvious, let's make a few unnecessary additions:
Set RenderMode of our ToolStip to ManagerRenderMode.
Set it's Autosize to false and make it a lil bigger.
And that's should be enough to reproduce it. Run the app and click the Button. You'll get the popup. Close the popup and voila ... the button now looks like someone is holding it pressed.
Any ideas on how to reset it to it's default state?
I've tried to call Buttons ResetBackColor, Refresh ... also as ToolStrips. Doesn't seem to work. Maybe I'm missing something?
The MessageBox or a modal form will interfere with that. The drop down is expecting children menu items.
Sample work around:
private void toolStripDropDownButton1_Click(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => MessageBox.Show("Help")));
}

C# Tabcontrol messed up my form

I had a form made up with buttons, textboxes, and labels. Then I decided to see if I could use tab control because I wanted to add other buttons but I didn't want them to be on the same page. So I cut all my buttons and textboxes and labels and I paste the tabcontrol. Then I paste all of the buttons and stuff back on in tab1. So before I moved on to making buttons I decided to test run the program. I got a page that came up and said nullreferenceexeption was unhandled. When I tried clicking on one of my button, I saw that there was no code inside it and it went from
private void Button1_Click(object sender, EventArgs e)
to
private void Button1_Click_1(object sender, EventArgs e)
Then the errors kept coming up. Now I just want to make my form the way it was before but even though I deleted the tabcontrol, when I click my buttons they still have the _Click_1 thing. Please tell me how I can fix this. I am new to C# so try to explain it simply.
And if you can, tell me how I can add the tabcontrol thing without messing up my entire program.
Also is there a way I could like roll back changes for a day or 2 in visual studio ultimate? Please get back as soon as possible I'm ripping my hair out I can't lose the program I've been working on for 2 weeks.
You can fix most of these problems, but it's going to take a bit of work. For each button, open the properties tab and go to the events section, find the event Click and click once in the text field. You should see a drop down arrow, click that arrow and look at the list. Find the event you expect the button to fire and click on it (Hint : it doesn't have _1 at the end)

Windows form has focus but doesn't process keyboard events

I have a strange problem:
I have a Form that I open using ShowDialog(). The form is populated with some buttons and comboboxes. One of the comboboxes is set as the ActiveControl of the Form and the Form has focus.
What I want to accomplish is that the user can enter its username immediately after the Form opens (without the need to select the combobox first). However, if I press the keyboard, nothing happens. However, when I first click on the form with the mouse, and then enter something using the keyboard, it works. I already tried a lot of things like calling Select() and Focus() on the form. I even tried to simulate a mouseclick event (OnMouseClick) on the Form without any luck.
Someone has an idea would could be the problem here?
many thanks
Chris
Try BringToFront()
var f = new Form1();
f.Show();
f.BringToFront();
Then just use Select on that control
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Select();
}
Assuming you're running ShowDialog() from another form it might be worth changing it to: ShowDialog(this) so the new form has the correct parent and the correct blocking behaviour. Without the 'this' I've seen forms open up behind other forms and other strange behaviour, including focus problems.
Just a thought.
Have you tried calling myComboBox.Focus();
Just because the form has focus doesn't necessarily mean any control within the form has focus. Also try checking the onKey events of both the form and the individual controls. This normally helps me diagnose what exactly has focus. If the form itself and non on its controls are getting any onKey events then try using form.Activate();

Where does tab goes?

I am working on winform project. At runtime when I am moving through different controls through tab key ; after one button my tab disappears for 2 clicks. I tried all things to fix this. I set tabstop=false for all contols in winform but still I am getting same problem.
Then I decided to add following code:
Control nextControl = this.GetNextControl(this.guipnlReportPatientMeasurementDetails.Controls[10], true);
where GetNextControl property gets the name of the control where my control will go after pressing tab key and Controls[10] is the button. So where should I place above piece of code so that I would get the name of next control. Should it be in button_click event or somewhere else ?
Guys please suggest.
You should set TabIndex property for each control according to the Tab order you want to impose (you can set it through designer).
Set TabStop = false only for those controls you want to exclude from Tab selection.
There's also a useful button that shows TabIndexes on the form:
You are digging yourself a hole. Find out what the real problem is, there's some kind of control that is either off the window or doesn't properly indicate the focus. If you have no clue what control that might be then add a timer and a label. Set the timer's Enabled property to True, Interval to 200. Write the Tick event like this:
private void timer1_Tick(object sender, EventArgs e) {
if (this.ActiveControl == null) label1.Text = "No control?";
else label1.Text = this.ActiveControl.Name;
}
That tells you where the tab goes.
I would first try to fix the tab order on the window before resolving to using this code. Did you do that?

Categories