C# 什么';在所有者绘制的列表框中使用控件的正确方法是什么?
我正在试验所有者绘制的列表框。我正在向列表框中的特定项添加文本框。但是,当我开始滚动时,文本框不会显示在正确的位置。正确的方法是什么? 这是我正在使用的代码C# 什么';在所有者绘制的列表框中使用控件的正确方法是什么?,c#,.net-2.0,listbox,controls,ownerdrawn,C#,.net 2.0,Listbox,Controls,Ownerdrawn,我正在试验所有者绘制的列表框。我正在向列表框中的特定项添加文本框。但是,当我开始滚动时,文本框不会显示在正确的位置。正确的方法是什么? 这是我正在使用的代码 Form1.cs Program.cs 编辑为了更清楚,我重新写了我的答案 下面的列表框显示一个文本框及其SelectedItem GetItemRectangle(int index) 用于检索列表框中项目的当前位置。这似乎是你正在寻找的关键方法 public class ListBoxEx : ListBox { publi
Form1.cs
Program.cs
编辑为了更清楚,我重新写了我的答案 下面的列表框显示一个文本框及其SelectedItem
GetItemRectangle(int index)
用于检索列表框中项目的当前位置。这似乎是你正在寻找的关键方法
public class ListBoxEx : ListBox
{
public ListBoxEx()
{
TextBox.Visible = false;
Controls.Add(TextBox);
}
private readonly Size TextBoxOffset = new Size(16, 16);
private const Int32 CellHeight = 40;
private readonly TextBox TextBox = new TextBox();
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
TextBox.Visible = SelectedIndex != -1;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
// Somehow necessary
e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds);
// Drawing the item's text
e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(ForeColor), e.Bounds);
// Drawing the item's borders
e.Graphics.DrawRectangle(new Pen(ForeColor), e.Bounds);
// Drawing updating the TextBox location
if (SelectedIndex != -1)
TextBox.Location = Point.Add(GetItemRectangle(SelectedIndex).Location, TextBoxOffset);
// Because clicking the scrollbar sometimes cause the ListBox to hide the TextBox
TextBox.BringToFront();
// Making sure the TextBox is redrawn ASAP
TextBox.Invalidate();
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
e.ItemHeight = CellHeight;
}
}
我同意过度骑行更好。不过,我原来的问题仍然存在。textbox控件的新位置应该是什么?如果我使用e.Bounds计算它,那么我会得到文本框的位置问题。如果我使用绝对值进行计算,则结果是文本框不会显示(或未渲染?)。我的代码在下一个commentvoid listBox1_DrawItem(对象发送者,DrawItemEventArgs e){PointF displayLocation=new PointF(20,e.Index*cellHeight+3);Debug.Print(“DrawItem::Index={0},bounds={1}”,e.Index,e.bounds);e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(),displayFont,displayBrush,e.Bounds);e.Graphics.DrawRectangle(displayPen,e.Bounds);if(e.Index==3){listBox1.Controls.Clear();//item3Text.Location=新点(10,e.Bounds.Y+18);item3Text.Location=新点(10,e.Index*cellHeight+18);Debug.Print(“DrawItem::location={0}”,item3Text.location);item3Text.Focus();item3Text.BringToFront();}}}我相信您要查找的方法是ListBox.GetItemRectangle(请参阅我编辑过的答案)。
namespace ListBoxControlScrollIssue
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
GetItemRectangle(int index)
public class ListBoxEx : ListBox
{
public ListBoxEx()
{
TextBox.Visible = false;
Controls.Add(TextBox);
}
private readonly Size TextBoxOffset = new Size(16, 16);
private const Int32 CellHeight = 40;
private readonly TextBox TextBox = new TextBox();
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
TextBox.Visible = SelectedIndex != -1;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
// Somehow necessary
e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds);
// Drawing the item's text
e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(ForeColor), e.Bounds);
// Drawing the item's borders
e.Graphics.DrawRectangle(new Pen(ForeColor), e.Bounds);
// Drawing updating the TextBox location
if (SelectedIndex != -1)
TextBox.Location = Point.Add(GetItemRectangle(SelectedIndex).Location, TextBoxOffset);
// Because clicking the scrollbar sometimes cause the ListBox to hide the TextBox
TextBox.BringToFront();
// Making sure the TextBox is redrawn ASAP
TextBox.Invalidate();
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
e.ItemHeight = CellHeight;
}
}