9 Replies - 13123 Views - Last Post: 03 December 2011 - 04:56 AM Rate Topic: -----

#1 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Access control from separate class

Posted 21 August 2008 - 08:53 AM

Allright. I have a form with a combobox, call it cmb1. I created a method in a separate classfile, Class1.cs, and now when I want to add items to cmb1, located in Form1.cs I can't reach it, get the following error:

Error 1 The name 'cmb1' does not exist in the current context

What do I have to do to access controlls in form1.cs from Class1.cs.

I'm very new to C#. :P

Is This A Good Question/Topic? 0
  • +

Replies To: Access control from separate class

#2 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: Access control from separate class

Posted 21 August 2008 - 09:01 AM

I think you have to create an instance of the form in the class. Something like tempForm = new Form1. And then do something like tempForm.cmb1 to access it. I'm not exactly a C# expert.
Was This Post Helpful? 0
  • +
  • -

#3 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Access control from separate class

Posted 21 August 2008 - 09:19 AM

Would you like to explain that more in detail?

Form1 TempForm = new Form1(); <--that would work if I could do TempForm.cmb1, but It won't work right now.
Was This Post Helpful? 0
  • +
  • -

#4 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Access control from separate class

Posted 21 August 2008 - 09:52 AM

I'm gonna explain a bit better:

I have following:

Form1.cs
Class1.cs

On Form1.cs I have a combobox called "cmb1". On startup, I want to fil it with some values. So in the Form load I have the following code:

private void Form1_Load(object sender, EventArgs e)
{

Loader.ReaderMess();

}

Now, Loader.ReaderMess(); exists in Class1.cs and looks like this:

public static void ReaderMess()
{
cmb1.items.add("Test1");
}

This won't work, I get the following error:

Error 1
The name 'cmb1' does not exist in the current context

In VB.NET if I'm not misstaken, you only need to do 'Form1.cmb1.additem "test1"', so I thought maybe I could do the same here, but I can't, that's the problem.

It's really annoying, can't seem to find a good way to solve it.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Access control from separate class

Posted 21 August 2008 - 09:56 AM

I would never reference a Form or a specific control in a class file, that defeats the purpose of OO programming. I would pass a reference to the ComboBox to the method, like this


public static void ReaderMess(ref ComboBox cmb)
{
cmb.items.add("Test1");
}





Then you can call it


Loader.ReaderMess(cmb1);


Was This Post Helpful? 0
  • +
  • -

#6 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Access control from separate class

Posted 21 August 2008 - 10:05 AM

That we pretty much what I was looking for.

BUT, it doesn't work all the way, I get an overload problem when I call it:

Loader.ReaderMess(cmb1);


Gives this error:

Error 1 The best overloaded method match for 'CascaLork.Loader.ReaderMess(ref System.Windows.Forms.ComboBox)' has some invalid arguments

Error 2 Argument '1' must be passed with the 'ref' keyword

Error 3 No overload for method 'ReaderMess' takes '0' arguments
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Access control from separate class

Posted 21 August 2008 - 10:19 AM

You're passing it by reference, so it needs to be

Loader.ReaderMess(ref cmb1);



My bad, forgot the ref in my original post
Was This Post Helpful? 1
  • +
  • -

#8 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Access control from separate class

Posted 21 August 2008 - 10:25 AM

Thank you PsychoCoder!!

You've saved my day!

I'm giving you huge kudos for this! I know it's basic stuff, but I'm quite new to this.
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Access control from separate class

Posted 21 August 2008 - 10:27 AM

Glad I could help. In the future (not saying this because I helped) you should use the "This Post Was Helpful" link to let the person know that you appreciated their help :)

Now, go forth and become a C# God :)

EDIT: Oops, spoke too soon lol :)
Was This Post Helpful? 1

#11 impatient  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 11

Re: Access control from separate class

Posted 03 December 2011 - 04:56 AM

I have exactly the same problem, i did what you advised but it is still now working.

I have 2 classes, one is MainForm, other is counter.

I am doing calculations in counter class,and i want to reach the control in MainForm which is picturebox.

My function in counter class :


public static void karsilastir(SavedLocation1 location1, Point currentyer, ref PictureBox pictureBox1, ref PictureBox pictureBox2)
        {
           
            if (location1.y- currentyer.Y < 0)
            {

                //MessageBox.Show("Dışarı šıktı.");
                pictureBox1.Visible = true;
                pictureBox2.Visible = false;
               
            }
            else
            {
                //MessageBox.Show("İšeri Girdi.");
                pictureBox2.Visible = true;
                pictureBox1.Visible = false;
            }
        }


When i do this :
 karsilastir(location1, currentyer, ref pictureBox1, ref pictureBox2);


Gives a lot of errors like :


The name 'pictureBox2' does not exist in the current context
The name 'pictureBox1' does not exist in the current context
cannot convert from 'ref pictureBox2' to 'ref System.Windows.Forms.PictureBox'
cannot convert from 'ref pictureBox1' to 'ref System.Windows.Forms.PictureBox'


How can i reach to pictureboxes in the MainForm, from my counter class?

Any ideas?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1