how to put a number in a text box using a button in visual basic application forms - visual-studio-2010

I am trying to make a calculator on visual basic application forms and I have most of the codes sorted. How do I make it so when I press the number button, it puts the number in the text box. It also needs to be able to work so if I pressed 1 then 2 then 3, it appears a 123.

Just set the SelectedText() property of the TextBox to the Text() property of your button.
For example:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.SelectedText = Button1.Text
TextBox1.Focus()
End Sub
If you make all the buttons fire the same handler, then it becomes:
Private Sub AllButtons_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, _
Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button0.Click
Dim btn As Button = DirectCast(sender, Button)
TextBox1.SelectedText = btn.Text
TextBox1.Focus()
End Sub

Related

vb - edit specific cell on double click and set read only after

I am developing simple app in visual basic and what I want to do is:
On load whole DataGridView1 is ReadOnly
Next if user will double click on cell, then it turns edit mode for that cell
I try to do this by:
Private Sub CellDouble(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
DataGridView1(e.ColumnIndex, e.RowIndex).ReadOnly = False
DataGridView1.BeginEdit(e.RowIndex)
End Sub
But it doesnt even react. (function is triggered but code not working)
Final step is to Set Read only back to that cell after edit
I simply did:
Private Sub CellDouble(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
DataGridView1.BeginEdit()
End Sub
and it's working fine...

Add handler to runtime-created control

I found in StackOverflow some vb.net code written by "PaRiMaL RaJ". i want the same think but i must convert this code to work on VB6.
can you help me please???
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' creating control
Dim btn1 As Button = New Button()
Dim btn2 As Button = New Button()
btn1.Parent = Me
btn1.Name = "btn1"
btn1.Top = 10
btn1.Text = "Btn1"
btn2.Parent = Me
btn2.Name = "btn2"
btn2.Top = 50
btn2.Text = "Btn2"
'adding handler for click event
AddHandler btn1.Click, AddressOf HandleDynamicButtonClick
AddHandler btn2.Click, AddressOf HandleDynamicButtonClick
End Sub
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "btn1" Then
MessageBox.Show("Btn1 clicked")
ElseIf btn.Name = "btn2" Then
MessageBox.Show("Btn2 Clicked")
End If
End Sub
The easiest way is to use a control array. Add a button to your form in the designer and set its Index property to 0. You can hide the button if you don't want it to be visible.
Then, when you want to dynamically add more buttons at run-time, just use the Load statement.
For example, if your button was named Command1:
Load Command1(1) ' Create a new button
Command1(1).Move 50, 50, 1500, 500 ' Set its size and position
Command1(1).Caption = "New Button" ' Give it a caption
Command1(1).Visible = True ' Make it visible
The button will use the same event handlers as your original button:
Private Sub Command1_Click(Index As Integer)
' Print the caption of the clicked button...
Debug.Print Command1(Index).Caption
End Sub

make label update on file directory change (visual basic)

When I use an OpenFileDialog, and select a file and click open etc, etc...
I want a label on the same form to automatically update with the directory path. At the moment I have this working, However the label updates on click, not automatically on the directory change.
CODE:
Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
Label4.Text = OpenFileDialog2.FileName
End Sub
Cna I change the "Label4_Click" to something else for an auto update?
How are you displaying the OpenFileDialog? You need to update the Label from there!...
Something like:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
Label4.Text = OpenFileDialog2.FileName
End If
End Sub

visual basic 2010 copy paste subroutine

I am trying to write 2 simple subroutines in VB 2010 that could be used with toolstripbutton contorls for multiple textboxes in a form. I know simple copy paste could be done using the textbox1.Copy() and TextBox1.Paste() methodes. What i am trying to do is write a common subroutine which could be used on any textboxes in the form not just one particular textbox.
My codes are below, I know there are errors in it, just wondering how it could be achieved. Any help would be highly appreciated. Thanks.
Public Class Form1
Private Sub copytext()
Dim txt As Control
If TypeOf txt Is TextBox Then
Clipboard.Clear()
Clipboard.SetText(txt.SelectedText)
End If
End Sub
Private Sub pastetext()
Dim txt As Control
If TypeOf txt Is TextBox Then
txt.Text = Clipboard.GetText
End If
End Sub
Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
Call copytext()
End Sub
Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
Call pastetext()
End Sub
End Class

Enabling CTRL+Z property in masked edit textbox using VB.Net

I am using a masked edit text box in my windows application that was developed by using vb.net.
In normal text boxes (CTRL+Z- to revert back to original value) is working fine. But In case of Masked Edit Textboxes its not working fine.
Can any one please help me about this.
This ctrl+Z should provide the functionality as same as normal textbox.
You can use a variable to store the current text by programming the Leave event and check during the KeyDown event for the combination of Control+Z:
Dim oldText As String = ""
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MaskedTextBox1.KeyDown
If e.Control AndAlso e.KeyCode = Keys.Z Then MaskedTextBox1.Text = oldText
End Sub
Private Sub MaskedTextBox1_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MaskedTextBox1.Leave
oldText = MaskedTextBox1.Text
End Sub

Resources