如何解决ListBox iteam与其他列表iteam重叠
我正在使用一个列表框来添加日志以显示用户。一旦日志超出列表框的限制,它将启用滚动条并将selectedindex保留到最后一个。问题是,当我向上滚动时,它与不可读的第一行的最后一行重叠。
我正在使用此代码
lstSteppingCommands.Items.Add(DateTime.Now.ToString("MM/dd/yy hh:mm:ss.fff") + " | " + text + "..");
lstSteppingCommands.SelectedIndex = lstSteppingCommands.Items.Count - 1;
// Create a Graphics object to use when determining the size of the largest item in the ListBox.
Graphics g = lstSteppingCommands.CreateGraphics();
// Determine the size for HorizontalExtent using the MeasureString method using the last item in the list.
int hzSize = (int)g.MeasureString(lstSteppingCommands.Items[lstSteppingCommands.Items.Count - 1].ToString(),lstSteppingCommands.Font).Width;
// Set the HorizontalExtent property.
if (lstSteppingCommands.HorizontalExtent < hzSize)
{
lstSteppingCommands.HorizontalExtent = hzSize;
}
请检查并让我知道我在犯什么错误。
我的列表框属性为:
this.lstSteppingCommands.BackColor = System.Drawing.SystemColors.Window;
this.lstSteppingCommands.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lstSteppingCommands.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.lstSteppingCommands.Font = new System.Drawing.Font("Segoe UI",9.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));
this.lstSteppingCommands.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(84)))),((int)(((byte)(84)))),((int)(((byte)(84)))));
this.lstSteppingCommands.FormattingEnabled = true;
this.lstSteppingCommands.HorizontalScrollbar = true;
this.lstSteppingCommands.ItemHeight = 17;
this.lstSteppingCommands.Location = new System.Drawing.Point(30,82);
this.lstSteppingCommands.Name = "lstSteppingCommands";
this.lstSteppingCommands.Size = new System.Drawing.Size(697,187);
this.lstSteppingCommands.TabIndex = 0;
this.lstSteppingCommands.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.lstSteppingCommands_DrawItem);
我认为原因是,如果添加更多项目,则列表框大小可以容纳10个项目,然后出现滚动;如果我们向下滚动并选择最后一个项目,然后再次向上滚动到第一个项目,则它与最后一个项目重叠(选定的项目) )。可能是因为它试图在列表框中容纳该内容,并且由于大小不允许它覆盖在第一个iteam上,所以
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。