0

I have a form that allows you to add multiple product ids to a buy. screenshot!

When you start the program here is what you see!

So every time you press a plus button another row appears!.

In order to avoid too many lines of code I decided to create a function so that it takes the number or the row where the plus button was pressed and adds 1 to it, so that the next row appears.

Right now it returns a string with the code, but I dont know how to execute it..

I have searched how to execute a string but the solution can never apply to my code.

//function
public string plus(int n)
        {
            string r="";

            r = "label" + (n + 1) + ".Visible = true;";
            r += "combobox" + (n + 1) + ".Visible = true;";
            r += "plusButton" + (n + 1) + ".Visible = true;";
            r += "minusButton" + (n + 1) + ".Visible = true;";

            return r;
        }

//plus button
private void plus1_Click(object sender, EventArgs e)
        {
            //code to execute (plus(1));
        }

Opinions and sugestions would be greatly appreciated, solutions even more!

mjkjhgf
  • 5
  • 1
  • 1
    Why do you want to make it some complicate? Why not doing in the code what you are trying to achieve with this string? Wpf, winforms, web, app? What's you frontend? – Mighty Badaboom Mar 27 '19 at 13:35
  • I also have a minus button, where it removes the row selected and all after it. The max rows are 20, imagine having 19 buttons with that much code (the minus button on the 2nd row would have to remove 18 rows, each with 4 component, so only in that button would be 72 lines of code... If I make this plus function work, the minus will work as well. I gave the plus example because it is less complicated to understand.. – mjkjhgf Mar 27 '19 at 13:40
  • 1
    You probably want a DataGridView with a `+` Button column. – Jimi Mar 27 '19 at 13:46

2 Answers2

1

Executing c# expression from string is really a bad idea and not what you want in this case. You could see this post for a detailed answer, but you really shouldn't.

I recommend you store all you component inside a collection like so.

public void plus(int n)
{
    labelsArray[n].Visible =
    comboboxArray[n].Visible =
    plusButtonArray[n].Visible =
    minusButtonArray[n].Visible = true;
}

private void plus1_Click(object sender, EventArgs e)
{
    plus(1);
}

You'd also need to declare new arrays to index your controls, and populate them with them. For exemple, for you labels array:

private Label[] labelsArray;


// Replace Form1 with the name of your class. This the constructor of your form.
public Form1()
{
    labelsArray = new [] {label1, label2, label3, ... };
}

Eventually, you could also create those controls dynamically instead of toggling their visibility.

Justin Lessard
  • 10,804
  • 5
  • 49
  • 61
  • doesn't work, says "The name 'labelsArray' does not exist in the current context", same error for all – mjkjhgf Mar 27 '19 at 13:44
  • @mjkjhgf You need to declare those variables and populate them with your controls. You should be able to work that out by yourself. – Justin Lessard Mar 27 '19 at 13:47
  • I'm afraid I don't understand.. sorry – mjkjhgf Mar 27 '19 at 13:50
  • @mjkjhgf I added an exemple on how to do it for your labels. Just repeat the logic for your other controls. – Justin Lessard Mar 27 '19 at 13:57
  • Thank you and sorry for the waiting, I felt sick :/ works like a charm! again, thank you very much!! – mjkjhgf Mar 27 '19 at 18:40
  • sooo.. the minus isn't working and I can't put my finger on it, any sugestions? `public void menos(int n) { labelsArray[n].Visible = cbidsArray[n].Visible = maisArray[n].Visible = menosArray[n].Visible = false; for (int i = n; i <= 20; i++) { labelsArray[n - 1 + i].Visible = false; cbidsArray[n - 1 + i].Visible = false; maisArray[n - 1 + i].Visible = false; menosArray[n - 1 +i].Visible = false; } }` – mjkjhgf Mar 27 '19 at 19:23
0

You don't need to define the code as a string. There are much better ways to do that. If you don't want to save your controls in separate collections and all your controls are child controls of your Form, you can run the same code with the following function:

public void plus(int n)
{
    ((Label)this.Controls.Find("label" + (n + 1), false)[0]).Visible = true;
    ((ComboBox)this.Controls.Find("combobox" + (n + 1), false)[0]).Visible = true;
    ((Button)this.Controls.Find("plusButton" + (n + 1), false)[0]).Visible = true;
    ((Button)this.Controls.Find("minusButton" + (n + 1), false)[0]).Visible = true;
}
Code Pope
  • 5,075
  • 8
  • 26
  • 68