How to display value from TextBox into a MessageBox in VB.NET? - vb.net

I honestly don't know what I'm doing wrong. I have a textbox named "txtNumSticks" where the user enters a number. After the user hits start, I want a message box to pop up that says "Okay! We'll play with (x) sticks!" But I can't get it to work. First day learning VB.net. Thanks in advance!
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim NumSticks As String
txtNumSticks.Text = NumSticks
Game.Show()
Me.Close()
MessageBox.Show("Okay! We'll play with " & NumSticks & "sticks!")
End Sub

You are setting the variable the wrong way around you should be assigning NumSticks to the value in the text box so:
NumSticks = txtNumSticks.Text
or alternatively without the use of a variable
MessageBox.Show("Okay! We'll play with " & txtNumSticks.Text & "sticks!")

You may want to add a little bit of error checking in your program to make sure your value entered is Numeric.
Dim NumSticks As String
NumSticks = txtNumSticks.Text.ToString
If IsNumeric(NumSticks) Then
Game.Show()
MessageBox.Show("Okay! We'll play with " & NumSticks & " sticks!")
Me.Close()
Else
' Let user know the value is non-numeric
MessageBox.Show("Non Numeric Value entered", "Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

Related

How to read contents from a file, do some processing, then write to a text box

It's been nearly 20 years since I last used VB.NET and I've forgotten everything :( My project:
Read the contents of a text file and write to a text box
If the line contains a specified string, write to a second text box
Repeat until end of file
What I have so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.SourceText.LoadFile("e:\xdcc.txt", RichTextBoxStreamType.PlainText)
Dim Term As String = "Santa"
For Each Line As String In IO.File.ReadLines("e:\xdcc.txt")
If Line.Contains(Term) = True Then
OutputText.Text = (Line) & vbNewLine
End If
Exit For
Next
End Sub
Problem:
I can read the contents to the first text box and I can output a matching line to the second text box. It's not parsing the rest of the file and writing all the subsequent matches.
Thank you
I believe there are two mistakes here:
first, your Exit For statement is causing the For loop to end on the first iteration;
Second, as I understood in your question you want to show in the text box all of the matches found inside the file. You need to concatenate the 'Current text' with the next match.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.SourceText.LoadFile("e:\xdcc.txt", RichTextBoxStreamType.PlainText)
Dim Term As String = "Santa"
For Each Line As String In IO.File.ReadLines("e:\xdcc.txt")
If Line.Contains(Term) = True Then
'OutputText.Text = (Line) & vbNewLine
OutputText.Text &= (Line) & vbNewLine
End If
'Exit For
Next
End Sub
That might solve your problem.
Remove the Exit For. that is causing the loop to end after the first round about
Think I figured it out. I removed that Exit For statement and in the If section I changed it to:
OutputText.Text = OutputText.Text & (Line) & vbNewLine
I now get the results that I was looking for

VB.net add line break after each loop

So I have been experimenting with VB.net (Windows Forms) to create a simple Ping Test app which pings the selected server and returns the elapsed time. I have declared a function to ping the address and then use a button to ping the server. The problem is that each time it pings, it only pings once and thus gives only one value. I would like to have a separate text box where the user enters the number of times they would like to ping the server for more accurate results.
Public Class Form1
Public Function Ping(ByVal server As String) As String
Dim s As New Stopwatch
s.Start()
My.Computer.Network.Ping(server)
s.Stop()
Return s.ElapsedMilliseconds.ToString
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim number As Integer = TextBox3.Text
For i = 1 To number
Try
TextBox1.Text = Ping(TextBox2.Text) & Environment.NewLine
Catch
MsgBox("Please enter a valid address", MsgBoxStyle.Critical, "Error")
End Try
Next
End Sub
End Class
I have tried using a loop to repeat the process and then return the results to a multi-line textbox for the user. Unfortunately it still only pings the address once and doesn't continue unless the ping button is clicked again, but then the first value is replaced by the next one. I believe the cause of the problem is that there should be a line break after each loop and I have tried using Enviroment.Newline but the problem persists. Any suggestions?
At the end I also would like to calculate the average ping of the results and expect to add all the ping values and divide by the number of times pinged. How would I get the results of the pings and add them?
Any help appreciated and excuse any spelling/grammatical errors.
In each loop you change the value of TextBox1 instead of appending the new value to the existing.
Optionally, you could also clear TextBox1 before starting the loop.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim number As Integer = TextBox3.Text
' Clear previous results
TextBox1.Text = String.Empty
For i = 1 To number
Try
' Append new result to existing ones
TextBox1.Text.AppendText(Ping(TextBox2.Text) & Environment.NewLine)
Catch
MsgBox("Please enter a valid address", MsgBoxStyle.Critical, "Error")
End Try
Next
End Sub

Change date format in code only

I've a code that simply creates a folder in a directory and gives it a name based on the values of a datetimepicker and a text box.
The date picker is displayed on the form as "16 October 2013" (how I want to keep it) but when I generate the file name I would lime the date to read in the format "161013"
The code I'm using is below if that helps
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lMailbox As String
lMailbox = t2.Text & "-" & d1.Text
' Check if folder exists, if not: create it
If Not Directory.Exists(nMailbox & lMailbox) Then
Directory.CreateDirectory(nMailbox & lMailbox)
' Folder created message
MessageBox.Show("Mailbox created!", "Lynx Control Panel", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
' Folder already exists
MessageBox.Show("Mailbox already exists!", "Lynx Control Panel", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If
End Sub
The nMailbox & lMailbox are declared at the top of the code page
d1 is the name of the datepicker
I'm very new to VB.net and would appreciate any help
Thanks
try this:
lMailbox = t2.Text & "-" & d1.Value.ToString("ddMMyy")
The format options are the same as for DateTime string formatting. Use Value of the DTP rather than the Text which is formatted otherwise/

validating alphanumeric input in textbox [VB2010]

im newbie here, im using vb2010, i just need some help guys.
here's my problem.
i want to validate user's input on my textbox, when user input like this "1a1:b2b:3c3", my project should accept it. but when user input like this "1a1b2b3c3", it will show a msgbox that the format must be "XXX:XXX:XXX".thanks for help in advance.
I done up a very quick example for you, more than enough to get you on the right track. I could have done it a different way, but I am sure this will get you going. I used MaxLength to determine that the user input at least 9 characters and if not let them know. I also made a function that passes the textbox's text into this and will go ahead and format it for you; saves the user time... besides we just need to make sure user primarily enters at least 9 characters anyways if I am correct... Good Luck!
Public Class Form1
Private strValidatedText As String = String.Empty
Private blnValid As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Make sure user can only enter up to 9 values...
With txtInput
.MaxLength = 9
.TextAlign = HorizontalAlignment.Center
End With
End Sub
Private Sub btnValidate_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
Dim strTextBox As String = txtInput.Text
strValidatedText = ValidateText(strTextBox)
Select Case blnValid
Case True
MessageBox.Show("It's valid! " & strValidatedText)
txtInput.Clear()
txtInput.Focus()
Case Else
MessageBox.Show(strValidatedText)
txtInput.Clear()
txtInput.Focus()
End Select
End Sub
Private Function ValidateText(ByVal strText As String)
Dim strNewText As String = String.Empty
If strText.Length = 9 Then
strNewText = (strText.Substring(0, 3) & ":" & strText.Substring(3, 3) & ":" & strText.Substring(6, 3))
blnValid = True
Else
strNewText = "There must be at least 9 characters in the textbox!"
blnValid = False
End If
Return strNewText
End Function
End Class
Also at that point in the "Select Case blnValid", you can do what ever you would like with that string because it's global...
MrCodeXeR
I would suggest you to use MaskedTextBox class, it will help you to take a formatted input from user. Have a look at this example.
I tried it with following code and it works fine in VB 2010. Just use this code before your variable declaration:
If TextBox1.Text = "" Then 'check if the textbox has a value
MsgBox("Please Enter ID Number")
Return 'will return to the app
ElseIf Not IsNumeric(TextBox1.Text) Then 'check if the entered value is a number
MsgBox("ID Must Be A Number")
Return

Having trouble with the messagebox.show

I'm having trouble with the messagebox.show. I want the answer and this line "would you like to try another temp conversion?" to appear on a message box with yesno buttons.
Public Class Form1
Dim intFah As Windows.Forms.DialogResult
Private Sub BtnFah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFah.Click
Try
Dim intFah As Integer
intFah = CInt(TxtBoxTemp.Text)
intFah = (intFah * 9) / 5 - 32
MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", MessageBoxButtons.YesNo)
Catch
MessageBox.Show("Would you like to start another temp conversion?", "System Error", MessageBoxButtons.YesNo)
End Try
End Sub
End Class
Your code didn't build for me - it's probably because in your MessageBox.Show:
MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", MessageBoxButtons.YesNo)
You aren't passing the right number of parameters. As per the link, it needs the text, a caption (title) and then the button options
If I change it to:
MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", "A Caption", MessageBoxButtons.YesNo)
Then it builds and when I run the app I can get the answer appearing in one line and the text on a second line as per what you're looking for.