943,634 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2807
  • VB.NET RSS
Dec 22nd, 2008
-1

A small calculator in vb

Expand Post »
hi friends,
I have designed a simple calculator using vb.net I want to execute click event of button which labeled as "1", when I press key "1" on keyboard. please help me.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
thilinam is offline Offline
35 posts
since Sep 2008
Dec 22nd, 2008
1

Re: A small calculator in vb

Normal buttons require ALT-key to be pressed. If you don't mind this, name the buttons
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2. '
  3. Button1.Text = "&1"
  4. Button2.Text = "&2"
  5.  
  6. End Sub
and trap "number"-buttons
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
  2. Select Case CType(sender, Button).Text
  3. Case "&1"
  4. ' Number 1 pressed
  5. Case "&2"
  6. ' Number 2 pressed
  7. End Select
  8.  
  9. End Sub
If you do not want ALT-key to be used, you have to trap KeyUp or similar event and use some other control instead of Button control.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 24th, 2008
1

Re: A small calculator in vb

I assume you have your click event sub proceedure running, to add another event for a button say "button1" you have to make sure you have enable its 'tabstop' property and then you can add another event sub proceedure.

However for your calculator, when the tabstop gets to the 'btn1' button all the user has to do is to press the "enter key".

This is in simple context, however you may have to implement threading for complex scenario where you need responses. (My opinion)

I am leaving you with some code snippet for your review, it may help.

[CODE syntax=vb]

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

Dim keyasc As Short

If keyasc >= System.Windows.Forms.Keys.D1 And keyasc <= System.Windows.Forms.Keys.D9 Then
txtDisplay.Text = Val(btn1.Text)
Exit Sub
Else
MessageBox.Show("Error key pressed")

End If

End Sub

[/CODE]
Last edited by hakimkal; Dec 24th, 2008 at 2:14 am.
Reputation Points: 10
Solved Threads: 5
Newbie Poster
hakimkal is offline Offline
20 posts
since Dec 2007
Dec 24th, 2008
0

Re: A small calculator in vb

thanks Teme64
thanks for sharing your code with us i think it will defiantly useful for someone.good job..
Reputation Points: 20
Solved Threads: 1
Newbie Poster
gurshan is offline Offline
15 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: This Vb.Net code will copy a file to a location and its extension
Next Thread in VB.NET Forum Timeline: Dataset Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC