Trouble with displaying numbers in labels. Visual Basic - vb.net

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.

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

why wont this visual basic script work?

i am getting really annoyed by this piece of code for visual basic
please can you help. i have looked on YouTube and everywhere else even the vb website!!! i would really appreciate it if someone could help me out
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Hi!!")
Timer1.Start()
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1)
If ProgressBar1.Value = 100 Then
Timer1.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
End Sub
Private Function GetNumericUpDown1(v As Integer) As Boolean
End Function
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ( : ( ) Then
MessageBox.Show("Well Done!")
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MessageBox.Show("!!Stopped!!")
Timer2.Stop()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ProgressBar2.Increment(0.6)
If ProgressBar2.Value = 100 Then
Timer2.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show("!!Started!!")
Timer2.Start()
End Sub
Private Sub SplitContainer1_Panel1_Paint(sender As Object, e As PaintEventArgs) Handles SplitContainer1.Panel1.Paint
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs)
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
ProgressBar3.Increment(1)
If ProgressBar3.Value = 100 Then
Timer3.Stop()
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer3.Start()
End Sub
Private Sub ProgressBar3_Click(sender As Object, e As EventArgs) Handles ProgressBar3.Click
End Sub
End Class
BTW:this is my first post so it is probably rubbish!!
I don't have enough points to comment.
First off, as others have said we can't recreate this since we have no idea what your Form looks like or what you are expecting it to do.
Secondly, you should turn on Option Strict in the Compile section of Project properties to avoid technical errors like ProgressBar2.Increment(0.6) since 0.6 isn't a valid integer.
I threw together a TabControl (which you never mentioned in the OP) with 3 tabs and the various buttons and progress bars you list in what seemed like a logical fashion to me and it ran just fine for what code you have. Clicked the button on each tab and each progress bar eventually got to 100%. I have no idea what else you were expecting.
If you remove all the empty subs and action listeners, it would be easier to detect the problem

Print Progress bar value to textbox

I'm trying to print a progressbar's percentage to a textbox. When ever I run my program, nothing appears in the textbox. This is my code:
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
ProgressBar1.Maximum = TextBox1.Text
End Sub
Help is very appreciated! Thank you.
Here is some code that i have written up for you to have a look at, it should lead you in the right direction and help you on your way :)
Imports System.ComponentModel
Public Class Form1
''This will display the information to the textbox and will also load a progressbar(you can change it to something else beside a textbox too eg label, windows form title and so on).
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = e.ProgressPercentage & "%"
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
''This is make the backgroundworker start at load up(change it to a button if need be
CheckForIllegalCrossThreadCalls = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
''This is the example that i created to show you how to set a task.
For i = 0 To 10000
TextBox1.Text = i
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
''once the task is complete it will show a messagebox and reset the progressbars value to 0 so its not full when the task is compelete.
MessageBox.Show("Completed")
ProgressBar1.Value = 0
End Sub
End Class
Let me know how you go, i live in a country where i cant access the website link that you posted.Happy Coding
UPDATE: do check out the backgroundworkers on google, there are a lot of tutorials to help you :)

vb.net Find and REMOVE a line in a textbox

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

Multiple Buttons keypad in VB.net

I have been battling with this for three days and asked a LOT of people the question, but maybe I'm asking it wrong. The project that I'm working , is to create a digital safe lock. I need to achieve the following:
I created a keypad with six buttons in a group box. When one click on the buttons, then the digit being clicked must be displayed in the listbox. As you press the rest (up to six digits max) the all have to be displayed in the listbox, next to each other in sequence like the way one will press the buttons on a calculator and then the listbox must display them in the same way. Here is whet I have figured out up to date:
Public Class Form1
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If pinlst.Items.Count = 0 Then
pinlst.Items.Add(btn1.Text)
Else
pinlst.Items(0).SubItems.Add(btn1.Text)
End If
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If pinlst.Items.Count = 1 Then
pinlst.Items.Add(btn2.Text)
Else
pinlst.Items(1).SubItems.Add(btn2.Text)
End If
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
If pinlst.Items.Count = 2 Then
pinlst.Items.Add(btn3.Text)
Else
pinlst.Items(2).SubItems.Add(btn3.Text)
End If
End Sub
Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
pinlst.Text = btn4.Text
End Sub
Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
pinlst.Text = btn5.Text
End Sub
Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
pinlst.Text = btn6.Text
End Sub
Private Sub btngp_Enter(sender As Object, e As EventArgs) Handles btngp.Enter
pinlst.Text = pinlst.Text & btn1.Text
pinlst.Text = pinlst.Text & btn2.Text
pinlst.Text = pinlst.Text & btn3.Text
pinlst.Text = pinlst.Text & btn4.Text
pinlst.Text = pinlst.Text & btn5.Text
pinlst.Text = pinlst.Text & btn6.Text
End Sub
End Class
It's best to assign every button to a character being pressed or a function being performed, in design time. For this to work, you need to have every button as a custom control, based off a regular button of course, exposing a property "CharPressed" or something like that. Then you can have a single static handler for all buttons, which you will wire dynamically in Sub New, after InitializeComponent. If this does not make sense, please feel free to ask a question in comments.