Results 1 to 12 of 12

Thread: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

  1. #1

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

    [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    From the main from i have

    C# Code:
    1. public bool Talk = false;
    2.  
    3.  
    4.   private void CB_TALK_CheckedChanged(object sender, EventArgs e)
    5.         {
    6.             Talk = CB_TALK.Checked;
    7.         }

    basicly to check the box switch the bool.


    from my user control

    c# Code:
    1. SpeechSynthesizer Voice = new SpeechSynthesizer();
    2.         private void TB_C_Enter(object sender, EventArgs e)
    3.         {
    4.             if (Form1.Talk)
    5.             {
    6.                 Voice.SpeakAsyncCancelAll();
    7.             Control C = new Control();
    8.             C = (Control)sender;
    9.             Voice.SpeakAsync(LBL_Name.Text + " " + C.Tag);
    10.             }
    11.            
    12.         }


    However I can't seem to get the Usercontrl to access the Talk variable.

    anyone got any idea what i'm forgetting?

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

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    What exactly does this mean?
    However I can't seem to get the Usercontrl to access the Talk variable.
    Are you saying that that code won't compile or that it doesn't pick up the value you expect at run time?
    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
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    I haven't compiled it because i can't get it to acknowledge that Talk is a public variable


    I also just tried public

    bool pubTalk
    {
    get{return Talk;}

    }

    and that didn't work either

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

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    In the code you posted, is Form1 actually a variable name or is it just the class name? I'm guessing it's the class name. In VB that would work because you can refer to the default instance via the class name but C# has no default instances. If you want to refer to an instance member then you have to do it via a variable that you've assigned an instance to.
    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
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    I'm not sure i follow

    If a variable is public on the user control i can refer to it on the form. Why can't i do it the other way around

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

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    Does your UserControl contain a variable named Form1?
    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
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    no

    but im trying to go access a variable in from1 from the usercontrol.

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

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    If your UserControl doesn't contain a variable named Form1 then how can you access a member of a Form1 object? The way you're doing it, Talk would have to be a static member of the Form1 class because you're trying to access it via the class itself, not a reference to an instance of the class.

    Think of it this way. What would you say if I asked you what the colour of House was? You couldn't tell me, could you, because House doesn't have a colour. Each individual house that gets built has a colour though, so you could tell me what the colour of a house is. This is the same thing. The Form1 class itself has no Talk field. Each individual Form1 object that you create does have a Talk field, so you need to to access the Talk field of an instance of the Form1 class via a reference to that instance.

    Now, having said all that, it's really bad design to have a UserControl trying to access a member of a specific form unless the UserControl itself has created that form. Your UserControl shouldn't really have to know that it's on a Form1. What if you decide later that you want to use the same UserControl on a Form2? What you should be doing is adding a Talk property to your UserControl. That way any form that it happens to reside on can set that property. Your code would then test its own Talk property without having to know that the Form1 class even exists, never mind having to access a specific instance.
    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
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    I guess your right about adding that variable to the user control. I was shooting for a centralized place to have one variable rather than having to loop through all the user contrls and set a public var to true or false.

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    There could be a workaround using this.Parent in your usercontrol where the parent would be the form where it is located.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    Quote Originally Posted by dee-u
    There could be a workaround using this.Parent in your usercontrol where the parent would be the form where it is located.
    That's not necessarily the case. this.Parent is the immediate parent, which might be a form but also might be a GroupBox, Panel, TabPage or something else. If you specifically want the containing form regardless of nesting then you should call a control's FindForm method.
    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

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [3.0/LINQ] what am i doing wrong ( accessing public vars from user control)

    Quote Originally Posted by jmcilhinney
    That's not necessarily the case. this.Parent is the immediate parent, which might be a form but also might be a GroupBox, Panel, TabPage or something else. If you specifically want the containing form regardless of nesting then you should call a control's FindForm method.
    Oooppsss... Yes, totally forgot about that one, I am sure there could be a way to determine the form where the usercontrol is located and the Parent property was the first to come to mind but you are correct that it could be another control also. And that there is the more suitable FindForm method.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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