2009-10-21 87 views
3

我有一个按钮的平面样式的图形用户界面。我想使用具有相同外观的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 
+0

如果你想要这个控件是只读的,它很可能是最好取下内文本框和一个标签或直接与绘制控件的表面上的文本替换它' Graphics.DrawString(...)'。当然,你会失去TextBox的内置功能,包括点击它的能力,并开始键入光标所在的位置。 – MusiGenesis 2009-10-23 11:52:02

回答

8

将文本框的BorderStyle设置为FixedSingle

更新:没有简单的方法来控制TextBox的边框宽度,但您可以通过创建自己的UserControl轻松地创建此效果。基本上,您只需将UserControl的BackColor设置为SystemColors.WindowFrame,然后在BorderStyleNone控件上放置一个TextBox。在控件的Resize事件中,您可以重新定位文本框,以便它留下2像素或5或任何您想要的边框(这就是UserControl本身显示的通过边缘)。

更新2:我已经写了名为 “ThextBox” 样本用户控件(用于 ICK-接壤牛逼extBox)具有可调边框:

public partial class ThextBox : UserControl 
{ 
    private TextBox _TextBox; 
    private int _BorderWidth = 1; 

    [Browsable(true)] 
    [DesignerSerializationVisibility 
     (DesignerSerializationVisibility.Visible)] 
    public override string Text 
    { 
     get 
     { 
      return _TextBox.Text; 
     } 
     set 
     { 
      _TextBox.Text = value; 
     } 
    } 

    [DesignerSerializationVisibility 
     (DesignerSerializationVisibility.Visible)] 
    public bool Multiline 
    { 
     get 
     { 
      return _TextBox.Multiline; 
     } 
     set 
     { 
      _TextBox.Multiline = value; 
      ResizeMe(); 
     } 
    } 

    [DesignerSerializationVisibility 
     (DesignerSerializationVisibility.Visible)] 
    public bool UseSystemPasswordChar 
    { 
     get 
     { 
      return _TextBox.UseSystemPasswordChar; 
     } 
     set 
     { 
      _TextBox.UseSystemPasswordChar = value; 
     } 
    } 

    [DesignerSerializationVisibility 
     (DesignerSerializationVisibility.Visible)] 
    public char PasswordChar 
    { 
     get 
     { 
      return _TextBox.PasswordChar; 
     } 
     set 
     { 
      _TextBox.PasswordChar = value; 
     } 
    } 

    [DesignerSerializationVisibility 
     (DesignerSerializationVisibility.Visible)] 
    public int BorderWidth 
    { 
     get 
     { 
      return _BorderWidth; 
     } 
     set 
     { 
      _BorderWidth = value; 
      ResizeMe(); 
     } 
    } 

    public ThextBox() 
    { 
     InitializeComponent(); 
     this.BackColor = SystemColors.WindowFrame; 
     _TextBox = new TextBox(); 
     _TextBox.Multiline = false; 
     _TextBox.BorderStyle = BorderStyle.None; 
     this.Controls.Add(_TextBox); 
     ResizeMe(); 
    } 

    protected override void OnFontChanged(EventArgs e) 
    { 
     base.OnFontChanged(e); 
     ResizeMe(); 
    } 

    private void ResizeMe() 
    { 
     if (this.Multiline) 
     { 
      _TextBox.Height = this.Height - (2 * _BorderWidth); 
     } 
     else 
     { 
      this.Height = _TextBox.Height + (2 * _BorderWidth); 
     } 
     _TextBox.Width = this.Width - (2 * _BorderWidth); 
     _TextBox.Location = new Point(_BorderWidth, _BorderWidth); 
    } 

    private void ThextBox_Resize(object sender, EventArgs e) 
    { 
     ResizeMe(); 
    } 
} 

若要使用此代码在您的项目中,将名为“ThextBox”的UserControl添加到您的项目中,并将此代码粘贴到设计人员添加的项目上。该控件具有可调节边框,并且与设计器“很好地发挥”,允许您在设计模式下设置所有相关属性。它还会自动保留输入的文本,边框宽度,密码字符等内容。

如果需要通过包括额外的特定文本框的属性来扩展此(如MaxLengthRightToLeft,只要按照例如在该控制的PasswordChar属性,其中一个像这样直接从文本框继承用户控件的代替的优点将你的控制将自动拥有所有的文本框属性但是,你不能这样做边框这样

+0

但是,我怎样才能调整线的宽度? – yeyeyerman 2009-10-21 15:15:17

+0

@MusiGenesis:是否有办法获得对边界的更多控制权,或者是否必须绘制自己的控制权? – 2009-10-21 15:32:19

+0

@Robert:我认为有一种控制边框宽度的Windows API方式,但我不确定,它无论如何都是一个大的PITA。只需在稍大的UserControl上放置一个无边界TextBox是实现这种效果的简单方法。 – MusiGenesis 2009-10-21 15:38:59

0

您可以在c#中的文本框控件上设置边框样式。在我的系统中,它们已被默认设置为“平坦”风格。

+0

他们在哪里可以平放? – yeyeyerman 2009-10-21 15:20:55

0

您是否尝试过BorderStyle属性设置为BorderStyle.FixedSingleBorderStyle.None

2

选项1:?

稍微难看哈克:(不推荐,除非是绝对必要

你可以有没有边框文本框,并将其添加到面板比文本框大x像素。通过改变Panel(并保持TextBox在中间),你可以改变TextBox边框的外观。

选项2:

更健壮的解决方案,但更多的是痛苦的。您可以创建自己的自定义控件并从TextBox继承。通过将文本框的边框设置为无,您可以将自己的边框绘制到控件上。这样做的好处是你可以精确地控制边框的样子(例如圆角等),但是会被警告,这会侵犯文本框中的文本并且看起来更粗糙。你可以做这样的事情:

public class BorderedTextBox: TextBox 
{ 
    protected override void OnPaint(PaintEventArgs pe) 
    { 
     base.OnPaint(pe); 

     // Draw your border here 
    } 
} 
+0

您对上述代码的评论将非常感谢......谢谢! – yeyeyerman 2009-10-22 10:11:24

相关问题