Results 1 to 9 of 9

Thread: [RESOLVED] userControl questions

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] userControl questions

    I created a usercontrol to help elevate the flood of changes i need to make to a win form that has like 300 textboxes on it




    I'm new to user controls so sorry if this is a little newbie ish

    but i can create a control build the project and then put it on the main form

    i can create a load event but how do i "address" the items of the control


    for example my contorl has some combo boxes that i want to add items to

    could it be usercontrol1.combobox1.items.add(); or something like that?

    ive been playing around and googleing but i haven't found anything that i understand yet.


    Thanks if you can point me to the right direction.

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: userControl questions

    I need to understand - are the objects that this control is calling on the form?

    If so, you'll need to do something like this

    Code:
    public void ChangeText(ref ComboBox cboBox)
    {
    cboBox.Items.Add("item to add");
    
    }
    call this by doing this:
    Code:
    this.mycontrol.ChangeText(ref this.ComboBoxtoChange);
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: userControl questions

    i'm not sure i understand the question


    here is what i have: (sorry about the size)
    Areas in blue are the different types of user control



    before i started with the user control they were all individual items
    and i had a loop that would add all the teamnames to each combobox

    I also had a leave event and a text change event


    how can i translate that over to the user control so that i can have a leave event for the combobox inside of the user control

    and secondly

    say you had a user control with two combo boxes
    then you added 64 of thouse user controls to a form

    how would you add items to the combo boxes?

  4. #4
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: userControl questions

    I see, you're asking about making changes to the ComboBoxes that are INSIDE the User Controls, correct?

    Then you do it pretty much the same way. You have to expose those ComboBoxes to the outside world, and the best way, in my opinion, is to write a custom method in the UserControl that allows you to add items to it. So for instance, you might call from your form a method on the control called "AddItemToList", and you'd pass the item you want to add.

    Now since you so many of those different controls, I'd create my own class that has that method (to cut down on code), and extend the existing UserControl class and then have your controls inherit from it.

    It's important to remember that all a User Control is is an extendable form (in a way). It simply allows you to group controls & functionality together that can be re-used multiple times.
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: userControl questions

    im starting to get the idea.

    im still very weak when it comes to class's in general and inhertance is still just a vauge concept to me.

    I am trying to push my knowlege on this one but at a certian point i might have to finish it useing methods that i know rather than not finish it on time with a more exotic (if not more elegant) solution


    anyway


    so what your saying is that i would write code under the usercontrol itself to accept incoming changes?

  6. #6

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: userControl questions

    here is what i have so far

    (this is actually very cool)

    under the user control

    public void addtocombobox(string[] x)
    {
    CB_40_U.Items.Add(x);
    CB_40_L.Items.Add(x);
    }



    then from the main form

    string[] x = { "test", "test2" };
    matchType11.addtocombobox(x);


    and this i could loop

    foreach matchtype1 in tabpage.controls

    or something like that



    then i assume that i would make my sub on the main form public and then hook that up to the textleave for text validation

    Im really excited this will save me an S ton of work

  7. #7
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: userControl questions

    It's very important to understand classes and inheritance, especially if you want to pursue a development career - you'll lose out on great opportunities.

    Now, your control - yes, that is the best way to do it in this case, for the moment. Remember that controls on the UserControl are protected. They're not usually accessible from outside of the control, and you should not try to make them so. Methods are the best way of altering their information.

    Onto inheritance. By extending the existing UserControl class, you can add custom code only once and have all of your controls use it.

    This would be your code:
    vb Code:
    1. public class MyCustomControlClass : UserControl
    2.     {
    3.  
    4.         //private variables
    5.         protected string _myVariable = "";
    6.  
    7.         //my custom method
    8.         public void MyCustomMethod(int myParameter1, int myParameter2)
    9.         {
    10.             //my custom code
    11.  
    12.         }
    13.     }

    See where it says : UserControl? The colon signifies that this class is inheriting all of the properties, fields, methods, functions, and events of the class specified, in this case, UserControl. This is important for large apps, for instance, if you have a special method you want to run on all of your forms, don't cut & paste to each one - instead, extend the existing Windows Form class and change the class your forms inherit from to that class instead. They'd then have access to all Form objects and yours, as well.

    I suggest you research this topic and you'll find how much time it can save you!
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  8. #8
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: userControl questions

    Oh I also wanted to mention that you'll need to do this;

    Code:
    public void addtocombobox(string[] x)
    {
    CB_40_U.Items.Add(x[0]);
    CB_40_L.Items.Add(x[1]);
    }
    Because you passed that as an array, you have to include the indexer
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  9. #9

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: userControl questions

    well in this case no

    becuase i want them to match

    and it should be addrange i think

    ( im still working on it)

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