473,396 Members | 1,996 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,396 software developers and data experts.

C#-APP: Accessing controls on form from an outside Class

Hello Experts! ;-)

I am fairly new to C#, and have this problem....

on my form, i have some controls (imagelist, etc..)

from another class, i need to access imageList1

Here is a snippet of the class

Expand|Select|Wrap|Line Numbers
  1.         public System.Windows.Forms.Button AddNewButton(int Top, int Left, int Width, int Height)
  2.         {
  3.             // Create a new instance of the Button class.
  4.             System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
  5.             // Add the button to the collection's internal list.
  6.             this.List.Add(aButton);
  7.             // Add the button to the controls collection of the form 
  8.             // referenced by the HostForm field.
  9.             HostForm.Controls.Add(aButton);
  10.             // Set intial properties for the button object.
  11.             aButton.Top = Top;// Count * 25;
  12.             aButton.Left = Left;// 100;
  13.             aButton.Height = Height;
  14.             aButton.Width = Width;
  15.             aButton.Tag = this.Count;
  16.             aButton.Text = ButtonProperties[this.Count].Text ;
  17.             if (ButtonProperties[this.Count].Lock == true) aButton.BackColor = System.Drawing.Color.Red;
  18.             if (ButtonProperties[this.Count].Lock == true) aButton.BackgroundImage  = imageList1.Images[0];
  19.             aButton.Click += new System.EventHandler(ClickHandler);
  20.  
  21.  
  22.             return aButton;
  23.         }
  24.  
The problem is in line 18

Any help would be appreciated.
Jul 17 '07 #1
8 1460
shweta123
692 Expert 512MB
Hi,

You should create the instance of the form and then call its controls using it.
e.g. objform.ImageList1
Jul 17 '07 #2
Hi,
and thanks for thew quick reply..

Isn't it what i'm already doing in line 11.

If it's not, could you be so kind to be a little more specific.

Expand|Select|Wrap|Line Numbers
  1.         static void Main(string[] args) 
  2.         {
  3.             if (args.Length == 0)
  4.             {
  5.                 Application.Exit();
  6.  
  7.             }
  8.             else
  9.             {
  10.                 //szMachineName = args[0];
  11.                 Application.Run(new Form1(args[0]));
  12.             }
  13.  
  14.         }
  15.  
  16.         private void Form1_Load(object sender, System.EventArgs e)
  17.         {
  18.             Init_APP();
  19.             bDone = true;
  20.         }
  21.  
Jul 17 '07 #3
shweta123
692 Expert 512MB
Hi,

Please see here, this code had you posted in your last thread

public System.Windows.Forms.Button AddNewButton(int Top, int Left, int Width, int Height)
{
// Create a new instance of the Button class.
System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
// Add the button to the collection's internal list.
this.List.Add(aButton);
// Add the button to the controls collection of the form
// referenced by the HostForm field.
HostForm.Controls.Add(aButton);
// Set intial properties for the button object.
aButton.Top = Top;// Count * 25;
aButton.Left = Left;// 100;
aButton.Height = Height;
aButton.Width = Width;
aButton.Tag = this.Count;
aButton.Text = ButtonProperties[this.Count].Text ;
if (ButtonProperties[this.Count].Lock == true) aButton.BackColor = System.Drawing.Color.Red;
if (ButtonProperties[this.Count].Lock == true) aButton.BackgroundImage = imageList1.Images[0];
aButton.Click += new System.EventHandler(ClickHandler);


return aButton;
}



I could not see here anything like Application.Run(new Form1(args[0]));
Where are you stuck exactly?



Hi,
and thanks for thew quick reply..

Isn't it what i'm already doing in line 11.

If it's not, could you be so kind to be a little more specific.

Expand|Select|Wrap|Line Numbers
  1.         static void Main(string[] args) 
  2.         {
  3.             if (args.Length == 0)
  4.             {
  5.                 Application.Exit();
  6.  
  7.             }
  8.             else
  9.             {
  10.                 //szMachineName = args[0];
  11.                 Application.Run(new Form1(args[0]));
  12.             }
  13.  
  14.         }
  15.  
  16.         private void Form1_Load(object sender, System.EventArgs e)
  17.         {
  18.             Init_APP();
  19.             bDone = true;
  20.         }
  21.  
Jul 17 '07 #4
Hi,

Ok, sorry

My project consist of:

Form1.cs, where all is done, this is where the previous listed code is from, it is here e.g. imageList1 resides

Then i have 3 classes/files:
CIniFile.cs
CLogbook.cs
CStatButton.cs

The first listed code where the problem is, is a method in Class CStatButton
From this Class, i want to be able to access the control imageList1 from Form1.

So the problem is in line 18 from the first post.

Hopes this make more sense...
Jul 17 '07 #5
Anyone able to help??
Jul 19 '07 #6
Plater
7,872 Expert 4TB
you could pass the reference to your image list into the constructor of your other classes, and have it retain a reference to it:
Expand|Select|Wrap|Line Numbers
  1. public class myclass
  2. {
  3.    private imagelist savethis=null;
  4.    public myclass(Imagelist savethis)
  5.    {
  6.       _myimagelist=savethis;
  7.    }
  8. }
  9.  
But I would more recomend that if you must, pass the object into the function calls and have it used there.
Jul 19 '07 #7
RoninZA
78
Hi Masterfrier...

I would change your addNewButton method to return an ImageList index, for example:
Expand|Select|Wrap|Line Numbers
  1. public System.Windows.Forms.Button AddNewButton(int Top, int Left, int Width, int Height, out int imageListIndex)
  2. {
  3. .
  4. .
  5. //set imageListIndex to the index of the image in the ImageList you 
  6. //want to assign to the button
  7. imageListIndex = 0;
  8. .
  9. .
  10. }
  11.  
Which you will call as follows:
Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. int imageListIndex = -1;
  4. System.Windows.Forms.Button newButton = <className>.AddNewButton(0, 0, 36, 80, out imageListIndex);
  5. newButton.BackgroundImage = imageList1.Images[imageListIndex];
  6. this.Controls.Add(newButton);
  7. .
  8. .
  9.  
Basically moving the code to assign the image and add the button to the form out of your class and into the calling form.

Hope it helps! :)
Jul 19 '07 #8
Hi.

Thanks Plater & RoninZA, as you mentioned Plater, i implemented it as RoninZA suggested.

It now operates perfectly.
Jul 19 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sean Connery | last post by:
I have a Microsoft UI Process Application Block that is controlling child forms in an MDI parent container. The views node in the app.config file has been set to stayOpen=false. Because there...
6
by: arvee | last post by:
Is there a way to access controls (and their properties) in a user control? The Web Form Designer marks controls as 'Protected' which makes them inaccessable from the host form. If I mark them as...
1
by: Nathan | last post by:
Hi, I have created a class library creating a number of forms and a few public variables. I have a project that references the .dll for this class library, and in that project I need to access...
6
by: Jon Masterson | last post by:
Hi All I am trying to access a control on my main form from a class. The control is a Windows Media Player which I do not think I can instantiate in code. The class needs to pass the file name...
5
by: Ben | last post by:
Hello I have a protected variable in a class (Class A) that I need to call from another class (Class B) to modify. I thought what I had to do was create a public method in the class (Class A)...
10
by: joint52 | last post by:
Hello This seems to work in my system, until variable 'i' reaches 15985, then the program crashes. My question is, why won't it crash sooner? How does it work? And why am I able to set the...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
0
by: khalid sohail | last post by:
hi can any1 help me in accessing controls that exist in datagrid using the javascript.......ordinary that controls are access by the document.getelementById('controlid') mathod but that does not...
0
by: khalid sohail | last post by:
hi can any1 help me in accessing controls that exist in datagrid using the javascript.......ordinary that controls are access by the document.getelementById('controlid') mathod but that does not...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.