Formating a random number in VB.NET - vb.net-2010

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim generator As New Random
Dim myRand As Integer
myRand = generator.Next(1000000000, 9999999999)
Label1.Text = ???
End Sub
This code generates a 10 digit number though it does not format the number they way i want.I want the format to be
123 456 7890
How could i achieve this?

You need to provide a **custom number format string to format the number as you want it. You can do so by passing a format string into the ToString method of every number.
For more on custom number formats go to:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
Try the following, this format works just fine in C#:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim generator As New Random
Dim myRand As Integer
myRand = generator.Next(1000000000, 9999999999)
Label1.Text = myRand.ToString("### ### ####")
End Sub

Related

Visual Basic Beginner ..SubStrings

I'm new to programming and I'm trying to figure out this simple question! The language is Visual Basic! The Question is below:
"Users of a computer program often like to enter numbers with commas inserted in the middle, such as "1,234,000,688". Most computer languages consider this format to be non numeric. Write a program that inputs a number containing no more than three commas, and produces a string containing the same number without the commas"
When I enter this number: 1,234,000,688 and hit Display in Visual Basic I get this error message --> Argument is out of range Exception was unhanded
I'm not exactly sure why this is happening because I'm within my strUserInput length.
My Code:
Public Class Form1
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Variable declarations
Dim strUserInput = txtUserInput.Text
Dim strOutputNumber1 As String
Dim strOutputNumber2 As String
Dim strOutputNumber3 As String
Dim strOutputNumber4 As String
' 1,234,000,688
strOutputNumber1 = strUserInput.Substring(0, 1)
strOutputNumber2 = strUserInput.Substring(2, 4)
strOutputNumber3 = strUserInput.Substring(5, 8)
strOutputNumber4 = strUserInput.Substring(9, 12)
lblDisplayNumber.Text = strOutputNumber1 & strOutputNumber2 & strOutputNumber3 & strOutputNumber4
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lblDisplayNumber.Text = String.Empty
txtUserInput.Text = String.Empty
End Sub
End Class
You're getting that exception because of the way you're using substring, the last one there starts at index 9 and extends 12 characters...that would be a 22 character number, and the text you have as input is probably much shorter. You don't need to make things overly complicated for yourself using substrings, all of the above code could be greatly condensed and cleaned up by simply using String.Replace like this:
Public Class Form1
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
lblDisplayNumber.Text = txtUserInput.Text.Replace(",", "")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lblDisplayNumber.Text = String.Empty
txtUserInput.Text = String.Empty
End Sub
End Class

I want to Increase employee Id as E1,E2,E3 and so on how to do this

I want to Increase employee Id as E1,E2,E3 and so on how to do this....Val(TextBox19.Text) + 1 I have done this to Increase 1,2,3 but how to append E at the start of every number in VB.net
I made this by going all sneaky about it...
But I think this is what you're looking for. With each click on the button, you get "E" + the incremented number.
Interested by the idea, I ended up on this for 5 hours :) (Nothing better to do)
Public Class Form1
'When Form1 loads.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'We hide TextBox1
TextBox1.Hide()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This will increment +1 in the hidden textbox.
Dim i As Integer
i = Val(TextBox1.Text) + 1
TextBox1.Text = i
'We dim the result of the hidden textbox as string, then finally connect "Add" with "Result"
Dim Add As String
Add = "E"
Dim Result As String
Result = TextBox1.Text
TextBox2.Text = Add & Result
End Sub
End Class

How to access different variables in different subs?

I've been trying to make a calculator on Visual Studio 2012 Ultimate. I've been trying to create different buttons, one asking for first number, second asking for second number, third asking for the operator and the last one displaying the answer in a message box. But whenever I try calling an Integer from another sub, it just doesn't work. I've tried making the sub Public. Here is my code :
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim prompt1, title1 As String
Dim a As Integer
prompt1 = "Enter First Number : "
title1 = "First Number"
a = InputBox(prompt1, title1)
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim prompt2, title2 As String
Dim b As Integer
prompt2 = "Enter Second Number : "
title2 = "Second Number"
b = InputBox(prompt2, title2)
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim prompt3, title3 As String
Dim op As String
Dim ans As Integer
prompt3 = "Enter Operator : "
title3 = "Operator"
op = InputBox(prompt3, title3)
If op = "+" Then
ans = a +
End If
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
End Sub
End Class
As you can see I've not completed it because I can't call a in the third button.
Any ideas ?
If I'm messing up, please point it out.
define variable a outside of button1
Public Class Form1
Dim a As Integer
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim prompt1, title1 As String
prompt1 = "Enter First Number : "
title1 = "First Number"
a = InputBox(prompt1, title1)
End Sub
Now you can use variable 'a'

How do I get the Word Variables to replace the number values in this ListBox Loop?

I have a school assignment that requires me to use a loop and some mathematical equations. What the project needs to accomplish is you type in a range at the top of numbers. Then in the text boxes below you type in a number and a word on each side. When you hit "Calculate" the program needs to replace all the MULTIPLES of the number you typed in with the WORD you typed below it. This should happen with both sets of numbers and words. Then if a number is a multiple of both it should display both words.
Here is a screen shot of my GUI to help you see what I mean.
This is the code so far and what it produces is a list or all the multiples just like I want...but it is just adding the word below the numbers.
How do I get it to replace the number with the word I want entered?
Thanks.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim counter As Integer
Dim intCounterHigh As Integer
Dim intCounterLow As Integer
Dim intMultipleLeft As Integer
Dim intMultipleRight As Integer
Dim strWordLeft As String
Dim strWordRight As String
intCounterHigh = txtCounterHigh.Text
intCounterLow = txtCounterLow.Text
strWordLeft = txtWordLeft.Text
strWordRight = txtWordRight.Text
intMultipleLeft = txtMultipleLeft.Text
intMultipleRight = txtMultipleRight.Text
While counter <= intCounterHigh - 1
counter += 1
lstResult.Items.Add(CStr(counter))
If counter Mod intMultipleLeft < 1 Then
lstResult.Items.Add(strWordLeft)
End If
If counter Mod intMultipleRight < 1 Then
lstResult.Items.Add(strWordRight)
End If
If counter Mod intMultipleRight < 1 And counter Mod intMultipleLeft < 1 Then
lstResult.Items.Add(strWordLeft & strWordRight)
End If
End While
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstResult.Items.Clear()
End Sub
Private Sub txtCounter_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCounterHigh.TextChanged
End Sub
End Class
I imagine that you've seen a bit more of data structures than what you show here. I'm not going to give you the full answer (that's not what we do here), but I will tell you that you already have the answer, you're just not seeing it. Let's approach this backwards, from most restrictive to least restrictive:
If counter is a multiple of both numbers, you print your two words.
If not, but counter is a multiple of the left number, you print the left word.
If not, but counter is a multiple of the right number, you print the right word.
If none of the above work, then you just print the number.
Those are the rules, right? Go forth, young grasshopper.

Inserting text after specific tag

I have a text file comprised of different tags. I am able to find out if a specific tag exists within the document using the following...
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String = IO.File.ReadAllText("C:\Example.xtp")
Dim index As Integer = text.IndexOf("<Tools>")
If index >= 0 Then
' String is in file, starting at character "<Tools>" insert text "TEST_HELLO"
End If
End Sub
End Class
however I want to also enter extra text after this tag when / if found
I am using VB.net
Try:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String = IO.File.ReadAllText("C:\Example.xtp")
Dim index As Integer = text.IndexOf("<Tools>")
Dim countChars as integer
countChars="<Tools>".Length
If index >= 0 Then
' String is in file, starting at character "<Tools>" insert text "TEST_HELLO"
text = text.Insert(index + countChars, "TEST_HELLO")
End If
End Sub
Edit
If you want to write the final text to a/the file , there are many ways. I just suggest one, but you need to search and read and find what is suitable for you:
Dim Writer As System.IO.StreamWriter
Writer = New System.IO.StreamWriter("C:\Textfile.txt") '<-- Where to write to
Writer.Write(text)
Writer.Close()