简体   繁体   中英

When typing text in textbox, keep background constant

I'm writing a program that starts out with requiring a certain amount of lines of text and for that I use a TextBox . To make the program look nice, I put a background image on the form. Now I don't want the TextBox to put a large white block on the image, so for that I made the TextBox have a transparent background. But here's the problem: as soon as I start putting text in the TextBox , the lines that have text will revert back to the white background that I don't want. So how can I stop my program from doing that?

I can't post images yet, so I'll just use links:

This image shows the background as I have it and how I want it to be:

This image shows what happens when I start typing:

I want the background to just remain the same while I type (of course the text colour should then be lighter, but the textbox.forecolor seems to have no effect.

So below is the code I have so far, I hope you can help me, I'm still quite new to this :)

public class NieuwSpel : Form
{
    Label spelerslijst, nummer;
    TextBox spelersInput, spelnr;
    Button OK;

    public NieuwSpel()
    {
        this.BackgroundImage = WeerwolvenvanWakkerdam.Properties.Resources.Background_NieuwSpel;
        this.FormBorderStyle = FormBorderStyle.Fixed3D;

        spelerslijst = new Label();
        spelerslijst.Location = new Point(10, 10);
        spelerslijst.Text = "Voer hier de spelerslijst in:";
        spelerslijst.Width = 200;
        spelerslijst.BackColor = Color.Transparent;
        spelerslijst.ForeColor = Color.White;
        this.Controls.Add(spelerslijst);

        spelersInput = new CustomTextBox();
        spelersInput.Location = new Point(10, 40);
        spelersInput.Size = new Size(200, 300);
        spelersInput.Multiline = true;
        spelersInput.BackColor = Color.FromArgb(100, 100, 100, 100);
        spelersInput.ForeColor = Color.White;
        spelersInput.GotFocus += this.setColour;
        this.Controls.Add(spelersInput);

        OK = new Button();
        OK.Text = "Start Spel!";
        OK.Location = new Point(110, 430);
        OK.Click += this.Start;
        this.Controls.Add(OK);

        nummer = new Label();
        nummer.Text = "Spelnummer:";
        nummer.Width = 75;
        nummer.Location = new Point(10, 360);
        nummer.BackColor = Color.Transparent;
        nummer.ForeColor = Color.White;
        this.Controls.Add(nummer);

        spelnr = new CustomTextBox();
        spelnr.Width = 50;
        spelnr.Height = 20;
        spelnr.Location = new Point(90, 360);
        spelnr.BackColor = Color.FromArgb(100, 100, 100, 100);
        spelnr.ForeColor = Color.White;
        this.Controls.Add(spelnr);
    }

    public void setColour(object o, EventArgs ea)
    {
        ((CustomTextBox)o).BackColor = Color.FromArgb(100, 100, 100, 100);
    }
}

public partial class CustomTextBox : TextBox
{
    public CustomTextBox()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor |
             ControlStyles.OptimizedDoubleBuffer |
             ControlStyles.AllPaintingInWmPaint |
             ControlStyles.ResizeRedraw |
             ControlStyles.UserPaint, true);
    }
}

This probably won't be easy in WinForms . If you're just messing around on your own and trying to learn, you might want to consider playing around with WPF . A lot of people still have to deal with WinForms , but I've developed in both and WPF definitely supersedes it.

It can provide the desired effect out-of-the-box:

在此输入图像描述

<Window x:Class="SampleWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:SampleWpf"
        Title="MainWindow" Height="250" Width="400" >
    <Window.Background>
        <ImageBrush ImageSource="images.jpg" />
    </Window.Background>
    <Grid>
        <TextBox Margin="5" Background="Transparent" Text="HELLO THERE!"
                 FontSize="20" FontWeight="Bold" Foreground="White" />
    </Grid>
</Window>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM