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
Related
Currently in the process of "self teaching" visual basic so apologies for the basic question
Maybe I am going about this the wrong way but I want to use btnTA_StartLog to create a file and activate Timer1 to log data in a specified interval. Problem being outlog is only defined in btnTA_startLog and not in the other 2 subs. How can I make the recently declared outlog public and accessible to the other subs?
Public Sub btnTA_StartLog_Click(sender As Object, e As EventArgs) Handles btnTA_StartLog.Click
Dim file As String = GetFileName(False, "csv", "Data Output")
Dim outlog As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(file, True)
Timer1.Start()
btnTA_StartLog.Enabled = False
btbTA_LoggingStop.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outlog.WriteLine(CStr(DateTime.Now) & "," & CStr(txbTA_Delivered_ml.Text) & "," & CStr(MyTC.Data.Temperature))
End Sub
Private Sub btbTA_LoggingStop_Click(sender As Object, e As EventArgs) Handles btbTA_LoggingStop.Click
Timer1.Stop()
outlog.Close()
End Sub
It has to be at the class level one way or another.
Private outlog As IO.StreamWriter
Public Sub btnTA_StartLog_Click(sender As Object, e As EventArgs) Handles btnTA_StartLog.Click
Dim file As String = GetFileName(False, "csv", "Data Output")
outlog = My.Computer.FileSystem.OpenTextFileWriter(file, True)
Timer1.Start()
btnTA_StartLog.Enabled = False
btbTA_LoggingStop.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outlog.WriteLine(CStr(DateTime.Now) & "," & CStr(txbTA_Delivered_ml.Text) & "," & CStr(MyTC.Data.Temperature))
End Sub
Private Sub btbTA_LoggingStop_Click(sender As Object, e As EventArgs) Handles btbTA_LoggingStop.Click
Timer1.Stop()
outlog.Close()
End Sub
I used a private field, but it could be a private property. It could be contained in another object for which you have class level reference to.
not a vb.net person so I dont know the exact syntax but the rules are the same for c# which I do know.
Define outlog at the form level, ie outside any functions, in that way all functions can use it.
I tried to solve the Visual Basic assignment in dart and I was able to using this :
I don't know if that's the best way to do it though. In Visual Studio I don't know what commands or methods that can help me do this and this is the result there.
Public Class Form1
Dim input As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Nested Loops"
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label.Click
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles InputTextBox.TextChanged
input = Val(InputTextBox.Text)
End Sub
Private Sub ComputeButton_Click(sender As Object, e As EventArgs) Handles ComputeButton.Click
For outer As Integer = 1 To input
For inner As Integer = 1 To outer
ListBox.Items.Add("#")
Next
ListBox.Items.Add("")
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox.SelectedIndexChanged
End Sub
End Class
I get this error when trying to treat it like dart
System.InvalidCastException: 'Conversion from string "#" to type 'Double' is not valid.'
I need help.
Consider:
For outer As Integer = 1 To input
Dim s as String
For inner As Integer = 1 To outer
s &= "#"
Next
ListBox.Items.Add(s)
Next
i.e. you want to have a string that grows by one # each time before you add it to the thing that will display it:
"#"
"##"
"###"
"####"
This uses simple string concatenation. In the future when you're programming for real (and e.g. wanting to run a process that creates millions of strings) you should consider using something designed for the job, such as a StringBuilder. This will be ok for some small handful of operations though
Though I'm rather puzzled why you use a listbox and not just a textbox..
I am currently enrolled in a visual basic class through my university and I am having trouble with one of the exercises assigned to me. I need to display the cost of an item for a company and I cannot get anything to display in a label.
This is the code
Option Explicit On
Option Strict On
Option Infer Off
Public Class MainForm
Private Sub BtnCalc_Click(sender As Object, e As EventArgs) Handles BtnCalc.Click
Dim intCost As Double
If RadTwin.Checked Then
intCost = 39.99
ElseIf RadFull.Checked Then
intCost = 49.99
ElseIf RadQueen.Checked Then
intCost = 49.99
ElseIf RadKing.Checked Then
intCost = 69.99
End If
lblCost.Text = intCost.ToString("n")
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub RadTwin_CheckedChanged(sender As Object, e As EventArgs) Handles RadTwin.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadFull_CheckedChanged(sender As Object, e As EventArgs) Handles RadFull.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadQueen_CheckedChanged(sender As Object, e As EventArgs) Handles RadQueen.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadKing_CheckedChanged(sender As Object, e As EventArgs) Handles RadKing.CheckedChanged
lblCost.Text = String.Empty
End Sub
End Class
I need to display the cost of each item in the lblCost when the radio button is chosen. I have worked at getting this to work for hours but I am not knowledgeable enough to know what to do. If anyone has any ideas on how to make this code work I would greatly appreciate it. It is likely something stupid simple that I am missing but I want to learn how to solve this problem if it happens again in the future.
I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class
when i try to show dialog of one of my forms it displays this error all other forms work perfectly i have tried to copy the code into another form happened the same
here is the error:
A first chance exception of type 'System.InvalidOperationException'
occurred in hyper market system.exe
Additional information: An error occurred creating the form. See
Exception.InnerException for details. The error is: Object reference
not set to an instance of an object.
If there is a handler for this exception, the program may be safely continued.
here is all my code
Public Class farm
Dim inifile As New IniFile(myfiles & "\system.ini")
Dim myfiles As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\HMsystem"
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Dim Count As Integer = 0
Dim total As Long = 0
Dim productnum As String = TextBox1.Text
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim productnum As String = TextBox1.Text
Dim num As String = TextBox2.Text
Dim itemname As String = inifile.GetString("productname", productnum, "غير موجود")
Dim price As String = inifile.GetString("productprice", productnum, "غير موجود")
ListView1.Items.Add(productnum)
ListView1.Items(Count).SubItems.Add(itemname)
ListView1.Items(Count).SubItems.Add(num)
ListView1.Items(Count).SubItems.Add(price)
total += price
Count += 1
Dim a As String = inifile.GetString("productquan", productnum, "0")
Dim itemquannow As String = inifile.GetString("productquan", productnum, "0")
If itemquannow <= 5 Then
Else
MsgBox("لم يبق الا 5 من هذا المنتج")
End If
inifile.WriteInteger("productquan", productnum, a - num)
MsgBox("تم الاضافة")
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label4.Text = total & " السعر النهائي"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListView1.Clear()
TextBox1.Clear()
TextBox2.Clear()
Count = 0
total = 0
MsgBox("تم الشراء بنجاح")
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
End Class
When you get an error message like that, i.e. "An error occurred creating the form", it almost always means the same basic issue: you have an event handler that is being raised because of a property value set in the designer and that event handler assumes that the user has made that change after the form has been displayed. For instance, if you set the Text property of a TextBox in the designer then that's going to raise the TextChanged event. If you have handled that event then your event handler is going to be executed during the initialisation of the form, before it has been displayed to the user. If you assume that, for instance, an item is selected in a ComboBox then you're in trouble because there will be no such selection.
As the error message states, look at the InnerException, which will tell you exactly where the original exception was thrown. That will tell you which event handler is the issue and you can then look at the code in that method and determine what would cause an issue if the form had not yet been displayed. If in doubt, update your question with the code of that event handler and tell us where the exception was thrown, which the stack trace will tell you.