0

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

4
  • Are these like multiple components per parent that repeat? If so, then a GroupBox, or a repeater, or there is also another control -- I beleive listbox to detail something that you can use to group them together, and have them act simularly. Also, if multiple checkboxes do the same thing, just point them at the same event handler. Commented Jan 30, 2020 at 19:14
  • @RobertMcKee from my understanding, each checkbox has the same functionality, display a textbox and enter a new component to replace the selected on a "submit" button Commented Jan 30, 2020 at 19:17
  • 1
    Then I would create a component that encapsulates all that functionality (called a UserControl), and put that 15-20 times on the form (easy), or add them dynamically as needed or in a loop. Here is some documentation on that: learn.microsoft.com/en-us/dotnet/api/… but you really need to load the project up in Visual Studio. There is a free version (Community) and that will help you immensely as it takes care of the designer files. Commented Jan 30, 2020 at 19:18
  • You can also write a CustomControl, but I urge you to not go down that route especially if you are new to the Desktop Applications. The UserControl is a drag/drop composite control that's easy for beginners to use. CustomControls are all code, but then you need to know a lot more about how controls work, their lifecycles, how to load/save/change state. They are more powerful than a UserControl, but a lot harder to implement correctly. Just realize there are UserControls and CustomControls and they are similar but very different. Commented Jan 30, 2020 at 19:23

2 Answers 2

1

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 });
Sign up to request clarification or add additional context in comments.

3 Comments

so instead of new System.Windows.Forms.Button() I can do new System.Windows.Forms.Textbox etc ?
You can do this with pretty much any kind of control. It's a little trickier with some controls (e.g. a button) because you also have to wire up the events programmatically as well. This answer could be improved by including an example of such.
@Travis I expanded the example to include other control types and events.
1

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.

2 Comments

Thanks for the info, I guess the thing that confuses me is the that I might be thinking as a PHP dev which is a mind of its own
@Travis I did some PHP myself, and most of the thinking seems to overlap with ASP.Net. So I think it is the Web Programmers way of thinking. I could look at the ASP.Net Page Lifecycle, and instantly see the similarities in basic programm flow: learn.microsoft.com/en-us/previous-versions/ms178472(v=vs.140) The biggest difference is that it is a whole lot easier to persist data between user inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.