[Webinar] Streamline your web hosting managementRegister Today

x
  • 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" />
</ComboBox>

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.  
0
VoodooFrog
Asked:
VoodooFrog
  • 2
  • 2
1 Solution
 
urban_smurfCommented:
Hi VoodoFrog,
Change your messagebox code to the following:

MessageBox.Show(cboDuration.Items.Item(cboDuration.SelectedIndex).ToString)

Hope that helps!
0
 
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...  
0
 
urban_smurfCommented:
That is quite strange, i tried again and this code works correctly:

Dim cbi As ComboBoxItem
cbi = ComboBox1.SelectedItem
MessageBox.Show(cbi.Content.ToString())
0
 
CuteBugCommented:
Use this

MessageBox.Show(((ComboBoxItem)cboDuration.Items[cboDuration.SelectedIndex]).Content.ToString());
0
 
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.  
0

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