简体   繁体   中英

How to delete a combobox's selected item from another Combobox

I'm trying to create a simple ticket fair app but I'm stuck at some place. I have load-Data() method which populates the 2 combo box (destination & source), each contains the same list of cities. I want Destination's selected item(city) removed from the Source.

I disabled the source combo box which gets enabled from the Selection_Changed event of Destination combo box.

I tried removing selected item of Destination from source at Selection_Changed event. but then it occurred to me what if user selects wrong item in the first place? it will get removed from the source anyway.

Any advice how do I go about achieving this?

here's the code i tried:

private void DestinationCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           // await LoadData();
        object selectedItem = DestinationCombo.SelectedItem;
            if(SourceCombo.Items.Contains(selectedItem))
            {
                SourceCombo.Items.Remove(selectedItem);
            }

        //await LoadData();
        SourceCombo.IsEnabled = true;
        }

'LoadDate' Method:

private async Task LoadData()
        {
            DestinationCombo.Items.Clear();
            SourceCombo.Items.Clear();
            file = await ApplicationData.Current.LocalFolder.GetFileAsync("jsonText.txt");
            var jsonContent = await FileIO.ReadTextAsync(file);

            DataModel2.RootObject states = JsonConvert.DeserializeObject<DataModel2.RootObject>(jsonContent);

            foreach (var state in states.country.state)
            {
                foreach (var city in state.city)
                {

                    DestinationCombo.Items.Add(city);
                    SourceCombo.Items.Add(city);

                }
            }
        }

First, where all these methods are, ie, in the class (most likely a Form#), declare a public variable called cities:-

public list<string> cities = new list<string>;

Then edit your loaddata() method a little:

private async Task LoadData()
    {
        DestinationCombo.Items.Clear();
        SourceCombo.Items.Clear();
        file = await ApplicationData.Current.LocalFolder.GetFileAsync("jsonText.txt");
        var jsonContent = await FileIO.ReadTextAsync(file);

        DataModel2.RootObject states = JsonConvert.DeserializeObject<DataModel2.RootObject>(jsonContent);

        foreach (var state in states.country.state)
        {
            foreach (var city in state.city)
            {
                cities.Add(city);
            }
        }

        DestinationCombo.Items.AddRange(cities.ToArray<String>());
        SourceCombo.Items.AddRange(cities.ToArray<String>());
    }

Finally, in the SourceCombo SelectionChanged Event , put this code:

private void SourceCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
       // await LoadData();
    object selectedItem = SourceCombo.SelectedItem;
    DestinationCombo.Items.Clear();
    DestinationCombo.Items.AddRange(cities.ToArray<String>());
    DestinationCombo.Items.Remove(selectedItem);
    }

You can also add similar code to the other ComboBox so that whichever ComboBox (Source or Destination) the user clicks, the selection is removed from the other ComboBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM