Vb + Custom Url with parameters - vb.net

How i do to custom a url with parameters ?
In my example i have 3 parameters
Textbox1.Text = "Ikea"
Textbox2.Text = "Table"
Textbox3.Text = "Black"
The final url is need to be like:
https://localhost/objects?color=Black&manufacturer=Ikea&type=Table
in Textbox4.Text.
So how i do to add the previous values of the previous Textbox ?
Currently i have that :
Private Sub FinalScrapper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Ikea"
TextBox2.Text = "Table"
TextBox3.Text = "Black"
TextBox4.Text = ("https://localhost/objects?color=Black&manufacturer=Ikea&type=Table")
'The correct parameters are something like that ?
'TextBox4.Text = ("https://localhost/objects?color=TextBox3.Text&manufacturer=TextBox1.Text&type=TextBox2.Text")
End Sub
Thanks you.

I used Interpolated Strings as suggested in the comments and the following worked for me:
Private Sub FinalScrapper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Ikea"
TextBox2.Text = "Table"
TextBox3.Text = "Black"
Dim manufacturer = TextBox1.Text
Dim type = TextBox2.Text
Dim color = TextBox3.Text
Dim s1 = $"https://localhost/objects?color={color}&manufacturer={manufacturer}&type={type}"
TextBox4.Text = s1
End Sub

Related

Move to next record based on specific value in textbox

I am in need of some help. I want to be able to go through my records using the next and previous buttons on my form using a dynamic
values.
The code I currently have does moves to next record but from the starting value, however I want to be able to start from a specific value for example, if I type 100
into textbox and click next it should display records for 101, 102 and so forth with each click.
How do I accomplish this. Many thanks
This is the current code i have at the moment.
Dim intcurrentindex As Integer = 0
If intcurrentindex < ds.Tables(0).Rows.Count - 1 Then
intcurrentindex = intcurrentindex - 1
TextBox1.Text = ds.Tables(0).Rows(intcurrentindex).Item("NAME").ToString
TextBox2.Text = ds.Tables(0).Rows(intcurrentindex).Item("CODE").ToString
TextBox3.Text = ds2.Tables(0).Rows(intcurrentindex).Item("STOCKNAME").ToString
TextBox4.Text = ds.Tables(0).Rows(intcurrentindex).Item("WEIGHT").ToString
TextBox5.Text = ds.Tables(0).Rows(intcurrentindex).Item("LOCATION").ToString
End If
maybe something like this:
Public Class Form1
Private intcurrentindex As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TxtCurrentIndex.Text = 0
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
If TxtCurrentIndex.Text = "" Then
intcurrentindex = 0
Else
'set intcurrentindex to textbox value
intcurrentindex = TxtCurrentIndex.text
End If
If intcurrentindex < ds.Tables(0).Rows.Count - 1 Then
TextBox1.Text = ds.Tables(0).Rows(intcurrentindex).Item("NAME").ToString
TextBox2.Text = ds.Tables(0).Rows(intcurrentindex).Item("CODE").ToString
TextBox3.Text = ds2.Tables(0).Rows(intcurrentindex).Item("STOCKNAME").ToString
TextBox4.Text = ds.Tables(0).Rows(intcurrentindex).Item("WEIGHT").ToString
TextBox5.Text = ds.Tables(0).Rows(intcurrentindex).Item("LOCATION").ToString
intcurrentindex = intcurrentindex + 1
End If
End Sub
Private Sub BtnPrevius_Click(sender As Object, e As EventArgs) Handles BtnPrevius.Click
If TxtCurrentIndex.Text = "" Then
intcurrentindex = 0
Else
'set intcurrentindex to textbox value
intcurrentindex = TxtCurrentIndex.text
End If
If intcurrentindex > 0 Then
TextBox1.Text = ds.Tables(0).Rows(intcurrentindex).Item("NAME").ToString
TextBox2.Text = ds.Tables(0).Rows(intcurrentindex).Item("CODE").ToString
TextBox3.Text = ds2.Tables(0).Rows(intcurrentindex).Item("STOCKNAME").ToString
TextBox4.Text = ds.Tables(0).Rows(intcurrentindex).Item("WEIGHT").ToString
TextBox5.Text = ds.Tables(0).Rows(intcurrentindex).Item("LOCATION").ToString
intcurrentindex = intcurrentindex - 1
End If
End Sub
End Class
you need a variable to store current index
Thank you guys for your help on this. I used binding source in the end to get it to work.

How to divide and get a whole number answer only in visual basic

Im making a math game atm, in the division section i need to make sure the question the computer asks is dividable and not give 2 random numbers which will result in a decimal. ie questions like 13/5 wont come up. How do i do that. Sorry if my indentation isnt correct im new to this interface.
Public Class Division
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 = Rnd1.Next(20, 40)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Result0 / Result1
Dim Score As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
TextBox1.Text = Result0
TextBox2.Text = Result1
Label3.Text = Total
Label4.Text = Score
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 = Rnd1.Next(20, 40)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Result0 / Result1
If TextBox3.Text = Label3.Text Then
Score += 1
TextBox3.Text = ""
Else
MessageBox.Show("Incorrect")
TextBox3.Text = ""
End If
TextBox1.Text = Result0
TextBox2.Text = Result1
Label3.Text = Total
Label4.Text = Score
End Sub
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Button1.PerformClick()
End If
End Sub
You can use cint
Example
Dim ans as integer = cint(ans1/ans2)
Try and check it.

Counter using KeyPress events

First time posting here, although I'm a frequent visitor when I'm looking for answers. I am new to VB and programming.
My problem is this. I had this figured out in VBA, but I want to convert my "program" to a standalone executable with VB (using Visual Studio 2015)
I want to keep track of certain keypresses done on a textbox, and so far I figured out something that works, but it seems messy.
Can anyone think of a better way of doing
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
Case "W" : Label3.Text = Val(Label3.Text) + 1
Case "R" : Label4.Text = Val(Label4.Text) + 1
End Select
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "" 'Clears the text box after each keypress
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim WcountA As Integer
Dim RcountA As Integer
Dim WcountB As Integer
Dim RcountB As Integer
Dim Wavg As Single
Dim Ravg As Single
Select Case Button1.Text
Case "Finish A" 'Finishes first count and stores results in labels
WcountA = Convert.ToInt32(Label3.Text)
RcountA = Convert.ToInt32(Label4.Text)
Button1.Text = "Finish B"
Label5.Text = WcountA
Label7.Text = RcountA
TextBox1.Focus()
Label3.Text = ""
Label4.Text = ""
Case "Finish B" 'Finishes second count and stores in labels
WcountB = Convert.ToInt32(Label3.Text)
RcountB = Convert.ToInt32(Label4.Text)
With Button1
.Text = "Finished"
.Enabled = False
End With
Label6.Text = WcountB
Label8.Text = RcountB
Label3.Text = ""
Label4.Text = ""
WcountA = Label5.Text
RcountA = Label7.Text
WcountB = Label6.Text
RcountB = Label8.Text
Wavg = (WcountA + WcountB) / 2
Ravg = (RcountA + RcountB) / 2
MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
End Select
End Sub
End Class
On the form I have the textbox that logs the keypress event, labels that increase by 1 with each specific keypess ("W" and "R"), a button that changes its function with each click(finish first count, finish second count) and some labels I had to use to store the first and second count for the final calculation.
Any suggestion will be appreciated.
Thanks in advance!
First of al, you need to take out the counters of the key pres handler like so;
Because they are gone every time the Button1_Click sub ends.
Public Class Form1
Dim WcountA As Integer
Dim RcountA As Integer
Dim WcountB As Integer
Dim RcountB As Integer
Dim Wavg As Single
Dim Ravg As Single
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
Case "W" : Label3.Text = Val(Label3.Text) + 1
Case "R" : Label4.Text = Val(Label4.Text) + 1
End Select
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "" 'Clears the text box after each keypress
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case Button1.Text
Case "Finish A" 'Finishes first count and stores results in labels
WcountA = Convert.ToInt32(Label3.Text)
RcountA = Convert.ToInt32(Label4.Text)
Button1.Text = "Finish B"
Label5.Text = WcountA
Label7.Text = RcountA
TextBox1.Focus()
Label3.Text = ""
Label4.Text = ""
Case "Finish B" 'Finishes second count and stores in labels
WcountB = Convert.ToInt32(Label3.Text)
RcountB = Convert.ToInt32(Label4.Text)
With Button1
.Text = "Finished"
.Enabled = False
End With
Label6.Text = WcountB
Label8.Text = RcountB
Label3.Text = ""
Label4.Text = ""
WcountA = Label5.Text
RcountA = Label7.Text
WcountB = Label6.Text
RcountB = Label8.Text
Wavg = (WcountA + WcountB) / 2
Ravg = (RcountA + RcountB) / 2
MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
End Select
End Sub
End Class
This is what I ended up doing, works better and looks cleaner. Thanks Lectere for pointing me to the right path!
Public Class Form1
Dim WcountA As Integer
Dim RcountA As Integer
Dim WcountB As Integer
Dim RcountB As Integer
Dim Wavg As Single
Dim Ravg As Single
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
Case "W" : lblWcount.Text = Val(lblWcount.Text) + 1
Case "R" : lblRcount.Text = Val(lblRcount.Text) + 1
Case Convert.ToChar(13) : Button1_Click(sender, e)
End Select
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "" 'Clears the text box after each keypress
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case Button1.Text
Case "Finish A" 'Finishes first count and stores results in variables
If lblWcount.Text = vbNullString Then 'checks for empty values and treats them as 0
WcountA = 0
Else
WcountA = lblWcount.Text
End If
If lblRcount.Text = vbNullString Then 'checks for empty values and treats them as 0
RcountA = 0
Else
RcountA = lblRcount.Text
End If
Button1.Text = "Finish B"
lbl_1.Text = WcountA
lbl_2.Text = RcountA
TextBox1.Focus()
lblWcount.Text = vbNullString
lblRcount.Text = vbNullString
lblcount.Text = "Count B"
Case "Finish B" 'Finishes second count and stores results in variables
lblcount.Text = vbNullString
If lblWcount.Text = vbNullString Then
WcountB = 0
Else
WcountB = lblWcount.Text
End If
If lblRcount.Text = vbNullString Then
RcountB = 0
Else
RcountB = lblRcount.Text
End If
With Button1
.Text = "Reset"
End With
lbl_3.Text = WcountB
lbl_4.Text = RcountB
lblWcount.Text = vbNullString
lblRcount.Text = vbNullString
Wavg = (WcountA + WcountB) / 2
Ravg = (RcountA + RcountB) / 2
MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
Button1.Focus()
Case "Reset" 'Resets values
Dim i As Integer
For i = 1 To 4 'clears labels that start with lbl_ (1 through 4)
Dim myLabel As Label = CType(Controls("lbl_" & i), Label)
myLabel.Text = vbNullString
Next
Initial() ' calls initial form state
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Initial() 'calls initial form state at load up
End Sub
Private Sub Initial() 'initial state sub
lblcount.Text = "Count A"
TextBox1.Focus()
Button1.Text = "Finish A"
End Sub
End Class
I managed to use loops for clearing labels, and also made pressing Enter during the KeyPress event be treated as a button click.
Loving this feeling of accomplishment when I finally figure something out on something new to me! Love pages like this one where one can learn so much!

dynamic sql generation error occured when trying to update database

I followed your code and I have put it a msgbox to confirm and yes its returns the right row number. However an error occurred here's my code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cb As New OleDb.OleDbCommandBuilder(da) 'command builder to update the database
ds.Tables("inventory_table").Rows(marker).Item(0) = TextBox1.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(1) = TextBox2.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(2) = TextBox3.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(3) = TextBox4.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(4) = TextBox5.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(5) = TextBox6.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(6) = TextBox7.Text 'update dataset
da.Update(ds, "inventory_table") 'this is where the updating of database takes place
MsgBox("Data updated")
End Sub
Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
TextBox1.Text = DataGridView1.SelectedRows(0).Cells(0).Value
TextBox2.Text = DataGridView1.SelectedRows(0).Cells(1).Value
TextBox3.Text = DataGridView1.SelectedRows(0).Cells(2).Value
TextBox4.Text = DataGridView1.SelectedRows(0).Cells(3).Value
TextBox5.Text = DataGridView1.SelectedRows(0).Cells(4).Value
TextBox6.Text = DataGridView1.SelectedRows(0).Cells(5).Value
TextBox7.Text = DataGridView1.SelectedRows(0).Cells(6).Value
marker = Me.DataGridView1.SelectedRows(0).Index
MsgBox(marker)
End Sub
The error says:
dynamic sql generation for the updatecommand is not supporyed against
a selectcommand that does not return any key column information
and the line da.update(ds, "inventory_table") is highlighted.
In the double click event set a global variable:
_rowIndex = DataGridView1.SelectedRows(0).Index
Then
ds.Tables("inventory_table").Rows(_rowIndex).Item(1) = TextBox1.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(2) = TextBox2.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(3) = TextBox3.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(4) = TextBox4.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(5) = TextBox5.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(6) = TextBox6.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(7) = TextBox7.Text

not accessible in this context because it is 'Private' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm writing some code for a calculator and I keep getting this error. I have the math functions in another class but the variables from form1 are not accessible. Below is my code.
I've also tried changing my Dim variables to public but that also doesn't work.
Public Class Form1
'create a value to keep track of whether the calculation is complete so the calculator can revert to zero for new calculations
Dim state As Integer
'create values to store information on numbers and signs entered as well as the answer returned
Dim one As Double
Dim two As Double
Dim ans As Double
Dim sign As Char
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'when "Return" or "Enter" Key is selected button 15 is clicked (seems to only work when textbox selected????)
Me.AcceptButton = Button15
'change the title of the form
Me.Text = "Simple Calculator"
'label the buttons logically
Button1.Text = "1"
Button2.Text = "2"
Button3.Text = "3"
Button4.Text = "4"
Button5.Text = "5"
Button6.Text = "6"
Button7.Text = "7"
Button8.Text = "8"
Button9.Text = "9"
Button10.Text = "0"
Button11.Text = "+"
Button12.Text = "-"
Button13.Text = "X"
Button14.Text = "/"
Button15.Text = "Calc"
Button16.Text = "About Me"
Button17.Text = "Clear"
'allows form to revcieve key events
Me.KeyPreview = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'create action when button 1 is clicked
'if state is 1 then previous calculation was solved, then clear textbox to allow for new input
If state = 1 Then
TextBox1.Text = ""
'insert 1 into textbox
Dim Int1 As Integer = 1
TextBox1.Text = TextBox1.Text & Int1
'return state of calculator to zero to designate that calculation has NOT been solved
state = 0
Else
'else insert 1 into textbox
Dim Int1 As Integer = 1
TextBox1.Text = TextBox1.Text & Int1
End If
End Sub
' the above function for each numbered button
Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int2 As Integer = 2
TextBox1.Text = TextBox1.Text & Int2
state = 0
Else
Dim Int2 As Integer = 2
TextBox1.Text = TextBox1.Text & Int2
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int3 As Integer = 3
TextBox1.Text = TextBox1.Text & Int3
state = 0
Else
Dim Int3 As Integer = 3
TextBox1.Text = TextBox1.Text & Int3
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int4 As Integer = 4
TextBox1.Text = TextBox1.Text & Int4
state = 0
Else
Dim Int4 As Integer = 4
TextBox1.Text = TextBox1.Text & Int4
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int5 As Integer = 5
TextBox1.Text = TextBox1.Text & Int5
state = 0
Else
Dim Int5 As Integer = 5
TextBox1.Text = TextBox1.Text & Int5
End If
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int6 As Integer = 6
TextBox1.Text = TextBox1.Text & Int6
state = 0
Else
Dim Int6 As Integer = 6
TextBox1.Text = TextBox1.Text & Int6
End If
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int7 As Integer = 7
TextBox1.Text = TextBox1.Text & Int7
state = 0
Else
Dim Int7 As Integer = 7
TextBox1.Text = TextBox1.Text & Int7
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int8 As Integer = 8
TextBox1.Text = TextBox1.Text & Int8
state = 0
Else
Dim Int8 As Integer = 8
TextBox1.Text = TextBox1.Text & Int8
End If
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int9 As Integer = 9
TextBox1.Text = TextBox1.Text & Int9
state = 0
Else
Dim Int9 As Integer = 9
TextBox1.Text = TextBox1.Text & Int9
End If
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int0 As Integer = 0
TextBox1.Text = TextBox1.Text & Int0
state = 0
Else
Dim Int0 As Integer = 0
TextBox1.Text = TextBox1.Text & Int0
End If
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
'create an action for when addition button is clicked
Try
'when button is clicked dim sign and text in textbox
sign = "+"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
'repeat the above action for remainder of arithmic functions
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
Try
'when button is clicked dim sign and text in textbox
sign = "-"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Try
sign = "*"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
Try
sign = "/"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
'run functions based on sign stored during calculation
Try
two = TextBox1.Text
If sign = "+" Then
Calculator2.Math.add()
ElseIf sign = "-" Then
Calculator2.Math.minus()
ElseIf sign = "*" Then
Calculator2.Math.multiply()
ElseIf sign = "/" Then
Calculator2.Math.divide()
End If
'if user attempts to divide by zero return divide by zero error in textbox
If TextBox1.Text = "Infinity" Then
TextBox1.Text = "Divide by Zero Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End If
Catch ex As Exception
TextBox1.Text = "Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
'create message box that provides information about author
Dim msg = "Student Name: Emily Wong" & vbCrLf & "Student Number: 0692740" ' Define the message you want to see inside the message box.
Dim title = "About Me" ' Define a title for the message box.
Dim style = MsgBoxStyle.OkOnly ' make an ok button for the msg box
Dim response = MsgBox(msg, style, title)
End Sub
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
'create a clear button to clear textboxes when clicked and reset state of calculator
TextBox1.Text = ""
state = 0
End Sub
And this is my other class
Public Class Math
Inherits Form1
'create funtions for add,sub, multiply, and divide features
Sub add()
ans = one + two 'creates calculation of the entered numbers
TextBox1.Text = ans 'returns answer into the textbox
state = 1 'returns state to 1 to show that calculation has occured
End Sub
Sub minus()
ans = one - two
TextBox1.Text = ans
state = 1
End Sub
Sub multiply()
ans = one * two
TextBox1.Text = ans
state = 1
End Sub
Sub divide()
ans = one / two
TextBox1.Text = ans
state = 1
End Sub
End Class
state is a private variable in Form1. You can't access from code outside of Form1. Your Math class cannot reference state. Remove the calls to state from the Math class code. An outside class should not be changing the state of another object in anycase.
Try this instead:
Public Class accounting
Dim Operand1 As Double
Dim Operand2 As Double
Dim [Operator] As String
These are the button from 1 - 0
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button19.Click, Button12.Click, Button11.Click, Button10.Click
txtans.Text = txtans.Text & sender.text
End Sub
This button is for period/point
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
If InStr(txtans.Text, ".") > 0 Then
Exit Sub
Else
txtans.Text = txtans.Text & "."
End If
End Sub
This button is for clear or "C" in calculator
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
txtans.Text = ""
End Sub
These are for add,subtract,divide, and multiply
Private Sub Buttonadd_Click(sender As Object, e As EventArgs) Handles Button13.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "+"
End Sub
Private Sub Buttondivide_Click(sender As Object, e As EventArgs) Handles Button15.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "-"
End Sub
Private Sub Buttonsubtract_Click(sender As Object, e As EventArgs) Handles Button16.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "*"
End Sub
Private Sub Buttonmultiply_Click(sender As Object, e As EventArgs) Handles Button14.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "/"
End Sub
This button is for "=" sign
Private Sub Buttoneequal_Click(sender As Object, e As EventArgs) Handles Button17.Click
Dim Result As Double
Operand2 = Val(txtans.Text)
'If [Operator] = "+" Then
' Result = Operand1 + Operand2
'ElseIf [Operator] = "-" Then
' Result = Operand1 - Operand2
'ElseIf [Operator] = "/" Then
' Result = Operand1 / Operand2
'ElseIf [Operator] = "*" Then
' Result = Operand1 * Operand2
'End If
Select Case [Operator]
Case "+"
Result = Operand1 + Operand2
txtans.Text = Result.ToString("#,###.00")
Case "-"
Result = Operand1 - Operand2
txtans.Text = Result.ToString("#,###.00")
Case "/"
Result = Operand1 / Operand2
txtans.Text = Result.ToString("#,###.00")
Case "*"
Result = Operand1 * Operand2
txtans.Text = Result.ToString("#,###.00")
End Select
txtans.Text = Result.ToString("#,###.00")
End Sub
This button is for backspace effect.
Private Sub Buttondel_Click(sender As Object, e As EventArgs) Handles Button21.Click
If txtans.Text < " " Then
txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1 + 1)
Else
txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1)
End If
End Sub
Note that "txtans.text" is the textbox where the user inputs and where the output shows.