Referencing dynamic controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will

    #1

    Referencing dynamic controls

    Hi all.
    I'm learning VB.Net and am developing a WinForms app.
    I'm trying to make an app that I will use to scan in one or more than
    on image. I want to use a tabbed interface to hold each image. Here's
    the code I'm using for testing purposes. I've got the code in the
    form's load event, but I think I'd have the same problems no matter
    where the code existed. Right now, the form has an empty tab control,
    everthing else is dynamic.

    <code>
    //Create a new tab
    TabPage tabPage1 = new TabPage();
    //Create a new panel
    Panel panel1 = new Panel();
    //add the panel to the form;
    this.Controls.A dd(panel1);
    //create a new Picturebox;
    PictureBox pictureBox1 = new PictureBox();
    tabPage1.Text = "Page1";
    //add the tab to the tab control;
    this.tabImageHo lder.Controls.A dd(tabPage1);
    //add the panel to the tab
    tabPage1.Contro ls.Add(panel1);
    //add the picturebox
    panel1.Controls .Add(pictureBox 1);
    panel1.AutoScro ll = true;
    panel1.Dock = System.Windows. Forms.DockStyle .Fill;
    pictureBox1.Siz eMode = PictureBoxSizeM ode.AutoSize;
    pictureBox1.Ima ge = new Bitmap(@"C:\Ima geStreamer1.gif ");
    </code>

    My problem, besides ignorance, is that I now can't reference any of
    the controls I've created anywhere else in the form. I think I
    understand why, because the controls have been created on the fly and
    are scoped to the Load event, right? If so, is there anyway I can make
    these dynamic controls be "global" to the rest of the methods in the
    form's class? Thanks.
  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: Referencing dynamic controls

    Hi Will,

    It's actually simple. Declare private variables in your Form-inherited class
    for each dynamic control you want to reference (or even an array or a
    hashtable if you expect a large number of controls). Then, in the Load event
    handler, instantiate the corresponding controls and store references to them
    to these class-scope variables. With this approach, you will be able to
    access any of the dynamic controls throughout the class.

    P.S. There is a dedicated microsoft.publi c.dotnet.langua ges.vb newsgroup for
    VB.NET - related discussions.

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Will" <groups@willwya tt.com> wrote in message
    news:28d5f694.0 309251914.672b3 cf6@posting.goo gle.com...[color=blue]
    > Hi all.
    > I'm learning VB.Net and am developing a WinForms app.
    > I'm trying to make an app that I will use to scan in one or more than
    > on image. I want to use a tabbed interface to hold each image. Here's
    > the code I'm using for testing purposes. I've got the code in the
    > form's load event, but I think I'd have the same problems no matter
    > where the code existed. Right now, the form has an empty tab control,
    > everthing else is dynamic.
    >
    > <code>
    > //Create a new tab
    > TabPage tabPage1 = new TabPage();
    > //Create a new panel
    > Panel panel1 = new Panel();
    > //add the panel to the form;
    > this.Controls.A dd(panel1);
    > //create a new Picturebox;
    > PictureBox pictureBox1 = new PictureBox();
    > tabPage1.Text = "Page1";
    > //add the tab to the tab control;
    > this.tabImageHo lder.Controls.A dd(tabPage1);
    > //add the panel to the tab
    > tabPage1.Contro ls.Add(panel1);
    > //add the picturebox
    > panel1.Controls .Add(pictureBox 1);
    > panel1.AutoScro ll = true;
    > panel1.Dock = System.Windows. Forms.DockStyle .Fill;
    > pictureBox1.Siz eMode = PictureBoxSizeM ode.AutoSize;
    > pictureBox1.Ima ge = new Bitmap(@"C:\Ima geStreamer1.gif ");
    > </code>
    >
    > My problem, besides ignorance, is that I now can't reference any of
    > the controls I've created anywhere else in the form. I think I
    > understand why, because the controls have been created on the fly and
    > are scoped to the Load event, right? If so, is there anyway I can make
    > these dynamic controls be "global" to the rest of the methods in the
    > form's class? Thanks.[/color]

    Comment

    Working...