Help... access tabpage controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shinobi
    New Member
    • Nov 2008
    • 7

    Help... access tabpage controls

    i have a tabcotrol(dotne tmagic) in c#.net and i am trying to create a tabpage containing a

    richtextbox which is created at runtime.

    richtextbox rch=new richtextbox();
    Crownwood.Magic .Controls.TabPa ge tb1 = new Crownwood.Magic .Controls.TabPa ge("Document1" , rch);

    but i can't access the richtextbox control property like

    rch.load();
    rch.rtf etc...

    can i set the properties of dynamically created controls in other events.?

    Help me...
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    From the small code sample you provided it looks like you are declaring the RichTextBox within the constructor or another method. This means that the RichTextBox is privately scoped to that constructor or method. Even though the control is then added to the TabPages controls collection, you won't be able to access it's properties or methods outside of the constructor or method within which it was created, as in this sample:

    Code:
            /// <summary>
            /// Creates an instance of my form
            /// </summary>
            private myForm()
            {
                RichTextBox rch = new RichTextBox();    // Variable is privately scoped
                Crownwood.Magic.Controls.TabPage tb1 = new Crownwood.Magic.Controls.TabPage("Document1", rch);
            }
    
            /// <summary>
            /// Does some stuff
            /// </summary>
            private void myMethod()
            {
                rch.Text = "Hello";     // <-- Won't compile. Can't see RichTextBox rch as it's declared within constructor.
            }
    To fix it, move the declaration of the RichTextBox control outside of the constructor and give it the approriate scope, depending on where you want to reference it from. If you only want to reference it from the code of the hosting Form class then private will do.

    Code:
            private RichTextBox rch; // Declare textbox
    
            /// <summary>
            /// Creates an instance of my form
            /// </summary>
            private myForm()
            {
                rch = new RichTextBox();   
                Crownwood.Magic.Controls.TabPage tb1 = new Crownwood.Magic.Controls.TabPage("Document1", rch);
            }
    
            /// <summary>
            /// Does some stuff
            /// </summary>
            private void myMethod()
            {
                rch.Text = "Hello";     // <-- Compiles OK. 
            }

    Comment

    • Shinobi
      New Member
      • Nov 2008
      • 7

      #3
      Thank you.
      I understand my mistake.

      Comment

      Working...