-1

I am creating my textbox programmatically in a console application that builds a form window on the fly. I am trying to get Input boxes such as the textbox to show up invisible but still allow the user to input data such as username and password or any other customisation fields I provide. This is for a game launcher and I am attempting to make it NOT look like a windows component. I have tried some of the solutions on the post below.

Transparency for windows forms textbox

EDIT: As you can see above I have already cited that this does not solve my issue. I do not use the form designer as it has a nasty habit of deleting my code because I presume "It knows better".

The Accepted answer for that does not work for me as I do not use the form designer and InitializeComponent(); Does not work it just tells me that it is not a function of the component. I have gotten as far as this.

using System.Windows.Forms;

namespace Launcher_Namespace
{
    public class TransparentTextBox : TextBox
    {
        public TransparentTextBox()
        {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
    }
}

And in the main body of code that initialises fields

            //Initialise Inputs
            _username = new TransparentTextBox();
            _username.Bounds = new Rectangle(120, 10, 120, 21);
            _username.BackColor = Color.Transparent;
            _username.BorderStyle = 0;
            _username.Visible = false;

But all this has achieved is allow me to set _username.BackColor = Color.Transparent; Without throwing an error. The Input box remains White with no border. I just want to make the background transparent. Even MSDN recomends this solution but It does not work for me. My only solution left is to build a custom Label class that grabs the inputs and reads the key inputs and adds them to the .Text property but I don't want to do this.

Community
  • 1
  • 1

2 Answers2

2

The solution in your linked answer works fine. If you're not using the designer it doesn't matter... you can still use the same solution. InitializeComponent() is simply a method that's created by the code generator in the designer file. If you ever want to know what it does to create controls (it can be very informational to have a look) then create a control using the designer and then inspect the .Designer.cs file.

EDIT: It acts a little funny. You can override OnPaint to fix the white background and disappearing text, see below. Not a "finished" implementation, the cursor doesn't seem to know where to go, but this should get you in the right direction.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 3; i++)
            {
                var x = new UserControl1 {Location = new Point(0, i*20)};
                this.Controls.Add(x);
            }
        }
    }

    public  class UserControl1 : TextBox
    {
        public UserControl1()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.ResizeRedraw |
                 ControlStyles.UserPaint, true);
            BackColor = Color.Transparent;
            TextChanged += UserControl2_OnTextChanged;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var backgroundBrush = new SolidBrush(Color.Transparent);
            Graphics g = e.Graphics;
            g.FillRectangle(backgroundBrush, 0, 0, this.Width, this.Height);          
            g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF(0,0), StringFormat.GenericDefault);
        }

        public void UserControl2_OnTextChanged(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • 1
    Which would sort of make this question a duplicate. =D – J. Steen Feb 04 '16 at 16:48
  • 1
    Absolutely. I'll vote to close as a dupe in a minute. Just wanted to try to help the poor guy, he sounds frustrated. – DrewJordan Feb 04 '16 at 16:50
  • If it was a duplicate I would have not been required to ask this question. Clearly the difference here is that the solution is not the same as the solution in other threads. This is not the same issue. – That Homeless Guy Feb 04 '16 at 17:02
  • This works but as soon as I type the background becomes white again. It sometimes becomes transparent again but all the text becomes invisible. It's java all over again. I'm just going to build a custom label to do the job. This doesn't work properly. – That Homeless Guy Feb 04 '16 at 17:05
  • 1
    you might try a WPF project if that's an option. I'm looking to see if there's a workaround for the white background. – DrewJordan Feb 04 '16 at 17:19
  • I don't use WPF because it has a habit of overwriting my code. I will also note that the project requires to be initially built as a console app. I am searching for a solution also. – That Homeless Guy Feb 04 '16 at 17:22
  • Maybe redraw it each time it updates but that doesn't account for the text becoming invisible too. – That Homeless Guy Feb 04 '16 at 17:27
  • I had something similar when trying to overwrite OnPaint. It seems to work ok with some flashing visible which is not exactly ideal but better than it was. I am also getting the stressed out cursor issue. I may just go ahead and try a label implementation and compare the two. I know a label won't flash when you update the text. – That Homeless Guy Feb 04 '16 at 20:01
  • Thank you sir. @DrewJordan I might add that I am not disposing of this and am still refining it to get rid of some of those weird redraw issues. I am going to try labels as well though. – That Homeless Guy Feb 04 '16 at 20:05
0

When we use SetStyle(ControlStyles.UserPaint,true) Control Border Doesn't paint. I did this in Textbox. My textbox border style is FixedSingle but after using setstyle with UserPaint Textbox border is not drawn. Textbox appears like border is set to None.

Ishrar
  • 105
  • 1
  • 7