Kccc

im makeing my own calculator for the game Runescape, i know i just play it in my spaire time and i was felling i needed to know more about visual basic and how to make a calculator, so heres a pic,

what i want to know is,
1.how do i set values for each item in the combo box,
2.how to get it toso that when u hit the calc button it mult the number in the first text box by the selected combo box items value and display it in the 3rd textbox, all help will be appreated,


Re: Visual Basic Express Edition having calculator problems

nobugz

A combobox can store more than just strings, it can store objects. Check this thread for C# code that shows the technique.





Re: Visual Basic Express Edition having calculator problems

Kccc

i dont know c# and what i mean is if i put 200 in the first texbox and selected shafts it would do
200*15 then display the result in the 3rd textbox, and i need to know how to set the value of the combobox item like shafts is worth 15 there for 200*15 is 200 logs becoming 3000 exp




Re: Visual Basic Express Edition having calculator problems

Kccc

i have this code and i was wondering if it set it so that shafts = 20exp so that when i did 200*shafts it would come out like 200*20
if so then how do i get the calculate button to do the math from textbox 1 and combobox1 to appear in texbox 3
Dim x As New Integer
x = 20
String.Equals("shafts", x)




Re: Visual Basic Express Edition having calculator problems

nobugz

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'--- Fill the combobox
cboUnits.Items.Add(New Unit("Shafts", 10))
cboUnits.Items.Add(New Unit("Oak shorts (u)", 15))
'... Etc
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim amount As Decimal = Decimal.Parse(txtAmount.Text)
Dim unit As Unit = CType(cboUnits.Items(cboUnits.SelectedIndex), Unit)
lblResult.Text = CStr(amount * Unit.mMult)
Catch ex As Exception
lblResult.Text = ex.Message
End Try
End Sub

'--- What we store in cboUnits:
Private Class Unit
Public mText As String
Public mMult As Decimal
Public Sub New(ByVal txt As String, ByVal mult As Decimal)
mText = txt
mMult = mult
End Sub
Public Overrides Function ToString() As String
Return mText
End Function
End Class

End Class






Re: Visual Basic Express Edition having calculator problems

Kccc

Thank you SOOOO much for that your the man




Re: Visual Basic Express Edition having calculator problems

Kccc

ok 1 more question how do i insted of getting it 2 mult 2 numbers do i get it to subtract 2 numbers then devide buy another heres the code i have but it dosent do it correctly, i dont know whats wrong i dont get eny errors so help plz


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'--- Fill the combobox
cboUnits.Items.Add(New Unit("Shafts", 150))
cboUnits.Items.Add(New Unit("Oak ShortBow (u)", 100))
cboUnits.Items.Add(New Unit("Oak LongBow (u)", 110))
cboUnits.Items.Add(New Unit("Willow ShortBow (u)", 200))
cboUnits.Items.Add(New Unit("Willow LongBow (u)", 220))
cboUnits.Items.Add(New Unit("maple ShortBow (u)", 300))
cboUnits.Items.Add(New Unit("Maple LongBow (u)", 330))
cboUnits.Items.Add(New Unit("Yew ShortBow (u)", 400))
cboUnits.Items.Add(New Unit("Yew LongBow (u)", 440))
cboUnits2.Items.Add(New Unit("1", 0))
cboUnits2.Items.Add(New Unit("2", 83))
cboUnits2.Items.Add(New Unit("3", 174))
cboUnits2.Items.Add(New Unit("4", 276))
cboUnits2.Items.Add(New Unit("5", 388))
cboUnits2.Items.Add(New Unit("6", 512))
cboUnits2.Items.Add(New Unit("7", 650))
cboUnits2.Items.Add(New Unit("8", 801))
cboUnits2.Items.Add(New Unit("9", 969))
cboUnits2.Items.Add(New Unit("10", 1154))
cboUnits2.Items.Add(New Unit("11", 1358))
cboUnits2.Items.Add(New Unit("12", 1584))
cboUnits2.Items.Add(New Unit("13", 1833))
cboUnits2.Items.Add(New Unit("14", 2107))
cboUnits2.Items.Add(New Unit("15", 2411))

End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim unit2 As Unit = CType(cboUnits2.Items(cboUnits2.SelectedIndex), Unit)
Dim unit As Unit = CType(cboUnits.Items(cboUnits.SelectedIndex), Unit)
Dim amount As Decimal = Decimal.Parse(txtAmount.Text)
lblResult.Text = CStr(unit2.mMult - amount)
Catch ex As Exception
lblResult.Text = ex.Message
End Try
End Sub

'--- What we store in cboUnits:
Private Class Unit
Public mText As String
Public mMult As Decimal
Public Sub New(ByVal txt As String, ByVal mult As Decimal)
mText = txt
mMult = mult
End Sub
Public Overrides Function ToString() As String
Return mText
End Function
End Class
End Class