سؤال

I want to differentiate the obligatory TextBox fields in my forms. I was achieving this by creating a class with a method called by every form. What that method does, is receive a list of textbox and for each one, it sets the backcolor to another.

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

I have changed the GUI now, and I dont like how it looks like when the backcolor is changed, then I wanted to draw a rectangle as I show in the next picture:

enter image description here

Leave to me the gradient stuff, I just need help to draw a rectangle of the same height of each TextBox and to be located at the end of it. I want to use that method definition too (I mean, to receive a list of textboxes).

Notice that the methods are static..

I tried something like this:

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);
}

But is not working. It´s don´t even access the TextBoxRectangle(). I think I´m doing it very wrong..

هل كانت مفيدة؟

المحلول

My suggestion is: Don't use the complex logic to draw a rectangle. Can't you just simply put a background image in the textbox that looks like the rectangle you showed in the question ?. No need to draw rectangle and stuff.

The image can be anything gradient/not gradient.

نصائح أخرى

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));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top