 |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
|
|
|
Using KeyDown method to change focus of different form objects
Last Post 25 Jan 2012 04:50 PM by leibowitz. 1 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
| Author |
Messages |
 |
Sweenus
 New Member Posts:1

 |
| 23 Sep 2011 05:41 AM |
|
Hi, I have been trying to figure out how to set focus to alternate buttons on a Form.
if ($args.count) {$processName=$args[0]}
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select Transfer Method"
$objForm.Size = New-Object System.Drawing.Size(280,155)
$objForm.FormBorderStyle="FixedSingle"
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq 13) {$x=$objForm.ActiveControl.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq 27) {$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq 39) {
$_name=$objForm.ActiveControl.Name
$_int=[int][string]$_name[-1]
$_name=$_name.Substring(0,$_name.Length-1) + [string]$_int
$objForm.ActiveControl.Name=$_Name echo $_name }})
#This last Add_KeyDown method is the one I would like to use use to set focus to adjacent buttons
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(20,60)
$Button1.Size = New-Object System.Drawing.Size(75,23)
$Button1.Text = "Option 1"
$Button1.Name = "Button1"
$Button1.Add_Click({$x=$Button1.Text;$objForm.Close()})
$objForm.Controls.Add($Button1)
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(20,84)
$Button2.Size = New-Object System.Drawing.Size(75,23)
$Button2.Text = "Option 2"
$Button2.Name = "Button2"
$Button2.Add_Click({$x=$Button2.Text;$objForm.Close()})
$objForm.Controls.Add($Button2)
$Button3 = New-Object System.Windows.Forms.Button
$Button3.Location = New-Object System.Drawing.Size(95,60)
$Button3.Size = New-Object System.Drawing.Size(75,23)
$Button3.Text = "Option 3"
$Button3.Name = "Button3"
$Button3.Add_Click({$x=$Button3.Text;$objForm.Close()})
$objForm.Controls.Add($Button3)
$Button4 = New-Object System.Windows.Forms.Button
$Button4.Location = New-Object System.Drawing.Size(95,84)
$Button4.Size = New-Object System.Drawing.Size(75,23)
$Button4.Text = "Option 4"
$Button4.Name = "Button4"
$Button4.Add_Click({$x=$Button4.Text;$objForm.Close()})
$objForm.Controls.Add($Button4)
$Button5 = New-Object System.Windows.Forms.Button
$Button5.Location = New-Object System.Drawing.Size(170,60)
$Button5.Size = New-Object System.Drawing.Size(75,23)
$Button5.Text = "Option 5"
$Button5.Name = "Button5"
$Button5.Add_Click({$x=$Button5.Text;$objForm.Close()})
$objForm.Controls.Add($Button5)
$Button6 = New-Object System.Windows.Forms.Button
$Button6.Location = New-Object System.Drawing.Size(170,84)
$Button6.Size = New-Object System.Drawing.Size(75,23)
$Button6.Text = "Option 6"
$Button6.Name = "Button6"
$Button6.Add_Click({$x=$Button6.Text;$objForm.Close()})
$objForm.Controls.Add($Button6)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(5,10)
$objLabel.Size = New-Object System.Drawing.Size(265,48)
$objLabel.Text = "Please click the appropriate button for your $ProcessName Process"
$objForm.Controls.Add($objLabel)
$objForm.Topmost = $True
$objForm.ActiveControl.Name="Button3" #Doesn't work
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Echo $objForm.ActiveControl.Name
$x
Any ideas? Am I approaching the use of my Form Object's ActiveControl form properties and methods incorrectly? Well, that's not the question, I know I'm doing something wrong. How should I approach this? |
|
|
|
|
leibowitz
 New Member Posts:15

 |
| 25 Jan 2012 04:50 PM |
|
I found that the command "$Button3.Select" will give the focus to that button. So, if you had, e.g. $Button1.Add_Click({$x=$Button1.Text;$Button2.Select()}) then the next button would be selected whenever the user clicked $Button1. As far as taking control of the right-arrow event, I didn't find a way to do that. It seems the right arrow tabs through the buttons as is when I run the app, no matter what I put in the keydown handler. However, there is probably some way to change this default behavior on the form..maybe take away the tab stop property or something like that.
|
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |