473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.Add(panel1);
//create a new Picturebox;
PictureBox pictureBox1 = new PictureBox();
tabPage1.Text = "Page1";
//add the tab to the tab control;
this.tabImageHolder.Controls.Add(tabPage1);
//add the panel to the tab
tabPage1.Controls.Add(panel1);
//add the picturebox
panel1.Controls.Add(pictureBox1);
panel1.AutoScroll = true;
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = new Bitmap(@"C:\ImageStreamer1.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.
Nov 15 '05 #1
1 6298
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.public.dotnet.languages.vb newsgroup for
VB.NET - related discussions.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Will" <gr****@willwyatt.com> wrote in message
news:28**************************@posting.google.c om...
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.Add(panel1);
//create a new Picturebox;
PictureBox pictureBox1 = new PictureBox();
tabPage1.Text = "Page1";
//add the tab to the tab control;
this.tabImageHolder.Controls.Add(tabPage1);
//add the panel to the tab
tabPage1.Controls.Add(panel1);
//add the picturebox
panel1.Controls.Add(pictureBox1);
panel1.AutoScroll = true;
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = new Bitmap(@"C:\ImageStreamer1.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.


Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." +...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
1
by: Felipe Garcia | last post by:
Does makes difference referencing controls inside EVERY page <%@ Register TagPrefix="PC" Namespace="MyControls" Assembly="Personal.MyControls" %> or referencing controls ONCE inside web.config,...
7
by: msdev | last post by:
Hello, I am creating my own webbrowser to learn VB .Net. I am stuck on an issue with regards to dynamically-created controls, in this case tabs on a tabcontrol and webbrowsers created within...
2
by: Alex Maghen | last post by:
I want to create a utility function that will seach the current page for one of my UserControls by it's type. So, let's say that I have a UserControl whose class I defined as follows: namespace...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.