簡體   English   中英

將矩形繪制為TextBox邊框

[英]Drawing Rectangle as TextBox Border

我在名為Validators的類中為文本框使用驗證方法。 我也在嘗試在無法驗證的文本框上繪制一個矩形。

我正在使用此代碼:

    private void TextBoxStyle(TextBox textBox)
    {
        Graphics graphics = textBox.CreateGraphics();
        Pen redPen = new Pen(Color.Red);

        graphics.DrawRectangle(redPen, textBox.Location.X, textBox.Location.Y, textBox.Width, textBox.Height);
    }

    /// <summary>
    /// Validates TextBoxes for string input.
    /// </summary>
    public bool ValidateTextBoxes(params TextBox[] textBoxes)
    {
        foreach (var textBox in textBoxes)
        {
            if (textBox.Text.Equals(""))
            {
                Graphics graphics = textBox.CreateGraphics();
                Pen redPen = new Pen(Color.Red);

                graphics.DrawRectangle(redPen, textBox.Location.X, textBox.Location.Y, textBox.Width, textBox.Height);

                return false;
            }
        }

        return true;
    }

問題是...矩形不會顯示。 我的代碼有問題嗎? 如果是,請幫助。

我看到一些潛在的問題:

  1. 您獲得了文本框的Graphics對象,但是在窗體中使用文本框的偏移量進行繪制。 最終結果:矩形在文本框的可見區域之外平移。 嘗試使用位置(0,0)
  2. 您繪制與文本框一樣寬的矩形。 最終結果:右邊緣和底邊緣將不可見。 您應該從這些值中減去筆的寬度。

在使用它時, 請檢查 ErrorProvider類。 它可能只是解決您的現成需求。

編寫用戶控件

  public partial class UserControl1 : UserControl
    {
        private string text;
         private bool isvalid = true;
        public string Text
        {
            get { return textBox.Text; }
            set { textBox.Text = value; }
        }

        public bool isValid
        {
            set
            {
                isvalid = value;
                this.Refresh();
            }
        }

        TextBox textBox = new TextBox();
        public UserControl1()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(UserControl1_Paint);
            this.Resize += new EventHandler(UserControl1_Resize);
            textBox.Multiline = true;
            textBox.BorderStyle = BorderStyle.None;
            this.Controls.Add(textBox);
        }

        private void UserControl1_Resize(object sender, EventArgs e)
        {
            textBox.Size = new Size(this.Width - 3, this.Height - 2);
            textBox.Location = new Point(2, 1);

        }

        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {
            if (isvalid)
                ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
            else
                ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);

        }
    }

更新:剛剛添加了isvalid屬性

您可以放置​​屬性以顯示邊框或不顯示邊框。 如果輸入有效,則顯示正常邊框,如果控制輸入無效,則顯示紅色邊框。

一旦以某種方式使TextBox控件無效,直接繪制到TextBox上的所有內容都將消失。

正確的方法是將用戶控件添加到項目中,並在其畫布上添加文本框。 在其周圍留一點邊框。

現在,您可以根據需要簡單地將用戶控件的畫布背景塗成紅色,看起來就像在TextBox周圍繪制了一個邊框。

您可以將代碼直接添加到用戶控件中,以便在文本更改時進行驗證。 這樣,您只需要編寫一次代碼,並向表單或頁面添加所需數量的TextBox。

您不應該僅從某個地方在控件上繪畫。 內置繪畫將在下一次覆蓋它。 控件有一個繪畫事件,您應該在其中繪畫。 需要繪畫時將使用該樣式。

在您的validate方法中,您應該只將驗證結果存儲在某個位置,以便可以在paint事件中使用它,並調用Invalidate()以便執行重新繪制。

    // You  may use this


    Label lblHighlight = new Label (); 
    Rectangle rc = new Rectangle(this.Left - 2, this.Top - 2, this.Width + 4, this.Bottom - this.Top + 4);
                    this.Parent.Controls.Add(lblHighlight);
                    lblHighlight.Bounds = rc;

lblHighlight.BackColor = "Red";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM