Array of textboxes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Array of textboxes?

    I'm making a program to help me upload files to my site en masse. I've already sorta created the commandline argument portion of it, but I want to create options for each filename passed via commandline, so I can define where on the server each file will go. For example, if I were to select 4 files and pass them to the program via commandline, I want 4 groups of textboxes to show up, with 2 textboxes per group. The first would simply be to show the file to be uploaded, and would be read only, the second would be to define the upload path. I'd probably also have buttons in each group to remove a file fom the upload queue, and a button seperate from the groups to add another group to select another file to upload.

    How would I go about making an array (I assume that's what this will need to be) of textboxes that I can iterate through and process each one individually? In PHP I can simply make the name attribute of the textbox something like "somename[]" and no matter how many I have, as long as they're named that name, they will be passed to the script as an array.

    I'm not sure if I'm explaining what I want to do properly, so let me know if it doesn't make sense.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I've done a lot of dynamic control generation like this, it's not so bad once you get the hang of it. A handy tip to remember is that you can name objects that inherit from control, then you can access them from a form object's control list by that name.

    Example...

    Code:
    Button newButton = new Button();
    newButton.Name = "btn0";
    this.Controls.Add(newButton);
    
    ...
    
    Button btn0 = this.Controls["btn0"] as Button;
    if (btn0 != null) ...
    Now, for the generation of your text box array itself, you've got two options.

    A) Throw them directly on your form or in a panel, changing the size and location of your text boxes as you generate them. The benefit here is that you can place them exactly where you want them in any order, but you have to keep track of, and increment locations.

    B) Put them on a FlowLayoutPanel . This wonderful little control will automatically organize controls you place on it (even when you re-size it!) and you can change properties on the control to change how the controls go on (ie, left to right, up to down, etc...). The plus side is obvious, but the downside is that you can only really get a grid of controls, you can't do anything else.

    So, your task broken down further...

    1) Define your loop to generate your controls
    2) Create a new object for your control
    3) Name your object
    4) Set it's properties according to whatever guidelines you've defined.
    5) Use method A or B above to place the object on your form, or some kind of panel.

    Hopefully that gets you started.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      Thanks, this is a way better explanation than the other one I found. I did manage to create the array of textboxes, and also was able to read each one's value. Now I think I just need to take it a step further and generate a panel, place my textboxes and buttons on it, then add the panel to the form, allowing me to better control the layout and group the controls more effectively. Hopefully I can make something nicely workable.

      I managed to pass the command line arguments (a few files I selected and dragged onto the executable) to the program and generate a new textbox for each argument passed that was a valid filename. I dragged 8 files onto the executable and the program opened with 8 textboxes and a button that I could click that would messagebox the value of each textbox, showing me that my logic is sound and that I can iterate through the array to get the values.

      Thank you for the reply, I'll be sure to ask you if I have any more questions.

      Comment

      Working...