Results 1 to 14 of 14

Thread: Controls in a class

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Controls in a class

    I am confused about something. Say I create a new class "Test". Then in that class I add a control to my form, and we will say it is a textbox. Now say back on my form1.cs file I want to change a property of that form, how in the world do I do that. I keep running into problems like that. Am I missing something?
    If I helped you please rate me.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Controls in a class

    Your explanation doesn't really make sense (apart from it being a C# question in a VB forum!)...

    How do you mean you add a control to your form in your class? Are you actually creating a new textbox and assigning it to the form's control's collection as part of some routine in the class?

    When you say "back on my form1.cs file I want to change a property of that form" what do you mean - do you mean you want to change a property of the new control rather than the form?

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Controls in a class

    I'm hoping we're talking about theoreticals, since it's not a good idea for a class to be adding controls to a form (generally speaking, there maybe some instances, where that is what you want.) ...

    Any ways, you should be able to access it using the Controls collection and passing it the name of the control - not the name of the instance variable, but the actual .Name property value. Now, that will work if the control is directly on the form... if it's on a panel, or other container, then you'll need to use the .Controls collection of THAT container.... so make sure you know where your controls are.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Controls in a class

    It is (c# but the forum there is like a dead zone lol and I can convert vb to c# pretty easily). I create a textbox like this:

    Code:
    TextBox myText = new TextBox();
    form.Controls.Add(myText);
    The "form" you see there was passed in through a method in which I created the textbox like this:

    Code:
    public class Test
    {
    
    public static void createTextBox(Form form)
    {
    //textbox code here
    }
    
    }
    Now in the form class I want to change the textbox's text property to "hello". How would I do that? Am I doing something wrong?
    If I helped you please rate me.

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Controls in a class

    OK, in that case rather than having createTextBox as a method, I'd have it as a function that returns a reference to your new textbox.

    Alternatively as techgnome says you can query the form's controls collection (or whatever container "owns" the new textbox). If you don't give the new textbox an explicit name this could be tricky, although it is probably the case that the new textbox will be the last control in the collection - you'd need to experiment to check this i suspect.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Controls in a class

    Techgnome I think that is what I am looking for. How do you actually tell it what name to use? Which method is it (contains, fine, etc)?
    If I helped you please rate me.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Controls in a class

    You give it a name by setting the name property:
    Code:
    TextBox myText = new TextBox();
    myText.Name = 'MyNewTextBox';
    form.Controls.Add(myText);
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Controls in a class

    Quote Originally Posted by ngreenwood6 View Post
    Techgnome I think that is what I am looking for. How do you actually tell it what name to use? Which method is it (contains, fine, etc)?
    Its up to you to name the new textbox in your code that creates it - simple enough if you can only add one textbox, otherwise you'll need to manage some kind of mechanism for adding numbers just like VS does, ie textbox1 is followed by textbox2 etc.

    Then you can loop through the form's control's collection with a foreach loop, testing the name of each item until you find the match.

    I think changing your sub to a function and just returning a reference is the simplest approach.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Controls in a class

    I know how to assign the control a name. I was talking about how do I search for the name in the controls collection.
    If I helped you please rate me.

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Controls in a class

    No need to loop....

    this.Controls('MyNewTextBox')
    Will get you the control. But, granted, you need to know the name you assigned it when you created it.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Controls in a class

    Quote Originally Posted by techgnome View Post
    No need to loop....

    this.Controls('MyNewTextBox')
    Will get you the control. But, granted, you need to know the name you assigned it when you created it.


    -tg
    Sorry - quite right. Brain not working.

    But the need to know the name is the reason its better just to return a reference to the textbox in the first place!

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Controls in a class

    ok thanks I will try it and let you know if I have any problems.
    If I helped you please rate me.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Controls in a class

    Even with a reference, later referencing the control is going to be a pill w/o the name... If in func A, I create and add it, and later in func G, I need to reference it... you still need some way of getting to it... whether it's through a variable, the controls collection or some other developer collection (think control arrays).... but it depends on how it plans to be used...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Controls in a class

    Thread moved to C# forum

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