How to access different variables in different subs? - input

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'

Related

when the text in a textbox is equal to a certain word i need to value in the combo box to be saved for that text

I have orders in text files in the debug folder and when i type the name of the order in a text box it displays the order is a list box and there is a combo box underneath where i can change the status of the meal preparation (Being prepared, ready to deliver etc,). If i go back to that form and type in the same order name into the textbox i need to previous prepartion status to be already in the textbox. Thanks for any help!
Public Class frmOrderStatus
Private Sub btnStatus_Click(sender As Object, e As EventArgs) Handles btnStatus.Click
Dim sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
lstOrder.Items.Add(sr.ReadLine)
Loop
End Sub
Private Sub OrderStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstOrder.Items.Clear()
btnStatus.Enabled = False
ChangeStatus.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
strTxtOrderNum = txtOrderNum2.Text
btnStatus.Enabled = True
ChangeStatus.Enabled = True
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
strSaveStatus = ChangeStatus.SelectedIndex
End Sub
Private Sub ChangeStatus_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ChangeStatus.SelectedIndexChanged
End Sub
End Class
It recognizes the file; it just tells you it is in use. A Stream must be closed and disposed. I don't see the StreamReader even being closed let alone disposed. A `Using...End Using block will close and dispose of objects even if there is an error.
I just used a text file I happened to have to test.
Private strTxtOrderNum As String = "host"
Private Sub ReadFile()
Using sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine)
Loop
End Using
End Sub
Private Sub WriteFile()
Dim strSelectedItem = ComboBox1.Text
Using swVar As IO.StreamWriter = IO.File.AppendText(strTxtOrderNum & ".txt")
swVar.WriteLine(strSelectedItem)
End Using
End Sub

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 display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub

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

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub