列表框水平滚动条无法正确更新

我有一个表单,其中包含一个列表框控件以及许多其他确定列表框内容的控件.将项目添加到列表框中可以正确设置滚动条的范围,但是更新列表框上的项目(通过this.lstResources.Items [index] = myUri)会使滚动条的范围减小到超过最大项目的宽度,从而切断最后几个字符.滚动条仍然可以使用,但是以这种方式更新列表框会导致列表中项目的滚动范围超出预期,并且是无法接受的.这是我实现列表框的方式:

public System.Windows.Forms.ListBox lstResources;
this.lstResources = new System.Windows.Forms.ListBox();

this.lstResources.FormattingEnabled = true;
this.lstResources.HorizontalScrollbar = true;
this.lstResources.Location = new System.Drawing.Point(331, 122);
this.lstResources.Name = "lstResources";
this.lstResources.Size = new System.Drawing.Size(307, 316);
this.lstResources.TabIndex = 8;
this.lstResources.Click += new System.EventHandler(this.ResourcesPage.LstResources_Click);

我在此列表框中执行的更改操作如下:

this.parentClassReference.lstResources.Items.Add(myUri);
this.parentClassReference.lstResources.Items[index] = myUri;
this.parentClassReference.lstResources.Items.RemoveAt(index);

我在窗体和列表框控件上都尝试过Refresh()和Update(),但都没有任何效果.我到处都在寻找列表框滚动条问题,但是似乎没有一个特有的重绘问题.

它应该是这样的:

这是实际发生的情况:

我觉得我缺少明显的东西,或者可能是错误地更改了项目,但是我对这个想法不熟悉.我对C#和UI设计并不完全陌生,但是我也不能说我也了解所有复杂的控件操作.任何帮助将不胜感激.

编辑1:这是应重现该问题的示例表格.我开始怀疑StringBuilder可能与更新部分有关,但是btnAdd_Click代码中也使用了它.

public partial class SampleListBoxForm : Form
{
    public Dictionary<string, string> CurrentUriQuery { get; set; }

    public SampleListBoxForm()
    {
        this.CurrentUriQuery = new Dictionary<string, string>();
        this.InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        this.UpdateParams("sampleApp");
        this.listBox1.Items.Add(this.GenerateURI());
        this.listBox1.Refresh();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        int index = 0;
        this.UpdateParams("sampleApp2");
        for (; index < this.listBox1.Items.Count; index++)
        {
            this.listBox1.Items[index] = this.GenerateURI();
        }
    }

    private void UpdateParams(string filename)
    {
        this.CurrentUriQuery = new Dictionary<string, string>();
        this.CurrentUriQuery["filename"] = filename;
        this.CurrentUriQuery["url"] = @"C:\Users\me\Desktop\" + filename;
        this.CurrentUriQuery["type"] = "dynamicType";
        this.CurrentUriQuery["p1"] = "foo";
        this.CurrentUriQuery["p2"] = "bar";
        this.CurrentUriQuery["p3"] = "stuff";
        this.CurrentUriQuery["p4"] = "test";
    }

    private string GenerateURI()
    {
        StringBuilder currentUri = new StringBuilder();
        bool firstParam = true;
        currentUri.Append(this.CurrentUriQuery["filename"]);
        foreach (KeyValuePair<string, string> pair in this.CurrentUriQuery)
        {
            if (pair.Key != "url" && pair.Key != "filename" && pair.Value != string.Empty && pair.Value != "0")
            {
                if (firstParam)
                {
                    currentUri.Append("?");
                    firstParam = false;
                }
                else
                {
                    currentUri.Append("&");
                }

                currentUri.Append(pair.Key);
                currentUri.Append("=");
                currentUri.Append(pair.Value.Replace(" ", ""));
            }
        }

        return currentUri.ToString();
    }

    public string[] ExtractPathAndQueryFromUriString(string uriString)
    {
        string[] pathAndQuery = new string[2];
        if (uriString.Contains('?'))
        {
            pathAndQuery[0] = uriString.Split('?')[0];
            pathAndQuery[1] = uriString.Split('?')[1];
        }
        else if (uriString.Contains("%3F"))
        {
            string[] stringSeparator = new string[] { "%3F" };
            pathAndQuery[0] = uriString.Split(stringSeparator, StringSplitOptions.None)[0];
            pathAndQuery[1] = uriString.Split(stringSeparator, StringSplitOptions.None)[1];
        }
        else
        {
            pathAndQuery[0] = uriString;
            pathAndQuery[1] = string.Empty;
        }

        return pathAndQuery;
    }
}

解决方法:

问题似乎出在与号字符之间.添加项目时,它似乎可以正确测量.当您更新项目时,它不会.

这种丑陋的骇客似乎是:记录索引,删除项目,将项目重新插入记录的索引.

或切换到DrawMode = OwnerDrawFixed并为您添加或更改的每个项目计算Horizo​​ntalExtent值.

private void ResizeListBox() {
  int maxWidth = 0;

  for (int i = 0; i < listBox1.Items.Count; i++) {
    int testWidth = TextRenderer.MeasureText(listBox1.Items[i].ToString(), 
                                             listBox1.Font, listBox1.ClientSize,
                                             TextFormatFlags.NoPrefix).Width;
    if (testWidth > maxWidth)
      maxWidth = testWidth;
  }

  listBox1.HorizontalExtent = maxWidth;
}

不幸的是,每次更改都必须调用它:

private void btnAdd_Click(object sender, EventArgs e) {
  this.UpdateParams("sampleApp");
  this.listBox1.Items.Add(this.GenerateURI());
  ResizeListBox();
}

private void btnUpdate_Click(object sender, EventArgs e) {
  this.UpdateParams("sampleApp");
  for (int index = 0; index < this.listBox1.Items.Count; index++) {
    this.listBox1.Items[index] = this.GenerateURI();
  }
  ResizeListBox();
}

这是一个快速而肮脏的DrawItem版本:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
  e.DrawBackground();
  if (e.Index > -1) {
    Color textColor = SystemColors.WindowText;
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
      textColor = SystemColors.HighlightText;
    }
    TextRenderer.DrawText(e.Graphics, listBox1.Items[e.Index].ToString(), 
                          listBox1.Font, e.Bounds, 
                          textColor, Color.Empty, TextFormatFlags.NoPrefix);
  }
}
上一篇:滚动条样式修改


下一篇:Vue的自定义滚动,我用el-scrollbar