locked
combo boxs RRS feed

  • Question

  • Hi all

    Please excuse my ignorance I am very new to writing windows store apps

    I have 2 combo boxes CB1 has 4 items

    I want to be able to select any of the four items and depending on which item is selected it then populates the second combobox with a number of items which will differ depending on which item is selected

    I presume its some sort of code in the selection change event that I need such as an if statement

    can anyone point me in the right direction on how to do this

    Thanks in advance

    Mark


    • Moved by Carl Cai Monday, October 21, 2013 5:26 AM get better support
    Saturday, October 19, 2013 10:44 PM

Answers

  • CRACKED IT GUYS!!!

    this.comboBox1.Items.Clear();

    Clears the combobox

    Thank you for all your help pointing me in the right direction

    Mark

    • Marked as answer by wambaugh1 Tuesday, October 29, 2013 6:40 AM
    Tuesday, October 29, 2013 6:40 AM

All replies

  • Hello,

    you can handle the SelectionChanged event of your first comboBox :

    Private Sub firstComboBox_SelectionChanged(
                  sender As Object, e As EventArgs)
      Select Case firstComboBox.SelectedIndex
        Case 0
          '// do something when item 0 is selected
        Case 1
          '// do something when item 1 is selected
        Case 2
          '// do something when item 2 is selected
        Case 3
          '// do something when item 3 is selected
      End Select
    End Sub


    Nimzozo, from France.

    Sunday, October 20, 2013 1:08 PM
  • Hi Mark,

    Take a look at this tutorial: How to create a master-details binding.

    SelectionChanged is a good suggestion, but to use binding is much easier than fire the SelectionChanged event.

    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.

    Monday, October 21, 2013 5:23 AM
    Moderator
  • nimzozo

    thank you that works great

    The problem I have now is how do I do the same based on two combo boxes

    I have 3 comboboxes combobox1, combobox2 & combobox3

    in need combobox3 to display items based on what combobox 1 & 2 have in them

    I think its something like an if then statement when the second combobox has been selected but cant figure it out

    Any ideas?

    Mark

    Thursday, October 24, 2013 11:19 PM
  • Hi wambaug1,

    Check out the link I suggested to you, it is a much easier way to work on more than three combobox. If only two combobox, the SelectionChanged event might be the best solution.

    Ok, you wish to use something like "if..then" or "switch" statement, you could add following code(C# code) in your second combobox SelectionChanged event.

    switch (firstComboBox.SelectedIndex)
    {
        case 1:
            switch (secondComboBox.SelectedIndex)
            {
                case 1:
                      break;
                case 2:
                      break;
            }
            break;
        case 2:
            switch (secondComboBox.SelectedIndex)
            {
                case 1:
                      break;
                case 2:
                      break;
            }
            break;
    }

    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, October 25, 2013 1:13 AM
    Moderator
  • Hello wambaugh,

    If your data which is in combobox are static than you use switch....case..... Otherwise you can try to get data of combobox selection changed event. Based on first combobox value, get the second combobx data. Selecting second combobox value, using selection changed event & get data based on 1st combobox selected value & 2nd combobox selected valus. Get 3rd combobox data.

          For example, get the country, city, area. When you select country based on that country data show in city (combobox). Based on that country & city (combobox) value. Get data & show in area combobox.

    Friday, October 25, 2013 4:44 AM
  • Thanks guys

    this works great

    one last problem

    i want to fill a combobox depending on what the title of the page is

    my page title is stored in{Binding Title}

    i want something like 

    if {Binding Title}= "Title 1" the do something

    if {Binding Title}= "Title 2" the do something else

    any ideas?

    Mark

    Sunday, October 27, 2013 9:48 AM
  • Hello ,

    try to get that title on your page's onnavigateto method. When your page load, check your page's title is title1 do something, page title is title-2 do something it is same as switch case, which is you use for selection changed event of combobox.

    Sunday, October 27, 2013 10:08 AM
  • Hi 

    thanks for the reply

    I create the combo using this

     <ComboBox x:Name="comboBox1" SelectionChanged="ComboBox_SelectionChanged1" Margin="123,10,686,574">
                    <x:String>Item 1</x:String>
                    <x:String>Item 2</x:String>
                    <x:String>Item 3</x:String>
                </ComboBox>

    i then want to change this depending on the page tite something like

    IF "Page Title" = "Title 1" then

    comboBox1.clear
    comboBox1.Items.Add("1");
    comboBox1.Items.Add("2");
    comboBox1.Items.Add("3");

    end if

    IF "Page Title" = "Title 2" then

    comboBox1.clear
    comboBox1.Items.Add("4");
    comboBox1.Items.Add("5");
    comboBox1.Items.Add("6");
    
    

    end if

    i just cant work out how to get the variable "Page Title" and compare it

    Im used to coding in VB so c# is a challenge

    your help is appreciated

    Mark

    Sunday, October 27, 2013 10:35 AM
  • Hello wambaugh1,

    if combobx value selected than you go to another page or continues to current page?

    If go to another page, then send value which is you selected in combobox.

    If continues of that page than in selection change event of combobox get value of combobox selecteditem,

    Private Sub ComboBox1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
    			'Title-1 selected
    	If ComboBox1.SelectedItem.ToString() = "Title-1" Then
    			'Title-2 selected
    	ElseIf ComboBox1.SelectedItem.ToString() = "Title-2" Then
    	End If
    End Sub

    Monday, October 28, 2013 4:15 AM
  • Hi there

    I understand how to change pages but i'm using one page for all 

    I need to pick up the title when the page loads and depending on the title which is different depending what tile is picked on the main page and then fill the combo box accordingly

    title is stored in {Binding Title} but i just cant work out how to extract the title from that and then put it in an if statement so that I can change the contents of the combobox

    is it possible to put it into a variable then check the variable eg VAR titlename={Binding Title}

    The container the text is in is called "pageTitle"

    Mark



    • Edited by wambaugh1 Monday, October 28, 2013 6:25 AM
    Monday, October 28, 2013 6:24 AM
  • Hello wambaugth,

    i understand you problem little bit, here is your solution based on what i am understand:

    \\your class name is classa which is using for binding title
    Dim a1 As classa = DirectCast(ComboBox1.SelectedItem, classa)
    		'Title-1 selected
    If a1.Title = "Title-1" Then
    		'Title-2 selected
    ElseIf a1.Title = "Title-2" Then
    End If
    
    Public Class classa
    	Public Property Title() As String
    		Get
    			Return m_Title
    		End Get
    		Set
    			m_Title = Value
    		End Set
    	End Property
    	Private m_Title As String
    End Class

    Monday, October 28, 2013 6:32 AM
  • Hi There

    Maybe im being thick here and not understanding you or not explaning my problem fully but from what I am seeing you are choosing what is selected from the combobox and then doing something depending what is selected in the combobox

    I know how to do that Im trying to get the title of the page not anything to do with the combobox itself other than changing its contents before it is even clicked

    my apologies if that is what you are showing in your example if It is correct whener do I put the example code in my page which is called page2.xaml

    Mark

    Monday, October 28, 2013 6:56 AM
  • Page title is static or not in page, i mean it is come to another page like (send page name to another page during navigation)?
    Monday, October 28, 2013 7:06 AM
  • yes thats correct the title is created in page one using a json data page and then page two opens depending on which tile is clicked on page one

    When page 2 opens the title is stored in {Binding Title} i just dont know how to get that out in the code and then populate the combobox depending what the title is

    mark

    Monday, October 28, 2013 9:06 AM
  • So, your page title is in textblock like that name is tbl1. So 

    if(tbl1.Text=="Page-1")

    {

    //Title-1 is in page

    }

    else if(tbl1.Text=="Page-2")

    {

    //Title-2 is in page

    }

    Monday, October 28, 2013 1:02 PM
  • Hi There

    Wherever I put the code it either prints it on the screen or says "value type string cannot be added to a collection"

    below is the complete code for page 2 you will see where I create the combo box. I want after the ending <\comboBox> to check what the page title {Binding Title} is and the do something depending on what the title is

    any idea where I would insert your code

    The text is put in a textblock call pageTitle if that helps

    I have tried pageTitle.text and Title.text but they both dont work

    ______________________________________________________________

    code

    _____________________________________________________________________          

    <Page
        x:Name="pageRoot"
        x:Class="Monthly_Audits.ItemDetailPage"
        DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Monthly_Audits"
        xmlns:data="using:Monthly_Audits.Data"
        xmlns:common="using:Monthly_Audits.Common"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        RequestedTheme="Light"
        mc:Ignorable="d">

        <!--
            This grid acts as a root panel for the page that defines two rows:
            * Row 0 contains the back button and page title
            * Row 1 contains the rest of the page layout
        -->
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
            DataContext="{Binding Item}"
            d:DataContext="{Binding Groups[0].Items[0], Source={d:DesignData Source=/DataModel/SampleData.json, Type=data:SampleDataSource}}">
            <Grid.ChildrenTransitions>
                <TransitionCollection>
                    <EntranceThemeTransition/>
                </TransitionCollection>
            </Grid.ChildrenTransitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="140"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <!--
                TODO: Content should be placed within the following grid 
                      to show details for the current item
            -->
            <Grid Grid.Row="1" x:Name="contentRegion">
                       <ComboBox x:Name="comboBox1" SelectionChanged="ComboBox_SelectionChanged1" Margin="123,10,686,574">
                    <x:String>Item 1</x:String>
                    <x:String>Item 2</x:String>
                    <x:String>Item 3</x:String>
                </ComboBox>
    /// INSERT CODE HERE TO CHECK TITLE NAME AND DO SOMETHING
             </Grid>
            <!-- Back button and page title -->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                            Style="{StaticResource NavigationBackButtonNormalStyle}"
                            VerticalAlignment="Top"
                            AutomationProperties.Name="Back"
                            AutomationProperties.AutomationId="BackButton"
                            AutomationProperties.ItemType="Navigation Button"/>
                <TextBlock x:Name="pageTitle" Text="{Binding Title}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                            IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>
            </Grid>
       </Grid>
    </Page>

    Mark

    Monday, October 28, 2013 10:39 PM
  • Hi Guys

    OK i think I've had a breakthrough I put the following in the navigationHelper_LoadState event and it goes in without error :

            private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                // TODO: Create an appropriate data model for your problem domain to replace the sample data
                var item = await SampleDataSource.GetItemAsync((String)e.NavigationParameter);
                this.DefaultViewModel["Item"] = item;
                string fred = pageTitle.Text as string;
                if (fred == "Title1")
                {   
                                comboBox1.Items.Add("Barr");
                }
            }

    when i run it doesn't give any error but when I navigate to the Page called "Title1" the combobox doesn't change it still has the items defined before

    also I have no way of knowing whether the variable "fred" is actually containing the right title as C# doesn't appear to have a messagebox option

    i feel im almost there

    any ideas?

    mark


    • Edited by wambaugh1 Monday, October 28, 2013 11:19 PM
    Monday, October 28, 2013 11:16 PM
  • Ok Guys getting nearer added the following when the combobox is dropped

    private void comboBox1_DropDownOpened(object sender, object e)
            {
                string fred = pageTitle.Text as string;

                if (fred == "Title1")
                {
                    comboBox1.Items.Add(fred);


                }
            }

    This works perfect except it doesn't delete what is there just adds the title to the combobox. everytime you drop the box it adds it again and so on

    I have tried comboBox1.Items.Clear but it doesnt exist and causing a crash

    How do I clear the contents of the combobox when it is dropped?

    Mark

    Tuesday, October 29, 2013 12:06 AM
  • Try to Combobox1.ItemsSource = null;
    Tuesday, October 29, 2013 4:39 AM
  • CRACKED IT GUYS!!!

    this.comboBox1.Items.Clear();

    Clears the combobox

    Thank you for all your help pointing me in the right direction

    Mark

    • Marked as answer by wambaugh1 Tuesday, October 29, 2013 6:40 AM
    Tuesday, October 29, 2013 6:40 AM