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.
Locked
thatsameer
Posts: 18
Joined: Fri Aug 03, 2018 7:02 am

How to select focus into current tab of TabControl

Post by thatsameer » Thu Apr 18, 2019 4:11 am

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

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How to select focus into current tab of TabControl

Post by jvierra » Thu Apr 18, 2019 5:11 am

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
Joined: Fri Aug 03, 2018 7:02 am

Re: How to select focus into current tab of TabControl

Post by thatsameer » Thu Apr 18, 2019 6:07 am

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

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How to select focus into current tab of TabControl

Post by jvierra » Thu Apr 18, 2019 7:01 am

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.

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How to select focus into current tab of TabControl

Post by jvierra » Thu Apr 18, 2019 7:04 am

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".

Locked