Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have two checked combo boxes..

First one will Consist of enums (BSECash,NSECash)...second will filled with enum names according to the selection of enums in first one...enums are..
Public Enum BSECash
Rolling = 1
T2T = 2
Auction = 3
Valan = 4
Close_Out = 5
Opening = 6
Corp_Act = 7
End Enum
Public Enum NSECash
Normal = 1
TFT = 2
Auction_Normal = 3
Close_Out = 4
Opening = 5
Corp_Act = 6
End Enum
issue is..suppose if i select BSECash and i check some of items in the second one...and later when i check NSEcash all previous checked items in the second one are becoming unchecked..

Private Sub ccmbExchngeSegment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ccmbExchngeSegment.SelectedIndexChanged, ccmbExchngeSegment.SelectedValueChanged, ccmbExchngeSegment.TextChanged
       Try
           ccmbSettlementType.Items.Clear()
           For iCnt As Int32 = 0 To ccmbExchngeSegment.CheckedItems.Count - 1
               If ccmbExchngeSegment.CheckedItems.Item(iCnt).ToString = "BSECash" Then
                  Dim iCntn As Int32 = [Enum].GetNames(GetType(BSECash)).Length
                 For jCnt As Int32 = 1 To iCntn
                       ccmbSettlementType.Items.Add(New clsValDescPair(jCnt, CType(jCnt, BSECash).ToString.Replace("_", " ")))
                   Next
                 ElseIf ccmbExchngeSegment.CheckedItems.Item(iCnt).ToString = "NSECash" Then
              Dim iCntn As Int32 = [Enum].GetNames(GetType(NSECash)).Length
                   For jCnt As Int32 = 1 To iCntn
                       ccmbSettlementType.Items.Add(New clsValDescPair(jCnt, CType(jCnt, NSECash).ToString.Replace("_", " ")))
                   Next
               End If

           Next


logic is missing some where...help me out..
Posted

1 solution

The answer to why the items are becoming unchecked is that whenever you select another enum in your first check box, you clear all the items then re-add them. This means that you remove everything and add new ones so there all record of what was checked before is lost. To solve this you must do something a bit more sophisticated than just refresh your list each time. I think the following ought to solve your problem (mix of pseudo code and real code):

VB
Dictionary of <enum,>> SelectedItems
Enum PreviouslySelectedEnum = null
Method  ccmbExchngeSegment_SelectedIndexChanged

    IF NOT PreviouslySelectedEnum = null 
        IF NOT SelectedItems contains key PreviouslySelectedEnum
            SelectedItems.Add(PreviouslySelectedEnum, new List<string>())
        END IF
        
        SelectedItems[PreviouslySelectedEnum].Clear()
        FOR i = 0 to ccmbSettlementType.Items.Count
            IF ccmbSettlementType.Items[i].Checked
                SelectedItems[PreviouslySelectedEnum].Add(ccmbSettlementType.Items[i].Text)
            END IF
        END FOR
    END IF

    NewSelectedEnum = ccmbExchngeSegment.Items[ccmbExchngeSegment.SelectedIndex]
    ccmbSettlementType.Items.Clear()
    For iCnt As Int32 = 0 To ccmbExchngeSegment.CheckedItems.Count - 1
        If ccmbExchngeSegment.CheckedItems.Item(iCnt).ToString = "BSECash" Then
            Dim iCntn As Int32 = [Enum].GetNames(GetType(BSECash)).Length
            For jCnt As Int32 = 1 To iCntn
                ccmbSettlementType.Items.Add(New clsValDescPair(jCnt, CType(jCnt, BSECash).ToString.Replace("_", " ")))
            Next
        ElseIf ccmbExchngeSegment.CheckedItems.Item(iCnt).ToString = "NSECash" Then
            Dim iCntn As Int32 = [Enum].GetNames(GetType(NSECash)).Length
            For jCnt As Int32 = 1 To iCntn
                ccmbSettlementType.Items.Add(New clsValDescPair(jCnt, CType(jCnt, NSECash).ToString.Replace("_", " ")))
            Next
        End If     
     Next

     IF SelectedItems contains key NewSelectedEnum
        FOR EACH String ASelectedItem in  SelectedItems[NewSelectedEnum]
            ComboBoxItem TheItem = Get combo box item where ASelectedItem = TheComboBoxItem.Text
            TheItem.Checked = true
        END FOR
     END IF
     
     PrevouslySelectedEnum = NewSelectedEnum     
End Method
</string>


This code basically saves in the SelectedItems list all the enums that ever get checked then when the selected index changes, reselects any that are in that list. This is not proper VB code though I have copied in your original code which you will need. In any code you write later, to get the list of enums that were selected just use the SelectedItems list and convert the Strings into actual enum values (using Enum.Parse).

Hope this helps,
Ed
 
Share this answer
 
Comments
DileepkumarReddy 20-Apr-12 7:56am    
Thank you very much for suggestion...i will check it out...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900