Get Combobox Value in VS - vb.net

I am making a simple pricing calculator by using a Combobox and Label Text.
My Combobox has 2 items: Weekend (30.000) and Weekday (20.000). While the multiplier is quantity.
So if I choose "Weekend (30.000)" in the combobox and input "2" to qty, the result would be 30.000 * 2 = 60.000.
I tried this code but could not work.
I wonder how do I get the value of the strings from "Weekend (30.000)" to 30000?
Private Sub txtQty_TextChanged(sender As Object, e As EventArgs) Handles txtQty.TextChanged
Try
lblFinal.Text = cboPrice.SelectedItem * txtQty.Text
Catch ex As Exception
End Try
End Sub
Private Sub cboPrice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPrice.SelectedIndexChanged
Try
If cboPrice.SelectedIndex = 1 Then
cboPrice.SelectedItem = "30000"
ElseIf cboPrice.SelectedIndex = 2 Then
cboPrice.SelectedText = "40000"
End If
Catch ex As Exception
End Try
End Sub

I tried again using this code and it worked. . .
Dim price as integer
Private Sub txtQty_TextChanged(sender As Object, e As EventArgs) Handles txtQty.TextChanged
Try
lblFinal.Text = Price * CInt(txtQty.Text)
Catch ex As Exception
End Try
End Sub
Private Sub cboPrice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPrice.SelectedIndexChanged
Try
If cboPrice.SelectedIndex = 0 Then
Price = 30000
Else
Price = 40000
End If
Catch ex As Exception
End Try
End Sub

Related

calculation error vb.net textbox

I am working with inventory system, Transaction form I had mainly 2 text box, first 1 is avaqty its from database available qty, other textbox is avqty from user adding qty.
When user add qty avaqty textbox automatically change to
(avaqty) -(avqty) but as an example 10-10 result come in -1
My code is:
Public Class transaction
Dim objcon As New connectionclass
Dim qty As String
Public Function getqty() As String
If itemid.Text = "" Then
Else
Try
objcon.myconnection.Open()
Dim getqty1 As New OleDbCommand("select balance from avaqty where item_id=" & itemid.Text & "", objcon.myconnection)
Dim reader As OleDbDataReader = getqty1.ExecuteReader
While reader.Read
qty = reader("balance")
End While
objcon.myconnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Return qty
End Function
Private Sub itemid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles itemid.KeyPress
If Asc(e.KeyChar) = 13 Then
objcon.myconnection.Open()
Try
Dim getdata As New OleDbCommand("select*from itemtbl where item_id=" & itemid.Text & "", objcon.myconnection)
Dim reader As OleDbDataReader = getdata.ExecuteReader
While reader.Read
itemdesc.Text = reader("item_des").ToString
yom.Text = reader("unit_of_mesure").ToString
cost.Text = reader("item_cost").ToString
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
objcon.myconnection.Close()
End If
If getqty() = 0 Then
avaqty.Text = 0
Else
avaqty.Text = getqty()
End If
End Sub
Private Sub initem_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles initem.CheckedChanged
dataview.DataSource = GETDATA()
End Sub
Private Sub outitem_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles outitem.CheckedChanged
dataview.DataSource = GETDATA2()
End Sub
Private Sub avqty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles avqty.TextChanged
Try
If avqty.Text = "" Then
avaqty.Text = getqty()
Else
avaqty.Text = CDbl(avaqty.Text - avqty.Text)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub avaqty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles avaqty.TextChanged
If avaqty.Text < 0 Then
MsgBox("not inough qty")
avqty.Clear()
End If
End Sub
End Class

setting Withblock variable or object variable in VB 2015

Public Class Form1
Private ContactsTableBindingSource As Object
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ContactsTableDataSet.Table' table. You can move, or remove it, as needed.
Me.TableTableAdapter.Fill(Me.ContactsTableDataSet.Table)
If ComboBox1.Text = Nothing Then
Try
ContactsTableBindingSource.AddNew()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If First_NameTextBox.Text = Nothing Then
First_NameTextBox.Text = "unknown"
End If
If Last_NameTextBox.Text = Nothing Then
Last_NameTextBox.Text = "unknown"
End If
If AddressTextBox.Text = Nothing Then
AddressTextBox.Text = "unknown"
End If
If Phone_NumberTextBox.Text = Nothing Then
Phone_NumberTextBox.Text = "unknown"
End If
If Email_AddressTextBox.Text = Nothing Then
Email_AddressTextBox.Text = "unknown"
End If
Try
Me.Validate()
Me.ContactsTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ContactsTableDataSet)
MessageBox.Show("The data has been saved", "Information", MessageBoxButtons.OK)
ContactsTableBindingSource.AddNew()
First_NameTextBox.Select()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Please suggest a correction in the code as I have being showing the error "object variable or Withblock not set"

ERROR with a basic user interaction program in VB.NET

Can some please advise me of what I'm doing wrong.
I'm new to visual basic 2012
What I am trying to do:
One input is saved into an Integer variable. The other input is saved into a Byte variable.
The output is calculated as the Integer * Byte/100 to the power 2.
If the user enters a non-numeric value, and clicks the button, show an error of "Illegal Value entered" using Messagebox.
If the user enters an "out of range" number, show an error of "input not in valid range" using Messagebox.
This is the code that I have:
Public Class Form1
Dim T1 As String
Dim T2 As String
Dim a As Integer
Dim b As Byte
Dim c As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
TextBox1.Text = False
TextBox2.Text = False
a = CInt(TextBox1.Text)
b = CByte(TextBox2.Text)
c = a * b / 100
c = c * a * b / 100 ^ 2
Label3.Text = "Output:" + c.ToString()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Illegal Value Entered")
Catch ex As IndexOutOfRangeException
MessageBox.Show("Input not in valid range")
Catch ex As Exception
MessageBox.Show("Some exception has occured")
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
Please remove the expressions TextBox1.Text = False (and for TextBox2) this would always force CInt() and CByte() to parse a string 'False' (not a user provided number). Your code should look like this.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
a = CInt(TextBox1.Text)
b = CByte(TextBox2.Text)
c = a * b / 100
c = c * a * b / 100 ^ 2
Label3.Text = "Output:" + c.ToString()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Illegal Value Entered")
Catch ex As IndexOutOfRangeException
MessageBox.Show("Input not in valid range")
Catch ex As Exception
MessageBox.Show("Some exception has occured")
End Try
End Sub
If you want to clear the numbers from textboxes after calculation, then please put these lines of code right after Label3.Text = ... line
TextBox1.Text = False
TextBox2.Text = False
CByte documentation clearly states that
If expression lies outside the acceptable range for the byte subtype,
an error occurs.
CByte(...) constructor would not be able to parse any number greater than 255.

Saving an array to file

I been trying to save an array to file but I can only create the text file but no text shows up in actual file. The loop doesn't work the way I want to. I need to store the Cust array into text file.
Dim car(4) As Decimal
Dim Cust(4) As String
'Declare module-level constants.
Const PER_DAY_Integer As Integer = 15
Const PER_MILE_Decimal As Decimal = 0.12D
Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
'Calculate the rental charges
Cust(0) = NameTextBox.Text
Cust(1) = AddressTextBox.Text
Cust(2) = CityTextBox.Text
Cust(3) = ZipTextBox.Text
Cust(4) = StateTextBox.Text
Try
car(0) = Decimal.Parse(BeginTextBox.Text)
Try
car(1) = Decimal.Parse(EndTextBox.Text)
Try
car(2) = Integer.Parse(DaysTextBox.Text)
'Calculate the number of miles driven and the charges
car(3) = car(1) - car(0)
car(4) = (car(3) * PER_MILE_Decimal + car(2) * PER_DAY_Integer)
'Display the results
TotalTextBox.Text = car(4).ToString("C")
MilesTextBox.Text = car(3).ToString("N1")
Catch ex As Exception
MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
DaysTextBox.Focus()
End Try
Catch ex As Exception
MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
EndTextBox.Focus()
End Try
Catch ex As Exception
MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
BeginTextBox.Focus()
End Try
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
'Clear the form
AddressTextBox.Clear()
CityTextBox.Clear()
StateTextBox.Clear()
ZipTextBox.Clear()
BeginTextBox.Clear()
EndTextBox.Clear()
DaysTextBox.Clear()
TotalTextBox.Clear()
MilesTextBox.Clear()
With NameTextBox
.Clear()
.Focus()
End With
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
'End the program
Me.Close()
End Sub
Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
'Print the form
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub
Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
End Sub
Private Sub WriteBtn_Click(sender As Object, e As EventArgs) Handles WriteBtn.Click
Dim outFile As IO.StreamWriter
Dim int As Integer = 0
Dim count As Integer = 0
outFile = IO.File.CreateText("Cust.txt")
Do While int < count
outFile.WriteLine(Cust(1)(int))
int += 1
Loop
outFile.Close()
End Sub
End Class
You were asked where it failed when you debug and you obviously haven't even bothered to debug or you'd have seen the issue. Look at this from your code:
Dim int As Integer = 0
Dim count As Integer = 0
outFile = IO.File.CreateText("Cust.txt")
Do While int < count
outFile.WriteLine(Cust(1)(int))
int += 1
Loop
Is int ever going to be less than count?
You can replace the entire contents of that last event handler with one line:
IO.File.WriteAllLines("Cust.txt", Cust)
Of course, you should not be using file names alone but rather a file path, but that's another issue.

Why isn't my vb.net code saving a row to my Access database? (Error)

I'm trying to add a 'coupling' to my database with the following code. It give me the error "Update requires a valid InsertCommand when passed DataRow collection with new rows."
Private Sub AddCoupling_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CouplingsTableAdapter.Fill(TestDatabaseDataSet.Couplings)
End Sub
Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim newCoup As DataRow = TestDatabaseDataSet.Tables("Couplings").NewRow
newCoup.Item("Type") = ComboBoxType.Text
newCoup.Item("Part Number") = TextPartNumber.Text
newCoup.Item("Description") = TextDescription.Text
newCoup.Item("Rubber Type") = ComboBoxRubberType.Text
newCoup.Item("TKN") = Integer.Parse(TextTKN.Text)
newCoup.Item("TKmax") = Integer.Parse(TextTKmax.Text)
newCoup.Item("TKW") = Integer.Parse(TextTKW.Text)
newCoup.Item("Ambient Temp") = Integer.Parse(TextTemp.Text)
newCoup.Item("PKW") = Integer.Parse(TextPKW.Text)
newCoup.Item("Max Speed") = Integer.Parse(TextSpeed.Text)
Try
TestDatabaseDataSet.Tables("Couplings").Rows.Add(newCoup)
CouplingsTableAdapter.Adapter.Update(TestDatabaseDataSet) ' ERROR HERE '
MessageBox.Show("Saved!")
Close()
Catch ex As Exception
MessageBox.Show("Failed to save to database. E:" & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub