automatic computaion - vb.net-2010

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
If String.IsNullOrEmpty(TextBox1.Text) OrElse String.IsNullOrEmpty(TextBox2.Text) Then Exit Sub
If Not IsNumeric(TextBox1.Text) OrElse Not IsNumeric(TextBox2.Text) Then Exit Sub
TextBox3.Text = CDbl(TextBox1.Text) + CDbl(TextBox2.Text)
End Sub
This code works for me but when i delete both value of the textbox the total is still there... any solution for my problem?

You have no code to remove the value from the output TextBox (TextBox3) when the input TextBoxes (TextBox1 and TextBox2) are cleared. You could just clear TextBox3 at the start of the TextChanged event handler.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
TextBox3.Text = ""
If String.IsNullOrEmpty(TextBox1.Text) OrElse String.IsNullOrEmpty(TextBox2.Text) Then Exit Sub
If Not IsNumeric(TextBox1.Text) OrElse Not IsNumeric(TextBox2.Text) Then Exit Sub
TextBox3.Text = (CDbl(TextBox1.Text) + CDbl(TextBox2.Text)).ToString
End Sub

Related

How to put many Textbox in one If else statement

This is the question
=If the FN button
will be click, then it will display a message base on the status of the textbox1 control. For the MN button, if
the event is click then it will display a message base on the status of the textbox2 control. The LN button
was clicked then it will display a message base on the status of the textbox3 control. Lastly, if the Clear
button control will be click and any of the textbox control is not empty then it will clear all the textbox
controls, but if all textbox controls are empty then it will show a message that all textbox controls are empty.
this is my code
Public Class Form1
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Label1.Text = "First Name"
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
Label2.Text = "Middle Name"
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
Label3.Text = "Last Name"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Text = "FN"
If TextBox1.Text = vbNullString Then
MessageBox.Show("The First name is empty")
Else MessageBox.Show("The First name is " & TextBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Text = "MN"
If TextBox2.Text = vbNullString Then
MessageBox.Show("The Middle name is empty")
Else : MessageBox.Show("The Middle name is " & TextBox2.Text)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Button3.Text = "LN"
If TextBox3.Text = vbNullString Then
MessageBox.Show("The Last name is empty")
Else : MessageBox.Show("The Last name is " & TextBox3.Text)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Button4.Text = "Clear"
If (TextBox1.Text) And (TextBox2.Text) = vbNullString Then
MessageBox.Show("Textbox control is empty")
Else : TextBox1.Clear()
End If
For Each txt As Control In Me.Controls.OfType(Of TextBox)()
txt.Text = ""
Next
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
End Class
My question is , how to work on clear button because i cant do it in Textbox2 and Textbox3 thankyou
Try something like this
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
Try to add Next txt to your For Each
So, you are asking about Button4_Click.
This is what I was about to suggest first, because clearing all text boxes whether they are empty or not, is faster than first checking and then clearing.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Button4.Text = "Clear"
' If (TextBox1.Text) And (TextBox2.Text) = vbNullString Then
' MessageBox.Show("Textbox control is empty")
' Else : TextBox1.Clear()
' End If
For Each txt As Control In Me.Controls.OfType(Of TextBox)()
txt.Text = ""
Next
MessageBox.Show("Textbox controls are empty")
End Sub
But then, if the intention is to a) just clear all textboxes, without any message if any one has some text. b) show the message only if all are empty.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Button4.Text = "Clear"
If (TextBox1.Text = vbNullString) And (TextBox2.Text = vbNullString) And (TextBox3.Text = vbNullString) Then
MessageBox.Show("Textbox controls are empty")
Else
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
or (if this works, VB6 is alien for me)
....
Else
For Each txt As Control In Me.Controls.OfType(Of TextBox)()
txt.Text = ""
Next
End If
End Sub
I'd use a List(Of TextBox) allowing you to leverage All and ForEach:
Public Class Form1
Private TBs As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TBs.AddRange({TextBox1, TextBox2, TextBox3})
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If TBs.All(Function(tb) tb.Text = "") Then
MessageBox.Show("All TextBoxes are empty!")
Else
TBs.ForEach(Sub(tb) tb.Clear())
End If
End Sub
End Class
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Button4.Text = "Clear"
If (TextBox1.Text = vbNullString) And (TextBox2.Text = vbNullString) And (TextBox3.Text = vbNullString) Then
MessageBox.Show("Textbox controls are empty")
Else
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
thanks sir Tom Brunberg, this code works for me :)

AllTextBox event in TextChanged

Hello This is a code of mine
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = Val(TextBox1.Text)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox2.Text = Val(TextBox2.Text)
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
TextBox3.Text = Val(TextBox3.Text)
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
TextBox4.Text = Val(TextBox4.Text)
End Sub
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
TextBox5.Text = Val(TextBox5.Text)
End Sub
Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
TextBox6.Text = Val(TextBox6.Text)
End Sub
How do i make a short code for all textboxes ?
I try with this code i didn't work
Dim i As integr
TextBox(i).text = Val(TextBox(i).text)
Thanks for any help
Use one handler for all:
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged, _
TextBox4.TextChanged, _
TextBox5.TextChanged, _
TextBox6.TextChanged
Dim txt = DirectCast(sender, TextBox)
txt.Text = Val(txt.Text)
End Sub
You only need to create one TextChanged handler and then bind all your Texbox events to it.
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, <...for all your textboxes ..>
Dim target = DirectCast(sender, TextBox)
target.Text = Val(target.Text)
End Sub
If you want to bind all textboxes in code, you can do this programatically once your form has loaded:
Dim allTextBoxes = Me.Controls.OfType(Of TextBox)()
For Each textbox In allTextBoxes =
AddHandler textbox.TextChanged, AddressOf TextBox_TextChanged
Next
In addition to Murray Foxcroft's answer. You could dynamically create the Handler for the textboxes and add the subroutine for their code in one by doing
Dim textboxes = Me.Controls.OfType(Of TextBox)()
For Each txt in textboxes
AddHandler textbox.TextChanged, Sub(sender As Object, e As EventArgs)
... (using sender to find out which textbox sent the command)
End Sub
Next

cursor is not at end of text after routine adds 1 char

I type a couple of letters in the textbox in vb.net and then hit the up arrow. This adds an accented e to the textbox but the cursor is not at end of text.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Up Then
MessageBox.Show("Up arrow key")
TextBox1.Text = TextBox1.Text & "é"
TextBox1.SelectionStart = TextBox1.Text.Length + 1
End If
End Sub
It is a very strange behaviour but it seems than vb.net is not updating the positions until TextBox1_KeyDown has finished.
What I propose until anyone propose something better is starting a timer and doing the updating of the SelectionStart in the Timer.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Up Then
MessageBox.Show("Up arrow key")
TextBox1.Text = TextBox1.Text & "é"
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
And by the way, you do not need the +1 in TextBox1.SelectionStart = TextBox1.Text.Length + 1

combobox multiple thread error

I have a problem with my code. I keep getting Multiple thread Error with backgroundworker, because of the combobox item display. Please look at my code below its a very simple code which I am planning to use on big scale, all I want it to do is "If item "1" selected show item "1" in label1. I can only assume that problem exists because Combobox runs in different thread....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.runworkerasync()
BackgroundWorker1.WorkerReportsProgress = True
Me.Cursor = Cursors.WaitCursor 'Cursor changes to wait
End Sub
Public Structure controlwithtext
Public controlname As Control
Public text As String
Public Sub New(ByVal ctrl As Control, ByVal text As String)
Me.controlname = ctrl
Me.text = text
End Sub
End Structure
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If comboBox1.SelectedItem = "1" then
BackgroundWorker1.ReportProgress(5, New controlwithtext(Label1, ComboBox1.SelectedItem))
End If
End Sub
Private Sub SetBackgroundWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If TypeOf e.UserState Is controlwithtext Then
Dim cwt As controlwithtext = CType(e.UserState, controlwithtext)
cwt.controlname.Text = cwt.text
End If
End Sub
Here's an example of how to read from and write to controls from the BackgroundWorker thread:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While True
System.Threading.Thread.Sleep(250)
Dim selection As String = Me.Invoke(Function()
If Not IsNothing(ComboBox1.SelectedItem) Then
Return ComboBox1.SelectedItem.ToString
Else
Return String.Empty
End If
End Function).ToString
If selection = "1" Then
Me.Invoke(Sub()
Label1.Text = ComboBox1.SelectedItem.ToString
End Sub)
Else
Me.Invoke(Sub()
Label1.Text = "something else"
End Sub)
End If
End While
End Sub

VB program, looping for iterations and excel writing

I'm writing a program for an internship and need some advice. I've done my research but have mostly returned fruitless... I need to loop the "buttonOneClick for one second iterations. The program will send a "P" character, wait one second, send a p, wait one second, etc... Also I need to write the information it receives to an excel spreadsheet. Any help/critiquing of existing code would be greatly appreciated.
Here's what I have:
Public Class Form2
Dim buttonOnePush As Boolean = False
Dim buttonTwoPush As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Send strings to a serial port.
Using com5 As IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort("COM5")
com5.WriteLine("P")
End Using
End Sub
Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""
Dim com5 As IO.Ports.SerialPort = Nothing
Try
com5 = My.Computer.Ports.OpenSerialPort("COM5")
com5.ReadTimeout = 10000
Do
Dim Incoming As String = com5.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
Catch ex As TimeoutException
returnStr = "Error: Serial Port read timed out."
Finally
If com5 IsNot Nothing Then com5.Close()
End Try
Return returnStr
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If IsNumeric(TextBox1.Text) AndAlso IsNumeric(TextBox2.Text) Then
TextBox1.Text = CDec(TextBox2.Text)
End If
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
If IsNumeric(TextBox6.Text) AndAlso IsNumeric(TextBox3.Text) Then
TextBox6.Text = CDec(TextBox3.Text)
End If
End Sub
Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
If IsNumeric(TextBox7.Text) AndAlso IsNumeric(TextBox4.Text) Then
TextBox7.Text = CDec(TextBox4.Text)
End If
End Sub
Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
If IsNumeric(TextBox8.Text) AndAlso IsNumeric(TextBox5.Text) Then
TextBox8.Text = CDec(TextBox5.Text)
End If
End Sub
Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
If IsNumeric(TextBox15.Text) AndAlso IsNumeric(TextBox16.Text) Then
TextBox15.Text = Hex(TextBox16.Text)
End If
End Sub
Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
If IsNumeric(TextBox14.Text) AndAlso IsNumeric(TextBox11.Text) Then
TextBox14.Text = Hex(TextBox11.Text)
End If
End Sub
Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
If IsNumeric(TextBox13.Text) AndAlso IsNumeric(TextBox10.Text) Then
TextBox13.Text = Hex(TextBox10.Text)
End If
End Sub
Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
If IsNumeric(TextBox12.Text) AndAlso IsNumeric(TextBox9.Text) Then
TextBox12.Text = Hex(TextBox9.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
buttonTwoPush = True
buttonOnePush = False
Me.Close()
Form1.Close()
End Sub
End Class
Use a Timer to issue that command at one second intervals. Drag a Timer over to your form from the toolbox, and double click on it to get a _Tick method.
Set the .Interval member of the timer in your form's constructor, and use the .Start and .Stop methods to control it.
For the Excel piece, you'll need to add a reference to the project for the Microsoft Excel 12.0 (or 14.0 if you have Excel 2010) Object Library. Find this under the COM tab of the Add Reference dialog which you get by right clicking on the project in the Solution Explorer. See this page for an exhaustive reference (scroll down to the bottom of the page for a quick example in VB.NET).