[VB.Net] How can I use KeyPress event and Resize my application while

How to use KeyPress event, How to resize the size of the program

Page 1 of 1

4 Replies - 2742 Views - Last Post: 20 August 2009 - 11:56 PM Rate Topic: -----

#1 FallenDreamL7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-August 09

[VB.Net] How can I use KeyPress event and Resize my application while

Posted 20 August 2009 - 10:58 PM

Hello,

I've tired to use KeyPress event while in focus of my program's window in VB.Net, but it doesn't work.

For example I wanted to press the number "2" anywhere while in focus of the application and as effect the pixels of the window will change from 200,200 to 600,600 (hortizontal and vertical)

Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

		Form1_KeyPress(Keys.D2)

		Dim size As Size
		size = Me.Size
		Me.Size = New Size(600, 600)

	End Sub



There seems to be a problem with the "Form1_KeyPress(Keys.D2)" part, what is the correct way to write the code for that?

Thank you very much for your help

This post has been edited by FallenDreamL7: 20 August 2009 - 11:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [VB.Net] How can I use KeyPress event and Resize my application while

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: [VB.Net] How can I use KeyPress event and Resize my application while

Posted 20 August 2009 - 11:07 PM

You're doing this wrong, you dont call the method from inside the method as you're doing. Try this

Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		If e.KeyCode = Keys.D2 OrElse e.KeyCode = Keys.NumPad2 Then
			 Dim size As Size
			 size = Me.Size
			 Me.Size = New Size(600, 600)
		End If
	End Sub



Also, make sure you have the forms KeyPreview Property set to True.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 FallenDreamL7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-August 09

Re: [VB.Net] How can I use KeyPress event and Resize my application while

Posted 20 August 2009 - 11:18 PM

Ah yes, I can see that code makes more sense.

However, I've tired the code you use and the "e.KeyCode" parts got highlighted in blue and it says...

"KeyCode" is not a member of 'System.Windows.Forms.KeyPressEventArgs'.

What seems to be that problem?
Thank you for your assistance.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: [VB.Net] How can I use KeyPress event and Resize my application while

Posted 20 August 2009 - 11:22 PM

That was my mistake, this works (I just tested it)

Private Sub Form1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Form1.KeyPress
	If e.KeyChar = CType(Keys.NumPad2, Char) OrElse e.KeyChar = CType(Keys.D2, Char) Then
		Me.Size = New Size(600, 600)
	End If
End Sub


Was This Post Helpful? 1
  • +
  • -

#5 FallenDreamL7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-August 09

Re: [VB.Net] How can I use KeyPress event and Resize my application while

Posted 20 August 2009 - 11:56 PM

Yes it works now, however I had to make a minor adjustment to it

If e.KeyChar = CType(ChrW(Keys.NumPad2), Char) OrElse e.KeyChar = CType(ChrW(Keys.D2), Char) Then


Thank you very much, I really appericiate your help.

This post has been edited by FallenDreamL7: 20 August 2009 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1