locked
ComboBox Issue RRS feed

  • Question

  • I have several combo boxes on one of my pages. In the C# code, I have set a calculation based on the combobox.selectedindex.

    What is happening is that everytime the page loads, the calculation is called. I believe because the combobox is being populated with the items collection.

    This only happens in the C# project, not the VB project with the same comboboxes.

    The other issue I have is trying to parse the selectedindex.

    In C# I have this - double.TryParse(Combo1ST.SelectedIndex, out AB1);

    I get, Object Not Set To Instance of An Object.

    In VB I have this and it works  - Double.TryParse(ComboBox1.SelectedIndex, AB1)

    Any Ideas

    Thursday, September 19, 2013 10:23 PM

Answers

  • As Prashant and Jamles said, SelectedIndex is already an integer. You don't need to parse it, just assign directly.

    hits1 = Combo1ST.SelectedIndex;
    hits2 = Combo2ND.SelectedIndex;

    // etc.

    You should do direct assignment instead of parsing on both the VB and C# code. The parsing works in VB because the integer gets converted to a string to be parsed: that's a lot of wasted work!

    One thing which would (somewhat) validate the Parse method: do you really mean SelectedIndex or do you really want SelectedItem? The first is the number of the selection (0,1,2,3...) the second is the actual value you set on it.

    A better way would be to do what we discussed in your other thread: store the values you want in the items (directly or by binding to a collection) rather than converting them to strings. You can then use DataTemplates in the ComboBox to display them appropriately. See the section on binding to an ItemsSource in How to add a combo box .

    While you can get away with hard-coded values for a naïve app, learning and making use of data binding will make your life enormously easier as your app gets more complicated.

    --Rob

    Friday, September 20, 2013 2:29 AM
    Moderator

All replies

  • Can you give an example of how you are creating the combo-boxes and what does it contain regarding your first question?

    Your second question doesn't look correct syntactically as well. double.TryParse accepts the first parameter as a string, and as per your code you are passing in an int - ComboBox.SelectedIndex will return an int not a string.


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    Friday, September 20, 2013 1:00 AM
    Moderator
  • Hi Ronald,

    I'm not sure what you mean by your first question, could you explain me more detail?

    And for your second question, TryParse Method (String, Double) would only accept "String" but not "int", that's why I think the code in C# will fail. While debugging the app, this error should be shown in the error list and stop the debugging.

    And I also tested with VB, it works, VB will translate int to string while running:

    Public Shared Function TryParse ( _
    	s As String, _
    	<OutAttribute> ByRef result As Double _
    ) As Boolean
    

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 20, 2013 1:05 AM
    Moderator
  • this is how I have my vb code.  this parses correctly.

    the combo boxes are created by adding them in from the toolbox of VS. in vb the selection_changed evrnt calls the Caluatetotal sub routine. when I navigate to the page in vb the calciulatetotal is not called until I select an item from a combobox.

    in c# the CalculateTotal routine is called as soon as I navigate to the page for some reason.

        Private Sub CalculateTotal()
            Dim AB1 As Double
            Dim hits1 As Double
            Dim hits2 As Double
            Dim hits3 As Double
            Dim BB1 As Double
            Dim HBP1 As Double
            Dim SF1 As Double
            Dim HR1 As Double
            Double.TryParse(ComboBox1.SelectedIndex, AB1)
            Double.TryParse(Combo1ST.SelectedIndex, hits1)
            Double.TryParse(Combo2ND.SelectedIndex, hits2)
            Double.TryParse(Combo3rd.SelectedIndex, hits3)
            Double.TryParse(ComboBB.SelectedIndex, BB1)
            Double.TryParse(ComboHBP.SelectedIndex, HBP1)
            Double.TryParse(ComboSF.SelectedIndex, SF1)
            Double.TryParse(ComboHR.SelectedIndex, HR1)

    here is my c# code. I cant figure out why it is being called here and not in vb.   I do need help parsing the combobox selectedindex as well..

      private void CalculateTotal()
            {
    
             
                int AB1 = 0;
                int hits1 = 0;
                int hits2 = 0;
                int hits3 = 0;
                int BB1 = 0;
                int HBP1 = 0;
                int SF1 = 0;
                int HR1 = 0;
                double.TryParse(Combo1ST.SelectedIndex, out hits1);
                double.TryParse(Combo2ND.SelectedIndex, out hits2);
                double.TryParse(Combo3rd.SelectedIndex, out hits3);
                double.TryParse(ComboBB.SelectedIndex, out BB1);
                double.TryParse(ComboHBP.SelectedIndex, out HBP1);
                double.TryParse(ComboSF.SelectedIndex, out SF1);
                double.TryParse(ComboHR.SelectedIndex, out HR1);


    Friday, September 20, 2013 1:58 AM
  • As Prashant and Jamles said, SelectedIndex is already an integer. You don't need to parse it, just assign directly.

    hits1 = Combo1ST.SelectedIndex;
    hits2 = Combo2ND.SelectedIndex;

    // etc.

    You should do direct assignment instead of parsing on both the VB and C# code. The parsing works in VB because the integer gets converted to a string to be parsed: that's a lot of wasted work!

    One thing which would (somewhat) validate the Parse method: do you really mean SelectedIndex or do you really want SelectedItem? The first is the number of the selection (0,1,2,3...) the second is the actual value you set on it.

    A better way would be to do what we discussed in your other thread: store the values you want in the items (directly or by binding to a collection) rather than converting them to strings. You can then use DataTemplates in the ComboBox to display them appropriately. See the section on binding to an ItemsSource in How to add a combo box .

    While you can get away with hard-coded values for a naïve app, learning and making use of data binding will make your life enormously easier as your app gets more complicated.

    --Rob

    Friday, September 20, 2013 2:29 AM
    Moderator