Join 136,547 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,847 people online right now. Registration is fast and FREE... Join Now!
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 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.
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.
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
csharp
public static void ReaderMess(ref ComboBox cmb) { cmb.items.add("Test1"); }
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