开发者

Windows Forms ListBox fails to properly resize horizontal scrollbar when adding item

开发者 https://www.devze.com 2023-03-09 23:44 出处:网络
I\'m working on an application that has a ListBox that I can populate by loading a file or by clicking an \"Add\" button which shows a dialog to prompt some information from the user. After hitting \"

I'm working on an application that has a ListBox that I can populate by loading a file or by clicking an "Add" button which shows a dialog to prompt some information from the user. After hitting "Save" from that dialog, the result is then added to the ListBox. However, for some reason after this takes place, the horizontal scrollbar does not resize properly.

Often it will appear, but the farthest right it will go still obscures most of the text. Oddly enough, upon double-clicking an item the same dialog is shown, I click cancel, and the scrollbar properly resizes. I've tried lstItems.Refesh() to no avail, the only workaround that causes the scrollbar to properly resize from this "add" prompt is to add the result twice, then remove one. This is the relevant code:

Original code for add prompt (does not work):

private void btnItemAdd_Click(object sender, EventArgs e)
{
    editForm editFrm = new editForm();
    editFrm.ShowDialog();
    if (editFrm.Result != null)
    {
        lstItems.Items.Insert(lstItems.Items.Count, editFrm.Result);
        lstItems.Refresh();
    }
}

Workaround:

private void btnItemAdd_Click(object sender, EventArgs e)
{
    editForm editFrm = new editForm();
    editFrm.ShowDialog();
    if (editFrm.Result != null)
    {
        lstItems.Items.Insert(lstItems.Items.Count, editFrm.Result);
        lstItems.Items.Insert(lstItems.Items.Count, editFrm.Result);
        lstItems.Items.RemoveAt(lstItems.Items.Count - 1);
        lstItems.Refresh();
    }
}

Possibly relevant. The code for the doubleclick event:

private void lstItems_DoubleClick(object sender, EventArgs e)
{
    if (lstItems.SelectedItem != null)
    {
        editForm editFrm = new editForm(lstItems.SelectedItem.ToString());
        editFrm.ShowDialog();
        lstItems.Items.Insert(lstItems.SelectedIndex, editFrm.Result);
        lstItems.Items.RemoveAt(lstItems.SelectedIndex);
        lstItems.Refresh();
    }
}

Why is this happening and is there a possible solution?

Edit: It appears as if someone has downvoted my question. I'd appreciate if you left a comment as to why it deserved a downvote rather than a hit and run of sorts :/ Was the question not clear? I'd be happy to try and explain more if necessary. I suspect it is due to lack of "research". I could link to a Google result if you'd like. Apparently no one else, to the best of my searching, has encountered this problem. I tried using the HorizontalExten开发者_运维知识库t property as shown here, but that didn't work either.


It sounds like the remove line causes the scrollbar to be re-evaluated but the add does not. One way that I can think of to force this functionality would be to invalidate and redraw the listbox like so:

lstItems.Invalidate();
lstItems.Redraw();
0

精彩评论

暂无评论...
验证码 换一张
取 消