Setting ToolStripComboBox Text Property At Runtime...

G

Guest

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
 
G

Guest

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
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top