By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,374 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,374 IT Pros & Developers. It's quick & easy.

Setting ToolStripComboBox Text Property At Runtime...

P: n/a
Perhaps it is because it is the end of the day and my eyes are tired of
looking at code but I cannot seem to figure out what I am doing wrong and
what I can do (if anything) to fix it.

On a Windows form, I have a ToolStripComboBox object with a handful of
entries in the Items collection. I would like to change the text of the
ToolStripComboBox object (using the Text property) to something other than
the text of one of the items in the items collection. For example, if the
items in the Items collection were "Item A", "Item B", and "Item C", I would
like to be able to set the text to "Item Z". If I manually type the text in,
it works. If I set the Text property at runtime, the text remains to be that
of the currently selected item.

Any suggestions on how to get around this will be greatly appreciated.

Thank you,

Jason Richmeier
Mar 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I was a bit baffled when I grabbed a copy of your test project and it worked
as expected. This led me to believe that there was something different
between your project and mine.

In my project, I am using the SelectedIndexChanged event and it is in this
event where I am attempting to change the Text propery of the
ToolStripComboBox object. When I add this event handler to your test project
and attempt to set the Text property there, it behaves just like my project
does. From my observation (which may be inaccurate), it appears that the
Text property gets set to what I set it to and then it is set back to the
text of the drop down list item sometime after the event handler finishes
execution. I am not sure how to get around this.

If you have any more suggestions I would greatly appreciate it.

Thank you,

Jason Richmeier

""Jeffrey Tan[MSFT]"" wrote:
Hi Jason ,

Thanks for your post!

I have created a little test project regarding your issue, however, I can
manipulate the ToolStripComboBox.Text property at runtime without any
problem. Attached is the test project. You can download it with Outlook
Express, not IE.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights

Mar 24 '06 #2

P: n/a
Hi Jason ,

Thanks for your feedback!

Yes, with SelectedIndexChanged event, I can reproduce out this problem.

After doing some debugging, I think the problem stems from native Win32
combobox behavior. The native combobox sends us the CBN_SELCHANGE
notification (= SelectedIndexChanged event) using SendMessage rather than
PostMessage. So its quite possible that the native combo does extra stuff
on return from this call. Its also possible they simply ignore certain
changes that occur during this message. For example, setting text has no
effect, but changing the selected index does seem to work.

Regarding this issue, we can use Control.BeginInvoke method to queue
another delegate on the UI thread, then this delegate will execute after
the SelectedIndexChanged event, which works well in our scenario. Like this:

private void SetComboBoxTest()
{
this.toolStripComboBox1.Text += "test";
}
private void toolStripComboBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
this.BeginInvoke(new MethodInvoker(SetComboBoxTest), null);
}
This works well in the sample project.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.