Results 1 to 13 of 13

Thread: [2.0] Most efficient way to output 20 or so textboxes+labels

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    [2.0] Most efficient way to output 20 or so textboxes+labels

    I'm curious which is the best method of showing around 20 text boxes , each with their own label. Best as in, most efficient (performance wise).

    1) Have 20 seperate labels + 20 seperate text boxes (build the app using the standard controls, possibly using the panel to assist in lining them up)

    2) Create a user control with a text box + label, then use this control 20 times.

    3) Create 1 big user control with everything together.

    My program will look like:

    input description - [text box]
    input description - [text box]
    input description - [text box]
    input description - [text box]
    input description - [text box]
    input description - [text box]
    input description - [text box]

    output description - [read-only text box]
    output description - [read-only text box]
    output description - [read-only text box]
    output description - [read-only text box]
    output description - [read-only text box]
    output description - [read-only text box]

    The likelihood of adding more fields at a later time is slim to none, and if I did, I really wouldn't mind re-designing the control (if I went with option #3), because the program would need to be recompiled anyways.

    Most of the text boxes will require a different type of validation too (float, double, int, max length, etc.), but visually each one will be the same size.

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    I can't see why any way would be more efficient than another, really. If anything I would imagine just laying them out separately would carry the lightest footprint, since if you go the usercontrol route, your usercontrol still contains the textbox and label objects, and ditto with the panel, only now you have a usercontrol object taking up memory too (or the panel)

    Easiest is another story.. Creating the controls in a loop, using the tag property creatively to describe what type of validation is required, a nice switch statement, and a single validating function would be the least amount of code probably..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Quote Originally Posted by conipto
    I can't see why any way would be more efficient than another, really. If anything I would imagine just laying them out separately would carry the lightest footprint, since if you go the usercontrol route, your usercontrol still contains the textbox and label objects, and ditto with the panel, only now you have a usercontrol object taking up memory too (or the panel)

    Easiest is another story.. Creating the controls in a loop, using the tag property creatively to describe what type of validation is required, a nice switch statement, and a single validating function would be the least amount of code probably..

    Bill
    Yeah, you need to do this with a loop, or else you will end up with a ton of copy and paste. I suggest and Array list of a a control that has both a textbox and a label.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Everything is created with code. If you create a form in the designer then all that does is write code for you, but it is still code being executed at run time. In your case there seems to be absolutely no reason to do anything other than just add the controls to your form as normal. The only reason to use a container would be if you needed to handle all those controls as a block for some reason, like diabling all of them in one go. The UserControl is even more pointless unless you either want to reuse the controls as a group, be able to add them as a group at run time or else hide some of the inner workings of them and encapsualte them in a single class. As far as lining them up, the IDE already plenty of advanced tools for that. The snap-lines feature is all you need in most situations. If you will need to move some or all of the controls based on other controls or the form then you may want to use a TableLayoutPanel or FlowLayoutPanel, but they are unnecessary too unless things will be moving or resizing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Thanks for the replies.

    Btw, what benefit would using a loop give me over manually laying them out in the designer?

    Correct me if I'm wrong, but couldn't I just assign that .Tag property to each text box, write a custom event handler for KeyDown, and point each textbox to that event?

    Then run a switch on the Tag to determine which text box the user is on, and do whatever I need to do based on the Tag.

    example:

    Code:
    private void InputTextKeyDownHandler(object sender, KeyEventArgs e)
    {
       TextBox tb = sender as TextBox;
       
       switch tb.Tag
       {
           case "1": // ...
       }
    }
    If I made the controls through code, not only would I need to write a seperate switch (for positioning/size -- 2 of the text boxes will end up being a different width than the rest), but I wouldn't be able to see the controls themselves in the designer.

    Unless I'm missing something, hmm...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    You don't need the Tag to do that. You can test which TextBox by testing the TextBox itself against your member variables.
    Code:
    if (tb == this.textBox1)
    {
    }
    elseif (tb == this.textBox2)
    {
    }
    Plus, if you need to handle every text box differently then you should have different event handlers. What's the point of accumulating all the controls into a common event handler only to separate them out and handle them differently?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    A lot of the same code will be used for each validation.

    Let's say I have 3 text boxes (there's really 9, but 3 is enough for this example):

    text box #1's rules:
    Only allows 0-9 and a negative sign, can only be -XXX to XXX in value. Must validate to float.

    text box #2's rules:
    Only allows 0-9, can only be XX to XX. Must validate to Int.

    text box #3's rules:
    Only allows 0-9 and a decimal, must validate to float, no other rules other than it can't be <0.

    In this case, all 3 text boxes are being stripped of every key other than 0-9 (and in some cases a negative/decimal). All my inputs will have that rule (numbers, negative and decimals only (based on which text box I'm on)). Having to do that for each textbox's keydown handler seemed like too much copy/paste.

    Edit:

    Wait, I think I missed the point of your post...

    You're saying I should point each textbox to that 1 handler, filter out which keys are allowed, and then run a seperate function (validation) based on which text box is currently in use?
    Last edited by ThisIsMyUserName; Apr 16th, 2006 at 09:13 PM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    There are a number of ways you could do this. You could use a different event handler for each control that all call the same validation method. You could use the same event handler for all controls that contains all the code for every control. You could use the same event handler that then calls different methods depending on the control. In my opinion, the most professional method would be to use the same event handler and different ones. You can handle multiple events with the same method, but you can also handle the same event with multiple methods. You could create an individual event handler for each control to perform the individual part, then have a common event handler for the common part. You can only use the designer to add one event handler per event. You have add any additional event handlers manually. You can edit the .Designer.cs file but the changes will be lost next time it's updated. You should do it in the constructor or Load event handler in the regular code file if you want to do it this way. Note that when the event is raised the event handlers will be executed in the order they were added, so the one you set in the designer will be executed first, followed by any you have added manually and in the same order they are added in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Remember, I've been coding in c#/.NET for roughly a week. I'm a newbie. Layed out in english, you're saying I should do this:

    1> Create a "global" event that first strips everything other than 0-9, decimal, negative.

    2> In the designer, point each text box to this event.

    3> In the form load area add this:

    this.textInput1.KeyDown += new System.Windows.Form.KeyEventHandler(this.text1ValidationEvent)

    Repeat this for all my text boxes (this.text2ValidationEvent , etc.). Then write the specific validation methods and name them accordingly?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Quote Originally Posted by ThisIsMyUserName
    Remember, I've been coding in c#/.NET for roughly a week. I'm a newbie. Layed out in english, you're saying I should do this:

    1> Create a "global" event that first strips everything other than 0-9, decimal, negative.

    2> In the designer, point each text box to this event.

    3> In the form load area add this:

    this.textInput1.KeyDown += new System.Windows.Form.KeyEventHandler(this.text1ValidationEvent)

    Repeat this for all my text boxes (this.text2ValidationEvent , etc.). Then write the specific validation methods and name them accordingly?
    Not bad for a self-proclaimed newbie.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Thanks for guiding me in the right direction. Now the fun part, writing the actual code, instead of battling .NET's inner workings lol. The funny thing is, this past week I've barely gotten to code anything in actual C#. It's mostly been trying to figure out how .NET works, and learning the IDE.

  12. #12
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    The part about tag I was mentioning is something like this in your input validation function..
    Code:
    switch (((TextBox)sender).Tag)
    {
       case "Double" : //Validate double only input  here
       case "Integer": //Validate integer's only input here.
       case "String" : //no Validation?
    {
    So that you don't need ten different validating routines..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    56

    Re: [2.0] Most efficient way to output 20 or so textboxes+labels

    Hmm that is pretty clever, and definitely would work well except each text box needs a seperate validation (deeper than just "is double" "is int"). Sorry I didn't make that clear originally.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width