问题描述
我正在使用一个列表框来添加日志以显示用户。一旦日志超出列表框的限制,它将启用滚动条并将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 (将#修改为@)