简体   繁体   中英

c# dynamically create system.windows.forms.label

I am in a bit of a pickle. Usually I am a PHP dev but for this role I am a C# developer. Yippee.

The project I am taking over has a very big deficiency inside, which is that it is not scale-able. Everything is hard coded inside and meant for one specific item. C# is new to me, and I am not sure where to look.

Current state of code is like so:

SomePage.Designer.cs:

private System.Windows.Forms.Label CompLbl1;
private System.Windows.Forms.Label CompLbl2;
private System.Windows.Forms.Label CompLbl3;
...
private System.Windows.Forms.Label CompLbl14;
private System.Windows.Forms.Label CompLbl15;
private System.Windows.Forms.CheckBox CompChkBx1;
private System.Windows.Forms.CheckBox CompChkBx2;
private System.Windows.Forms.CheckBox CompChkBx3;
...
private System.Windows.Forms.CheckBox CompChkBx14;
private System.Windows.Forms.CheckBox CompChkBx15;
private System.Windows.Forms.TextBox CompScanBox1;
private System.Windows.Forms.TextBox CompScanBox2;
private System.Windows.Forms.TextBox CompScanBox3;
...
private System.Windows.Forms.TextBox CompScanBox14;
private System.Windows.Forms.TextBox CompScanBox15;

SomePage.cs

private void CompChkBx1_CheckedChanged(object sender, EventArgs e)
    {
        if (Rwk1 == true)
        {
            Rwk1 = false;
            CompScanBox1.Visible = false;
        }
        else
        {
            Rwk1 = true;
            CompScanBox1.Visible = true;
        }

        // Console.WriteLine("Rework = " + Rwk1.ToString());
}
... all the way
private void CompChkBx15_CheckedChanged(object sender, EventArgs e){}

All of the above is hard coded.

This will break when implemented on a larger scale, say 20 components. Is there a way to make this a bit more dynamic? I'd appreciate a point in the right direction

WindowsForms Forms use partial classes . You work on one part. The Designer on another. What you both write is combined during compilation. And what the designer wrote is executed when you call InitializeComponents(); in the constuctor.

While you should not edit in the Designer part (it tends to seize up if you do that), you can fully look at how stuff is done. It only can do things that you can do. And technically all UI elements are created Dynamically at runtime to begin with, so there is nothing special to consider here.

The earliest time you should be doing any work like filling the UI with values is in the Shown() event. Earlier might cause hickups with elements not being there fully yet or long running operations disrupting the shwoing of the form. Stuff like creating the elements you can do in the constructor. Just do not forget to save it in a list or other collection.

Of course another problem of course is that you basically crossed over from Web(page) to Desktop programming. At least PHP indicates Web Programming. And Web Programming useses entirely different design patterns and programm flow from Desktop programming. That being said: While the MVC pattern is practially synonymous with dynamic Webpages nowadays, it started it's life for Desktop Programming.

You can dynamically create components and add them to the items in the current window. The tricky part doing it that way is getting the layout to work nicely (this is easier to do in WPF).

For example,

var myButton = new System.Windows.Forms.Button()
{
    Name = "MyButton",
    Location = new System.Drawing.Point(100, 100),
};
myButton.Click += (sender, args) =>
{
    //Do something here.
};

var myTextBox = new System.Windows.Forms.TextBox()
{
    Name = "MyTextBox",
    Text = "Default text"
};
myTextBox.TextChanged += (sender, args) =>
{
    //Do something here.
};

this.Controls.AddRange(new Control[] { myButton, myTextBox });

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