Results 1 to 4 of 4

Thread: Loading a new form + using objects on other forms

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    346

    Loading a new form + using objects on other forms

    Well like the title says I want to know how to interact with objects / controls that are on other forms.

    e.g. if i had a text box on one form, i want to be able to change its text from another form.

    I am using this code to load new forms :

    Code:
                frmMain main = new frmMain();
                main.Show();
    Is there a better way of going about doing this?

    Thanks!

    Microsoft gives you Windows, Linux gives you the whole house!


    I am who I am because of who we all are.


    We have just gotten a wake up call from the Nintendo generation.

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

    Re: Loading a new form + using objects on other forms

    You should read the first tutorial in my signature. It's written with VB.NET code examples but the principles are identical in C# and you can use one of the code converters in my signature to get the equivalent C# 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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    346

    Re: Loading a new form + using objects on other forms

    Thanks, that did help but I cant use controls on other forms without making a new instance of it all the time, is there no way to control them when they created in design view?

    Microsoft gives you Windows, Linux gives you the whole house!


    I am who I am because of who we all are.


    We have just gotten a wake up call from the Nintendo generation.

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

    Re: Loading a new form + using objects on other forms

    Did you read all three parts of that tutorial? I'm pretty sure it addresses exactly what you're asking, as that's the most commmon question when it comes to forms. The fact is that a form is an object just like any other. As such it is subject to the same rules as any other object. If you want to access a member of any object then you need a reference to that object. This means that if you want to access a control on Form1 from Form2 then Form2 needs a reference to the Form1, and that means the specific instance of Form1 that you want to affect, not a new instance. If you have a notebook that you want to write on do you go to the shop and buy a new notebook? Each instance of a form is an object and affecting any one of those objects has no effect on the others.
    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

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