0

I'm attempting to take a string value from a Combo box then pass it through an object variable to a class and store it there in a string variable.

 private void cboTimeZone_SelectedValueChanged(object sender, EventArgs e)
 {
     extTime1.timeZone = cboTimeZone.SelectedItem;
 }

I'm not totally use to the combo box options to use so.

5
  • 1
    "Passing a string value from a ComboBox through an object variable to a class into a string variable" -- What exactly do you mean by thet? Does it mean that you want to store the current text value of a ComboBox in another object's string field/property? Then it's a simple assignment. Commented Jan 29, 2011 at 17:58
  • >> My poor explanation in that content is poor. I want to store the current text value of the ComboBox selectedValue in a string variable in a class. extTime1 is an object variable of that class. Commented Jan 29, 2011 at 18:01
  • 1
    possible duplicate of Can't get Value from ComboBox Commented Jan 29, 2011 at 18:03
  • 1
    @allthosemiles: The following explanation about terminology is somewhat off-topic here, but still valuable to know: In C#, you usually don't call an class' fields and properties "variables"; same as you wouldn't call methods "functions" or "procedures". (So, if you say "string variable in a class", some people like me would probably reach the conclusion that you meant a static field of type string in some unnamed class, which is not what you apparently mean.) Commented Jan 29, 2011 at 18:16
  • Okay. thank you. ^_^ Still getting use to this. And all of the terminology. Commented Jan 29, 2011 at 18:23

2 Answers 2

1

Does the cboTimeZone contain string objects? In this case, a simple cast should be enough if extTime1.timeZone is a string:

extTime1.timeZone = (string)cboTimeZone.SelectedItem

if cboTimeZone was filled with objects of type myObject, u can use the ToString() method on the item if you overwrote it in your myObject class:

extTime1.timeZone = cboTimeZone.SelectedItem.ToString()

If you selected a specific property MyProperty of myObject to be shown in the combo box, you can first cast to the object and then access the property by using

extTime1.timeZone = ((myObject)cboTimeZone.SelectedItem).MyProperty

to get that property as a result.

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I've never seen a placement of (string) like that. That was what i needed.
Since everything derives from Object and ToString is a member of Object, there is no need for the additional cast in the second example.
+1 yeah, you are right, thanks for the hint. It only may be interesting to cast to the object if you want to link to a certain property. I will change that above.
1

It's not clear from your question whether your ComboBox is data-bound or not; either way, I think it would be a good idea to first figure out if SelectedItem is indeed the correct property to use, or if there's another, more appropriate one.

If you've set a DataSource for your ComboBox, you've probably also set a DisplayMember. In that case, the DisplayMember will determine which property of the currently selected item of the data source will be shown in the ComboBox as text.

If you've set a ValueMember, you can also use the SelectedValue property to retrieve that property of the currently selected data source item.

SelectedItem simply retrieves the currently selected data source item. This may be a complex object, or a string object, or something else; check with your data source.

The ComboBox's Text property simply contains the text that's currently displayed in the ComboBox's text input field and has type string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.