Question Smarter way to deal with multiple buttons?

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
So after asking my last question on here, I managed to remove over a 1,000 lines of code....which was pretty impressive.

So thought I would ask this question to see if there was a smarter way to see if I could reduce my code further.

My form has 24 buttons. Currently, each button_click event has a separate entry and code - some of it quite simplistic:

C#:
private void sd1but_Click(object sender, EventArgs e)
        {
            sdNobox.Text = HoldSD1.Text;
            hcodbox.Text = HoldCode1.Text;
            projtbox.Text = HoldProj1.Text;
            descbox.Text = desctbox1.Text;
        }

That just recalls the text back to where I need it for the program to then manipulate that data on a different button press.

Not too problematic, but the next set of buttons has this code:

C#:
private void resbut1_Click(object sender, EventArgs e)
        {
            string data = HoldSD1.Text + "," + HoldCode1.Text + "," + HoldProj1.Text + "," + HoldTime1.Text + "," + desctbox1.Text;
            string[] Lines = File.ReadAllLines("../../../dataset1.txt");
            File.Delete("../../../dataset1.txt");// Deleting the file
            using (StreamWriter sw = File.AppendText("../../../dataset1.txt"))
            {
                foreach (string line in Lines)
                {
                    if (line.IndexOf(data) >= 0)
                    {
                        continue;
                    }
                    else
                    {
                        sw.WriteLine(line);
                    }
                }
            }
            HoldSD1.Text = "";
            HoldCode1.Text = "";
            HoldProj1.Text = "";
            HoldTime1.Text = "";
            desctbox1.Text = "";
        }

This code essentially removes the line of text from a txt file, so that when I reload the program, it reads from that txt file and only reloads those items that have not been removed - I have another txt file that contains all entries for later evaluation.

There are 12 of each button, each ending with 1 to 12.

This all works, but I was wondering if there was a cleverer way of dealing with multiple buttons like this to see if I could reduce the code I am creating? I can create and add the buttons to Panels if that is helpful, but not sure that it is?

Happy to go research, but have no idea what I would be looking for at the moment.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
Off the top off my head grouping controls into user controls sounds like a better idea. So you'll just have 12 user controls, and just write the button handler once for each of the 2 buttons that go into the user control instead of 24 button handlers.

Otherwise, use helper methods that take structs/classes that are simple DTOs (data transfer objects). Then create 12 instances of these DTOs. In your 24 button handlers you just pass in the appropriate DTO.
 
Top Bottom