TextBox event that will Clear User input - vb.net

please am working on an Annual Crop Cashflow Calculator like an Excel
i have 3 text boxes.
textbox22 textbox31 and textbox71
if i multiply textbox22 by textbox31 it shows in textbox71
but the problem is i need an event jus like excel after calculating and user goes back to empty either textbox31 or textbox22 the answer in texbox71 should be empty...
but with what i have here d previous answer will still be there until user inputs another entry
solution please
below is my code
Private Sub TextBox71_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox22.TextChanged, TextBox31.TextChanged
If String.IsNullOrEmpty(TextBox22.Text) OrElse String.IsNullOrEmpty(TextBox31.Text) Then Exit Sub
If Not IsNumeric(TextBox22.Text) OrElse Not IsNumeric(TextBox31.Text) Then Exit Sub
TextBox71.Text = CDbl(TextBox22.Text) * CDbl(TextBox31.Text)
End Sub

You can add code to the Enter event that runs whenever the user enters a TextBox.
Private Sub MyTextBox_Enter(sender as object, e as EventArgs)
MyTextBox.Clear
SomeOtherTextBox.Clear
End Sub

Related

Visual Basic - Displaying a combo box entry over a textbox

to keep it short and simple, I am attempting to display a number selected through a combo box on a second form through a label.
Here are the bits that are relevant to the issue I am having:
for i = 1 To 31
cmb_days.Items.Add(i)
next
' Populating combo box
days = cmb_days.text
frm_result.lbl_renting = "Renting for " & days & " Days"
I have tried using other variants such as cmb_days.selecteditem to no avail
I am also kinda having issues with like telling my code to do things with whatever number is actually selected in the combo box, idk I am veryyyyy new
That looks more or less correct.. But the question is where your call to update the label occurs in your code...
This is a minimal working example, with just a ComboBox on a form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 31
ComboBox1.Items.Add(i)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Text = ComboBox1.Text
End Sub
End Class
The update of the label (in this case the text of the form, but it works exactly the same with a label on a different form) occurs in the SelectedIndexChanged event of ComboBox1.
If you copied your code straight from the project, you haven't had time to select anything yet, so the Text property will be empty.

how to set minimum limit of a textbox vb.net

I need to set a minimum length for a password that can be used in a textbox and then set a label which will say if it meets the minimum number of characters required. I know how to set the limit of characters, what I can't do is the part where it will show in a label as soon as I leave the textbox. I was thinking I need to use an event, like maybe Leave or LostFocus, but it's not working. Please help :(
Ok, There are plenty of ways to do what you want to achieve. I personally like to a separate subroutine; if you need to change one thing, you wont have to edit every single event that has the same codeFrom what I can understand, something like this should help get you on your way. Basically, we just setup a subroutine that will check to see if textbox1.text's length is more than five and we trigger the subroutine by using events such as a button click of if the textbox is clicked off.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''save button
checkPassword(TextBox1.Text)
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
checkPassword(TextBox1.Text)
End Sub
Private Sub checkPassword(password As String)
If Not password.Length > 5 Then
Label1.Text = "The password must be more than 5 charcharacters"
TextBox1.Clear()
Else
Label1.Text = "Password accepted"
End If
End Sub

Avoid some functions/events to run when one function/events is clicked or executed

im developing a system using vb.net and i have some question about datagridview function. Is it possible in gridview that when i click the rowheader function it will not execute the cell enter function of it?
Because my problem is in my cell enter event/function there is a code that will show some text box if the user enter on the 1st cell. and i want that when i click the row header of my grid view this cell enter event/function will not be executed. is it possible guys? give me some tips or tricks on how to do it.
i've also done trying like this
Private sub gridview_RowHeaderMouseClick . . .
textbox.visible = false
gridview.endEdit(true)
end sub
the result is okay but it is not good for the client view and i want to improve it more.
This is my code.
Private Sub dgCharges_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgCharges.CellEnter
if e.ColumnIdex >= 0 Then
if e.ColumnIndex = 5 Then
'Show Textbox.
End If
End if
End Sub
Private Sub dgCharges_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgCharges.RowHeaderMouseClick
'Textbox.visible = false //this line i've use to hide the textbox when user
clicked row header.
dgCharges.EndEdit(True)
End Sub
Just put a test in the CellEnter function that only executes the restricted part of the code if the column index >= 0. The header has index -1
Private Sub DataGridView1_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If e.ColumnIndex >= 0 Then
'Do my stuff
End If
End Sub
------ EDIT ----
To prevent the datagridview cell editors from popping up just set the column to readonly. Alternatively for more control use
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridViewMsg.CellBeginEdit
'Test your criteria
If shouldBeReadonly Then
e.Cancel = True
End If
End Sub

Visual Studio Button code error

This is my first time using visual studio and I'm running into an error with my tax calulator application.
The problem is that visual studio is saying that the multiplication line using "*" and "+" can not be done due to the variables being text boxes. So my question to the community, is should I change the text box to something else? Or where in my code did I mess up.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale) And IsNumeric(txtSalesTaxRate)) Then
lblTax = txtSale * txtSalesTaxRate
lblTotal = txtSale + lblTax
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
If you need me to give you the full layout of my application, do ask.
I can see multiple problems in your code. I will explain how the "vs" works.The textboxes that you've made are controls.You can't just take their value so simple.They have many property and one of them is .text which allows you to take the value inside them.Another mistake you've made is what you've tried to do with the textboxes.What you type in those textboxes is ..well text. The program can't tell if the value inside is a number or just text. You must convert that value in a number using Cint.So you're code will look like this:
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
lblTax.text= cint(txtSale.text) * cint(txtSalesTaxRate.text)
lblTotal.text= cint(txtSale.text) + cint(lblTax.text)
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
What cint does is to convert every type of data to integer. Also the .text property lets you to set the value of the control (in our case the label caption)
I tried Electric-web's code and ran into a problem with the output. I changed "CInt" to "CDbl" and the tax calculator worked.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
lblTax.text= cdbl(txtSale.text) * cdbl(txtSalesTaxRate.text)
lblTotal.text= cdbl(txtSale.text) + cdbl(lblTax.text)
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class

How to set the textbox click event so I can enter numbers to the textbox from an onscreen keyboard? [duplicate]

This question already has answers here:
how to set a code so that mouse is clicked in the textbox to enter info? [closed]
(2 answers)
Closed 9 years ago.
In Visual Basic 2010. I have two textbox and an onscreen number keyboard. Every time I click a number, the number is shown in both the textboxes. How do I make it so I need to click into the textbox first then the number is only entered into that textbox?? This is what I have in my code and it is not working. The numbers I tried to enter to the mtbNum textbox and txtQuantity1 is only shown mtbNum textbox no matter which textbox I clicked on. My professor said to change the click event of the textboxes to textbox_click. However I don't know where to go from there. Can someone please help me? How do I change it so that the textbox has to be clicked for the number to be entered? Please and thank you.
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If mtbNum.Focus = True Then
mtbNum.Text += "5"
ElseIf txtQuantity1.Focus = True Then
txtQuantity1.Text += "5"
Else
End If
End Sub
Private Sub mtbNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mtbNum.Click
End Sub
Private Sub txtQuantity1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQuantity1.Click
End Sub
not clear sure what you want, but I think you are looking for this. I'll delete this if this is not what you are looking for.
solution 1:
if txt1.setFocus=true then
'do the button code
else if txt2.setFocus=true then
'do button code
end if
solution 2:
*better yet, have a checkBox so that the user could clearly choose which textbox he/she will enter, say :
if chk1.checked = true then
txt1.setFocus
'do button code
else if chk2.checked = true then
txt2.setFocus
'do button code
end if