none
Converting A Combobox Selection into a integer? RRS feed

  • Question

  • I am working on a calculator program for a game and when someone selects say "Regular Log" from the combo box, how can i make that convert into an integer for a simple math equation.?
    Thursday, December 18, 2008 10:13 PM

Answers

  • demon824,

    I see, ok, well you could use an Integer Array that has values of numbers you want to associate with the Index of your ComboBox, try this:

    Public Class Form1  
     
        Private cbInt(2) As Integer 
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            cbInt(0) = 25  
            cbInt(1) = 50  
        End Sub 
     
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged  
            Me.Text = Math.Log(cbInt(ComboBox1.SelectedIndex))  
        End Sub 
    End Class 

    This will change the Text in the Form's Text (Just for the example), according to what Index is selected by the user, which in turn associates the value with our predefined values of 25 and 50.

    I hope this helps.


    Best Regards,

    Adjutor
    Thursday, December 18, 2008 11:15 PM

All replies

  • demon824,

    I am not sure how you mean convert the String, "Regular Log" into an Integer; do you mean find out the SelectedIndex of the item?  If so, just do something like this:

    MessageBox.Show(Math.Log(ComboBox1.SelectedIndex)) 

    I am not sure what "simple math equation" you want to use, so I just used a Log.

    I hope that helps, any questions feel free to post.


    Best Regards,

    Adjutor
    Thursday, December 18, 2008 10:31 PM
  • All right heres a better explanation. I want to have a collection of strings in a combobox (so far i did that) but, I want each of those combobox strings to be a certain number. (ex. the combobox choice is Regular, i want that to also equal the integer 25).. Is that better? Is this even possible? 
    Thursday, December 18, 2008 10:34 PM
  • demon824,

    I see, ok, well you could use an Integer Array that has values of numbers you want to associate with the Index of your ComboBox, try this:

    Public Class Form1  
     
        Private cbInt(2) As Integer 
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            cbInt(0) = 25  
            cbInt(1) = 50  
        End Sub 
     
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged  
            Me.Text = Math.Log(cbInt(ComboBox1.SelectedIndex))  
        End Sub 
    End Class 

    This will change the Text in the Form's Text (Just for the example), according to what Index is selected by the user, which in turn associates the value with our predefined values of 25 and 50.

    I hope this helps.


    Best Regards,

    Adjutor
    Thursday, December 18, 2008 11:15 PM
  • A recent thread had a very similar question and I believe this problem is better solved using an Enum as it provides for more readability and gives a direct connection between the text displayed to the user and the number that is hidden underneath.

    Check out the the code in the post by Chris Dunaway

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/47f3de72-05d8-44eb-bec3-c8946f9cadb9

    -Hazelrah
    Friday, December 19, 2008 12:10 AM