Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've googled a bit and managed to find a few articles on how to add controls from within a running application, but neither seem to explain it in a way that suits my needs. I wonder if anybody can help me understand how I can go about this, or direct me to a resource that will do the same.

I'm writing a recipe book application, and as part of the process of adding/editing a recipe, I want to be able to let the user add or remove ingredients from a list. Obviously, there's no way to know how many ingredients will be in any given recipe, so I cannot add that many textboxes beforehand.

I want to have a textbox with a +/- button to the right of it like you see on many websites, so that when the user clicks the +, it will make a new control below it, and if they click the -, then it will delete that control. Also, while it may be a slightly different topic, I'd also like to know how I can let the user re-arrange the textboxes so they display accordingly on a printout.
Posted

Handle the Click event for the add and remove buttons:
C#
private Button runTimeButton = null;
private void butAdd_Click(object sender, EventArgs e)
    {
    Button but = sender as Button;
    if (but != null)
        {
        if (runTimeButton == null)
            {
            runTimeButton = new Button();
            runTimeButton.Text = "Added!";
            runTimeButton.Location = new Point(but.Location.X, but.Location.Y + 30);
            runTimeButton.Click += new EventHandler(runTimeButton_Click);
            Controls.Add(runTimeButton);
            }
        }
    }

void runTimeButton_Click(object sender, EventArgs e)
    {
    MessageBox.Show("Added Button Clicked!");
    }

private void butRemove_Click(object sender, EventArgs e)
    {
    if (runTimeButton != null)
        {
        Controls.Remove(runTimeButton);
        runTimeButton.Click -= runTimeButton_Click;
        runTimeButton = null;
        }
    }

However, if you are looking at the user adding lots of information, you might want to consider using a DataGridView instead - it is designed for the user to add and remove rows.

When it comes to printing, don't let users move anything!
Don't try to print the actual form - it will look horrible. Instead, use the PrintDocument class[^] to handle your print outs - it gives you full control over what and where things are printed, including fonts, sizes, colours, etc., which do not have to be related in any way to the screen display.
 
Share this answer
 
Regarding the printing question, I guess I could have been more clear.

I have it setup to store the recipe data in an XML document, and am using XSL and CSS so that the document will display in a normal web browser if the user chooses. It also uses this same technology to display the recipe from within my winform. I want the user to be able to order the ingredients in the list in whatever order makes logical sense. The webpage will be what gets printed.

I'll look into the datagridview control, though. Sounds like it may be the best option.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900