How to select focus into current tab of TabControl

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

How to select focus into current tab of TabControl

Post by thatsameer »

Hi,

Before I begin i would like to thank everyone on Sapien Forums.

I have a tabcontrol with tabs. I can refresh the tab I'm on by mouse clicking in the tabs window and pressing F5.

I can also go to the next tab plus refresh the tab page with the navigation button:
$buttonNext_Click={
if($tabcontrol1.SelectedIndex -lt $tabcontrol1.TabCount - 1)
{
$tabcontrol1.SelectedIndex++
[System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
UpdateNavButtons
}


What I can't figure out is how to shift the focus onto the current tab without moving to the next tab.

I've tried:
$tabcontrol1.SelectedIndex
[System.Windows.Forms.SendKeys]::SendWait("{F5}")

also with
$tabcontrol1.SelectedTab
$tabcontrol1.Select()

But powershell isn't "clicking" into the window with the ones I've tried.

I've ran out of ideas how to have powershell "click" into the current tabs window to shift the focus into it.

Hope this makes sense.

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to select focus into current tab of TabControl

Post by jvierra »

You can set the currently displayed tab by selecting the tab. You are selecting the tab control. There is no "clicking" when selecting a tab.

$tabcontrol1.SelectedTab = $tabcontrol1.TabPages['tabPage3']
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: How to select focus into current tab of TabControl

Post by thatsameer »

Nice one, thanks. I'm very nearly there.

Instead of naming the tab page (as all my tabs have the same name), I can use the tab number e.g.
$tabcontrol1.SelectedTab = $tabcontrol1.TabPages[1]
[System.Windows.Forms.SendKeys]::SendWait("{F5}")

Very nearly there now... How can I make the tab number dynamic so it sets the tab page number to what it currently on on the GUI form?

$CurrentTabNumber = ........
$tabcontrol1.SelectedTab = $tabcontrol1.TabPages[$CurrentTabNumber]

So if i click the first tab it will automatically insert [0], and [1] if the 2nd tab is selected, so on...

Thanks so much
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to select focus into current tab of TabControl

Post by jvierra »

Every page has a name. The name is not the text shown but is the "Name" property.

There is no such thing as tab number. The "TabPages" property of the control is an array. There is a "TabIndex".

If you click on a tab the index will be the tab page you clicked on. There is no need t do this as the clicked tab is the "SelectedTab".

"F5" does nothing in a form as the form receives the F5 but form do not have a refresh event.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to select focus into current tab of TabControl

Post by jvierra »

To refresh any control use the control "Refresh()" method.

$tabpage1.Refresh()
$tabcontrol1.Refresh()
$textbox1.Refresh()


You can capture the F5 key in the form or any control in the keypress event. What it does is custom in the code. There is no need to use "SendKeys".
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked