2013-01-17 31 views
0

我想區分表單中的強制性TextBox字段。我通過用每種形式調用的方法創建一個類來實現這一點。該方法所做的是接收一個文本框列表,併爲每個文本框設置另一個背景顏色。在每個文本框內繪製一個矩形c#winform

public static void Mostrar_campos_obligatorios(List<TextBox> lista_textbox) 
{ 
    foreach (TextBox tbx in lista_textbox) 
    { 
     tbx.Paint += new PaintEventHandler(TextBoxRectangle); 
    } 
} 

我現在已經改變了圖形用戶界面,而且我不喜歡它的外觀時,背景色是變了樣,然後我想畫一個矩形,如我在接下來的畫面:

enter image description here

給我留下漸變的東西,我只需要幫助繪製每個文本框高度相同的矩形並位於它的末尾。我也想使用該方法定義(我的意思是,接收文本框列表)。

請注意,該方法是靜態的..

我想是這樣的:

public static void TextBoxRectangle(object sender, PaintEventArgs e) 
{ 
    tbx = (TextBox)sender; 
    Color c1 = Color.FromArgb(255, 113, 255, 0); 
    Color c2 = Color.FromArgb(255, 2, 143, 17); 

    LinearGradientBrush br = new LinearGradientBrush(e.CellBounds, c1, c2, 90, true); 

    ColorBlend cb = new ColorBlend(); 
    cb.Positions = new[] { 0, (float)1 }; 
    cb.Colors = new[] { c1, c2 }; 

    br.InterpolationColors = cb; 

    Rectangle rect = new Rectangle(tbx.Location.X + 4, tbx.Location.Y + 4, 13, 13); 

    e.Graphics.FillRectangle(br, rect); 
} 

但不工作。它甚至不訪問TextBoxRectangle()。我認爲I'm做非常錯誤..

回答

0

我的建議是: 不要使用複雜的邏輯來繪製一個矩形。 難道你不能只是簡單地把一個背景圖像放在文本框中,看起來像你在問題中顯示的矩形?。不需要繪製矩形和東西。

圖像可以是任何漸變/不是漸變。

+0

是的,我真的用表情混合。但我爲我的應用程序使用Winforms。毫無疑問,這是最好的答案。我真的在做一些簡單的事情,一些非常複雜的事情。謝謝! – Andres

0
public static void Mostrar_campos_obligatorios(List<TextBox> lista_textbox, PaintEventHandler eventHandler) 
{ 
    foreach (TextBox tbx in lista_textbox) 
    { 
     tbx.Paint += new PaintEventHandler(eventHandler); 
    } 
} 

...

Mostrar_campos_obligatorios(lista_textbox, new PaintEventHandler(TextBoxRectangle));