how can i make my combobox to show it's max index at all times?

Dani AI

Generated

Clarifying the two possible goals here: either (A) have the ComboBox display the highest value as the current selection after you rebuild the list, or (B) have the drop‑down list scroll so its last item is visible when the user opens it. pointed at selecting the last item, and posted a working handler that repopulates the list and avoids reentrancy — that solved 's case. Below are practical, robust patterns and a few gotchas to avoid regressing later.

Use a simple guard and BeginUpdate/EndUpdate when you rebuild items so you do not reenter the selection handler and the control does not flicker. After you refill the Items collection set the selection programmatically to the last element so the control shows the new maximum. If you want the list to actually open and show that item, set DroppedDown = true after adjusting the selection.

Example pattern (replace ComputeNewMax with your logic):

private bool updatingCombo;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (updatingCombo) return;
    updatingCombo = true;

    comboBox1.BeginUpdate();
    int newMax = ComputeNewMax(); // your logic here
    comboBox1.Items.Clear();
    for (int i = 1; i <= newMax; i++)
        comboBox1.Items.Add(i.ToString());
    if (comboBox1.Items.Count > 0)
        comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
    comboBox1.EndUpdate();

    updatingCombo = false;
}

Troubleshooting / cautions: if the ComboBox is data‑bound change SelectedValue/SelectedIndex on the data source rather than Items. If Sorted is true and your items are numeric strings, lexicographic sorting will misorder "10" vs "2" — either sort numerically or use fixed-width strings. To control how many items appear without scrolling use MaxDropDownItems/IntegralHeight. For advanced control of the drop list scroll position you can use the native CB_SETTOPINDEX message, but that requires P/Invoke and is rarely necessary for the described scenario.

Recommended Answers

All 6 Replies

You mean the last item in it?

In that case it would be combobox.SelectedItemIndex = combobox.Items.Count - 1;

This is just a guess though, since I don't have time to try it out for now. Give it a go and tell me if it is what you seek.

If you are talking about the dropdown height, there is a property for that setting.

it is not working.
the options are selecteditem
or selected index.
from some reason it is not working.

if i have 10 numbers in my combobox
after i pick 3 i have a function which updates the combobox numbers to be from 1 to 7. and i want 7 to appear on the combobox window.

I have no idea why anyone would ever want to do this, but here you go...

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
            int i = Convert.ToInt32(comboBox1.SelectedItem.ToString());
            int j = Convert.ToInt32(comboBox1.Items[comboBox1.Items.Count - 1]) -i;
            
            comboBox1.Items.Clear();
            for (int z = 1; z <= j ; z++)
                comboBox1.Items.Add(z.ToString());
            if(comboBox1.Items.Count > 0)
                comboBox1.Text = comboBox1.Items[comboBox1.Items.Count - 1].ToString();
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        }

// Jerry

That does seem like a weird course of action to take. Well...Good job I guess Jerry :p

ok i got what i needed
thanks jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.