[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

Beginner problem using combobox value

I have a basic silverlight project that I am doing in an attempt to learn the program.  I have a decent amount of experience programming office projects within the MS office suite using VBA, but have not done a lot of standalone programming.  My end goal is to create a rectangle that is animated based on the parameters given from a series of controls --

I am starting using a combobox to attempt to change the duration of the animation that I have defined on the rectangle.   I am having a problem simply getting the value from the combobox however.  

The combobox is defined as:

 <ComboBox   Height="27" Name="cboDuration" Width="114"  >
            <ComboBoxItem Content="1"  />
            <ComboBoxItem Content="2" IsSelected="True"  />
            <ComboBoxItem Content="3" />
            <ComboBoxItem Content="4" />

Currently I am simply trying to get the value that is selected out of the combobox and display it with a message box.  I have tried the following:  

MessageBox.Show(cboDuration.SelectedValue.ToString()) -- returns System.Windows.Controls.ComboBoxItem

MessageBox.Show(cboDuration.SelectedItem.ToString()) -- returns System.Windows.Controls.ComboBoxItem

MessageBox.Show(cboDuration.SelectedIndex.ToString()) -- returns the correctly selected Index -- not really useful though since I still cannot grab the value

MessageBox.Show(cboDuration.SelectedItem) (or SelectedValue) without the .ToString() results in a conversion error.  
  • 2
  • 2
1 Solution
Hi VoodoFrog,
Change your messagebox code to the following:


Hope that helps!
VoodooFrogAuthor Commented:
I am still just getting the System.Windows.Controls.ComboBoxItem  value showing up when I change it to what you suggest.  It seems that the SelectedItem and the Items.Item() refer to the same point.  

Could there be something wrong with the way I set the combobox up in the XAML?  it seemed simple enough, and with the selectedIndex it even seems to work...  
That is quite strange, i tried again and this code works correctly:

Dim cbi As ComboBoxItem
cbi = ComboBox1.SelectedItem
Use this

VoodooFrogAuthor Commented:
thank you urban -- it was the .Content that I was missing.  I appreciate your help.  

CB -- tried yours, I think the formatting doesn't work in VB, which I am using for this project.  

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now