1
votes

Forgive me if I'm not posting to correct standards, but here is my question. I am using VS2017, C# Winforms development. I created a User Control that is simply just a Text Box. I'm capturing some events in this textbox and changing colors based on things happening, like entry/leave, and read only modes. Yesterday, as I was designing my forms everything was all fine and dandy. Today, I come back to my machine and now I cannot open the designer. I receive an error that says "The Designer cannot process the code at line 470, please see the Task List for details. The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again."

Here is the designer code it says is causing the issue

this.txtName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.txtName.BackColor = System.Drawing.Color.Transparent;
        this.txtName.Location = new System.Drawing.Point(139, 23);
        this.txtName.Name = "txtName";
        this.txtName.PasswordChar = "\0";
        this.txtName.ReadOnly = false;
        this.txtName.Size = new System.Drawing.Size(303, 19);
        this.txtName.TabIndex = 2;

Here is the code for my UserControl

    public partial class TextBoxCustom : UserControl
{
    Color onEnter = Color.Beige;
    private Color readOnly = Color.Silver;
    Color onLeave = Color.White;
    public string Text
    {
        get
        {
            return textBox1.Text;
        }
        set
        {
            textBox1.Text = value;
        }

    }

    public string Name
    {
        get { return textBox1.Name;}
        set { textBox1.Name = value; }
    }
    public bool ReadOnly
    {
        get { return textBox1.ReadOnly; }
        set
        {
            textBox1.ReadOnly = value;
            if (value == true)
            {
                textBox1.BackColor = readOnly;
            }
        }
    }
    public string PasswordChar
    {
        get { return textBox1.PasswordChar.ToString(); }
        set { textBox1.PasswordChar = Convert.ToChar(value); }
    }
    public void Clear()
    {
        Text = "";
    }
    public TextBoxCustom()
    {
        InitializeComponent();
    }

    private void TextBoxCustom_Leave(object sender, EventArgs e)
    {
        textBox1.BackColor = (textBox1.ReadOnly) ? readOnly : onLeave;
    }

    public void SetDataBindings(BindingSource source,string model)
    {

         textBox1.DataBindings.Add(new Binding("Text", source, model, true, DataSourceUpdateMode.OnPropertyChanged));
    }


    private void TextBoxCustom_Enter(object sender, EventArgs e)
    {

        textBox1.BackColor = (textBox1.ReadOnly)? readOnly: onEnter;
    }


}

Now, if I were to comment out line 470 like it says, I would receive an error on a different line. I've also commented out the whole block of designer code, and been able to open the designer, but the code just regenerates and throws the error again when I try and go back into it. I've tried cleaning/rebuilding/restarting visual studio. I've deleted the Bin and Obj folders, I've even made sure the UserControl is part of it's own separate project being referenced in the main app.

Thank you in advance for any help.

1
Does your project build successfully as-is? Have you done a clean build (and cleared your obj and bin directories)?Dai
Yes, my project builds successfully as-is. I can run the project, and the controls work as intended. I've cleaned and built/re-built numerous times as well as completely removed the obj and bin directories so they re-created themselvesw053lxl
this.txtName.PasswordChar = "\0"; produces a "String must be exactly one character long." message on my machine.LarsTech
Bingo! I was able to correct the issue from that - I changed my PasswordChar from a string to a char and corrected the references in the designer code. Solved the issuew053lxl
One basic reason you got into this trouble is because you used a UserControl instead of deriving your own class from TextBox. The latter is much superior since you don't have to forward all of these properties you want to still access. Override the OnEnter() method.Hans Passant

1 Answers

1
votes

This line of code in the designer:

this.txtName.PasswordChar = "\0";

produces the following message:

"String must be exactly one character long."

The TextBox control uses Char as the data type for the PasswordChar property instead of string. Switching it should resolve the issue.