Combobox problems

  • Thread starter Jesper, Denmark
  • Start date
J

Jesper, Denmark

Hi,

I'm using a combobox in DropDown style. It shows the time in hours for a
proces. In the drop down list I've put in some values like

15 min
30 min
45 min
1 h
2h
etc...

When these are selected, they are converted into hours. This number is
written in the Text property of the cmb_control. I.e. when 45 min is
selected, I want the combobox to show 0.75. However, It's like the Text
property is not reacting (or cannot override a selected index). I've tried
deselect by setting the SelectedIndex property to -1 before writing to the
Text property of the control, but it does not work either.

How can I enforce the Text property of the combobox shown after a
SelectedIndexChanged event.

Any idea to resolve this problem?

Regards Jesper.
 
N

Nicholas Paldino [.NET/C# MVP]

Jesper,

This doesn't resolve your issue, but from a UI standpoint, it's a little
confusing to have one thing displayed in the drop down list, and then
another displayed in the box when you select that item. Wouldn't it just be
easier to have one consistent representation for the value?
 
J

Jesper, Denmark

From a programming point of view yes, but not for my purposes.
However I just found 5 mins ago an article where another guy had the same
problem. It was resolved with delegates:

please have a look at

https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2357193&SiteID=1
(or see snippet below)

....and tell me what you think, is it a hack?

SNIPPET ********************
private delegate void ChangeTextDelegete();

public void ChangeText()
{
DataRowView dv = this.comboBox1.SelectedItem as DataRowView;
this.comboBox1.Text = dv["value"].ToString();
this.comboBox1.SelectionStart = this.comboBox1.Text.Length;
}

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new ChangeTextDelegete(ChangeText));
}
END SNIPPET ******************************

Nicholas Paldino said:
Jesper,

This doesn't resolve your issue, but from a UI standpoint, it's a little
confusing to have one thing displayed in the drop down list, and then
another displayed in the box when you select that item. Wouldn't it just be
easier to have one consistent representation for the value?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jesper said:
Hi,

I'm using a combobox in DropDown style. It shows the time in hours for a
proces. In the drop down list I've put in some values like

15 min
30 min
45 min
1 h
2h
etc...

When these are selected, they are converted into hours. This number is
written in the Text property of the cmb_control. I.e. when 45 min is
selected, I want the combobox to show 0.75. However, It's like the Text
property is not reacting (or cannot override a selected index). I've tried
deselect by setting the SelectedIndex property to -1 before writing to the
Text property of the control, but it does not work either.

How can I enforce the Text property of the combobox shown after a
SelectedIndexChanged event.

Any idea to resolve this problem?

Regards Jesper.
 
N

Nicholas Paldino [.NET/C# MVP]

Jesper,

The comment wasn't about the programming point of view. From a UI
perspective, it just doesn't make sense.

Regardless, the reason why this works is that it calls BeginInvoke.
BeginInvoke will send the windows message to call the delegate, but not wait
for it to be processed, which means that it returns immediately, and then
will be processed sometime after the current event handler exits.

It is a hack, IMO, but without fundamentally changing the combobox, it's
probably your best shot right now.

You should be aware that it's not guaranteed that the delegate will be
called immediately after the method that calls BeginInvoke exists. It's
completely possible that there will be other windows messages that are
placed in the queue to be processed beforehand, and those have to be
processed before the delegate is executed.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jesper said:
From a programming point of view yes, but not for my purposes.
However I just found 5 mins ago an article where another guy had the same
problem. It was resolved with delegates:

please have a look at

https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2357193&SiteID=1
(or see snippet below)

...and tell me what you think, is it a hack?

SNIPPET ********************
private delegate void ChangeTextDelegete();

public void ChangeText()
{
DataRowView dv = this.comboBox1.SelectedItem as DataRowView;
this.comboBox1.Text = dv["value"].ToString();
this.comboBox1.SelectionStart = this.comboBox1.Text.Length;
}

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new ChangeTextDelegete(ChangeText));
}
END SNIPPET ******************************

Nicholas Paldino said:
Jesper,

This doesn't resolve your issue, but from a UI standpoint, it's a
little
confusing to have one thing displayed in the drop down list, and then
another displayed in the box when you select that item. Wouldn't it just
be
easier to have one consistent representation for the value?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message
Hi,

I'm using a combobox in DropDown style. It shows the time in hours for
a
proces. In the drop down list I've put in some values like

15 min
30 min
45 min
1 h
2h
etc...

When these are selected, they are converted into hours. This number is
written in the Text property of the cmb_control. I.e. when 45 min is
selected, I want the combobox to show 0.75. However, It's like the Text
property is not reacting (or cannot override a selected index). I've
tried
deselect by setting the SelectedIndex property to -1 before writing to
the
Text property of the control, but it does not work either.

How can I enforce the Text property of the combobox shown after a
SelectedIndexChanged event.

Any idea to resolve this problem?

Regards Jesper.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

No, this will not help you. You basically need to handle the repainting
of the entire drop down yourself, so you have to override the combobox
behaivor to do that.
 

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