Avoid resize of combobox while dropdown visible and adding items?

Avoid resize of combobox while dropdown visible and adding items?

Post by Tm90cmUgUG » Fri, 15 Dec 2006 15:05:01


Hello,

If I place a standard .NET combobox on a Windows Form, I can write code such
that items are added to the list while the combobox's dropdown is displayed.
The following sample code uses a timer to accomplish this (I added a combobox
named comboBox1 to the form):

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//populate combobox with some initial items
for (int i = 0; i < 10; i++)
comboBox1.Items.Add(i);
_addItemsTimer = new Timer();
_addItemsTimer.Interval = 3000;
_addItemsTimer.Tick += new EventHandler(_addItemsTimer_Tick);
}

void _addItemsTimer_Tick(object sender, EventArgs e)
{ //add a bunch more items to the dropdown
for (int i = 15; i < 55; i++)
comboBox1.Items.Add(i + offSet);
offSet = offSet + 50;
}

private void comboBox1_DropDown(object sender, EventArgs e)
{
//start timer that will add items to dropdown list
_addItemsTimer.Start();
}

private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
_addItemsTimer.Stop();
}

private Timer _addItemsTimer;
private int offSet;
}

If I click the dropdown icon on the combobox, the initial list of items is
displayed. After waiting three seconds, more items are added. Here's the
problem: when the new items are added, the size of the dropdown window
greatly increases (the first time items are added). This is rather ugly.
Worse, if the form happens to be near the bottom of the screen, then if the
dropdown is expanded and items are added, the dropdown list window extends
beyond the desktop size. The user cannot scroll down to see additional
items. When the dropdown is closed and reopened, the dropdown window height
returns to normal. How can I avoid the dropdown size (height) changing while
still adding items to the list when the dropdown is displayed?

Thanks,
Notre
 
 
 

Avoid resize of combobox while dropdown visible and adding items?

Post by QWxCcnVBb » Fri, 15 Dec 2006 16:05:01

Are you required to display the items as they're being added to the combobox?
If not, you might consider executing comboBox1.BeginUpdate prior to adding
the items to the combobox and then executing comboBox1.EndUpdate after you've
added the items. You won't see the items as they are being added and you may
not see them after they've been added, but this last matter could be overcome
by executing comboBox1.Refresh.