1 Replies - 5598 Views - Last Post: 08 June 2010 - 05:52 AM Rate Topic: -----

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

removing items from combobox in codebehind

Posted 08 June 2010 - 04:28 AM

I'm currently messing about with the following - two radio buttons, and a combo box where its contents change depending on which radio button is selected. Items are adding fine but they are not removing after the radio button selection has changed, so the combo box is just adding more items instead of generating a new list of items.
Here is all my code.

xaml
<UserControl x:Class="RadioButtonTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <RadioButton Content="RadioButton 1" Height="16" HorizontalAlignment="Left" Margin="12,26,0,0" Name="radioButton1" VerticalAlignment="Top" Checked="RadioButton1_Checked" Unchecked="RadioButton1_UnChecked"/>
        <RadioButton Content="RadioButton 2" Height="16" HorizontalAlignment="Left" Margin="136,26,0,0" Name="radioButton2" VerticalAlignment="Top" Checked="RadioButton2_Checked" Unchecked="RadioButton2_UnChecked"/>
        <ComboBox Height="23" Margin="12,70,244,0" Name="comboTest" VerticalAlignment="Top" />
    </Grid>
</UserControl>



.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RadioButtonTest
{
    public partial class MainPage : UserControl
    {
        private class Item
        {
            public string Name;
            public int Value;
            public Item(string name, int value)
            {
                Name = name; Value = value;
            }
            public override string ToString()
            {
                // Generates the text shown in the combo box
                return Name;
            }
        }

        public MainPage()
        {
            InitializeComponent();
        }

        private void RadioButton1_Checked(object sender, RoutedEventArgs e)
        {       
            comboTest.Items.Add(new Item("Blue", 1));
            comboTest.Items.Add(new Item("Red", 2));
            comboTest.Items.Add(new Item("Black", 3));        
         }

        private void RadioButton2_Checked(object sender, RoutedEventArgs e)
        {
            comboTest.Items.Add(new Item("Red", 1));
            comboTest.Items.Add(new Item("Pink", 2));
            comboTest.Items.Add(new Item("Blue", 3));
        }

       private void RadioButton1_UnChecked(object sender, RoutedEventArgs e)
        {
            comboTest.Items.Remove("Blue");
            comboTest.Items.Remove("Red");
            comboTest.Items.Remove("Black");
        }

        private void RadioButton2_UnChecked(object sender, RoutedEventArgs e)
        {
            comboTest.Items.Remove("Red");
            comboTest.Items.Remove("Pink");
            comboTest.Items.Remove("Blue");
       }
      
      
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: removing items from combobox in codebehind

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: removing items from combobox in codebehind

Posted 08 June 2010 - 05:52 AM

call this before adding new items to the combobox...

comboText.Items.Clear();



This will allow you to only have to use the Checked event and not both the Checked and Unchecked events of the radiobuttons.

This post has been edited by eclipsed4utoo: 08 June 2010 - 05:53 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1