当前位置: 代码迷 >> .NET组件控件 >> 怎么能让控件获得输入焦点
  详细解决方案

怎么能让控件获得输入焦点

热度:3450   发布时间:2013-02-25 00:00:00.0
如何能让控件获得输入焦点
自定义一个控件,当该控件获得焦点时用光标并可以接受用户输入

类似于TEXTBOX,应该怎么实现。

------解决方案--------------------------------------------------------
那看来只能模拟了,随便写了一个代码参考下:)
private List <char> list;
public Form1()
{
InitializeComponent();
list = new List <char> ();
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyValue == 8)
{
if (list.Count > 0)
{
list.RemoveAt(list.Count - 1);
}
}
else
{
list.Add((char)e.KeyValue);
}
this.PaintText(this.ClientRectangle);
base.OnKeyDown(e);
}
private void PaintText(Rectangle rect)
{
if (this.Visible && !this.IsDisposed)
{
BufferedGraphicsContext myContext = BufferedGraphicsManager.Current;
BufferedGraphics buffer = myContext.Allocate(this.CreateGraphics(), rect);
if (buffer.Graphics != null)
{
buffer.Graphics.Clear(this.BackColor);
drawString(buffer.Graphics, rect);
}
buffer.Render();
buffer.Dispose();
}
}
private void drawString(Graphics g, Rectangle rect)
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
string str = new string(list.ToArray());
g.DrawString(str, this.Font, SystemBrushes.ControlText, rect, format);
}
  相关解决方案