日期:2014-05-17  浏览次数:20721 次

窗体类的控制TextBox边框问题
C# code

            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Red, 2.0f);

            foreach (Control ctr in this.Controls)
            {
                if (ctr is TextBox)
                {
                    g.DrawRectangle(pen, new Rectangle(ctr.Location, ctr.Size));

                }

            }
//以上正常,在当前窗体里面的TextBox控件加了边框
            foreach (Control ctr1 in this.Controls)
            {

                TabControl tab = ctr1 as TabControl;
                if (tab != null)
                {
                    foreach (TabPage tp in tab.TabPages)
                    {

                        foreach (Control c in tp.Controls)
                        {
                            if (c is TextBox)
                            {
                               //这里设置边框不行
                                g.DrawRectangle(pen, new Rectangle(c.Location, c.Size));
                                c.Text = "abc";//这里有效,证明c已经找到了TextBox控件
                                 //这里为什么没有BorderStyle这个属性呢?
                                // c.BorderStyle = System.Windows.Forms.BorderStyle.None;


                            }
                        }
                    }
                }
            }



            pen.Dispose();
        }


  //这里设置边框不行
  g.DrawRectangle(pen, new Rectangle(c.Location, c.Size));
  c.Text = "abc";//这里有效,证明c已经找到了TextBox控件
  //这里为什么没有BorderStyle这个属性呢?
  // c.BorderStyle = System.Windows.Forms.BorderStyle.None;


------解决方案--------------------
(c as TextBox).BorderStyle
------解决方案--------------------
重绘控件,最好还是自己继承一个重绘的好,以避免添加太多的Paint事件。

C# code

/// <summary>
    /// CFlatTextBox:浮动文本框
    /// </summary>
    [ToolboxBitmap (typeof(FlatTextBox), "FlatBox.bmp")]    
    public class FlatTextBox : System.Windows.Forms.TextBox