我有一个按钮的平面样式的图形用户界面。我想使用具有相同外观的TextBox控件,但是我找不到可以在哪里配置外部行。 WinForms中有没有可以给FlatStyle的控件?谢谢!有没有办法在C#中实现平面文本框?
编辑1
感谢约FixedSingle边框样式的信息,但后来,我怎样才能改变线条的属性?
编辑2
我已经实现两者的一点点的解决方案。我想如果你能帮助改进这个课程,因为我不是C#的专家,而且我觉得这个代码有点混乱。下面是代码:
class BorderTextBox : UserControl
{
private TextBox m_textBox;
private int m_borderSize;
private void ResizeComponent()
{
m_textBox.Size = new Size(Size.Width - 2 * m_borderSize, m_textBox.Size.Height);
Size = new Size(Size.Width, m_textBox.Size.Height + 2 * m_borderSize);
}
protected override void OnResize(EventArgs z_event)
{
base.OnResize(z_event);
ResizeComponent();
}
public BorderTextBox()
{
SuspendLayout();
// TextBox
m_textBox = new TextBox();
m_textBox.BorderStyle = BorderStyle.None;
m_textBox.Name = "textBox";
m_textBox.TabIndex = 0;
// Body
BackColor = Color.Black;
Name = "Body";
Controls.Add(m_textBox);
ResumeLayout(false);
PerformLayout();
}
public bool UsePasswordStyle
{
get { return m_textBox.UseSystemPasswordChar; }
set { m_textBox.UseSystemPasswordChar = value; }
}
public int BorderSize
{
get { return m_borderSize; }
set
{
m_borderSize = value;
m_textBox.Location = new Point(m_borderSize, m_borderSize);
ResizeComponent();
}
}
}
编辑3
我有在实施只读属性的一些问题。我试图阻止编辑框处理OnClick事件,并在内部显示intermitent游标。当我在此类中定义OnClick方法时:
class BorderTextBox : UserControl
{
...
protected override void OnClick(EventArgs e)
{
if (!ReadOnly)
base.OnClick(e);
}
...
}
此方法只获取边框上的点击,但不在文本框内。有没有办法捕捉到这些事件?或者如何删除组件中元素的事件处理程序?
m_textBox.Click -= //the EventHandler we don't want
如果你想要这个控件是只读的,它很可能是最好取下内文本框和一个标签或直接与绘制控件的表面上的文本替换它' Graphics.DrawString(...)'。当然,你会失去TextBox的内置功能,包括点击它的能力,并开始键入光标所在的位置。 – MusiGenesis 2009-10-23 11:52:02