Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

So basically I've made a windows form I've put a text box on it and called it txtBox. I then have two classes within the namespace the form and the class I want to work on.

C#
namespace WCFWindowsF
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ClientPart wcfCall = new ClientPart();
        }
    }

    class ClientPart
    {
        static void Zain(string[] args)        
        {
            txtBox.Text = "WCF Simple Demo";
        }
    }
}


The error that occurs is 'The name 'txtBox' does not exist in the current context.' I just can't understand why since both classes are within the same namespace. Any help would be most appreciated. Thanks.
Posted
Updated 7-Dec-11 23:32pm
v2

You need to inherit the source Class to get the textbox as shown below.

C#
namespace WCFWindowsF
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            ClientPart wcfCall = new ClientPart();
        }
    }
 
    class ClientPart : Form1
    {
        static void Zain(string[] args)        
        {
            txtBox.Text = "WCF Simple Demo";
        }
    }
}


Regards,
Eduard
 
Share this answer
 
Comments
WurmInfinity 8-Dec-11 5:49am    
I see, thanks for the help but the problem is still persisting "Missing partial modifier on declaration of type 'WCFWindowsF.Form1'; another partial deceleration of this type exists.". I've read some stuff off google someone said make it 'public partial class Form1' but that didn't make a difference :(
Make the textBox public you can access it anywhere. OR inherit Form class in ClientPart class
 
Share this answer
 
Comments
WurmInfinity 8-Dec-11 5:53am    
So I've added the inheritance but now I get the problem "Missing partial modifier on declaration of type 'WCFWindowsF.Form1'; another partial deceleration of this type exists."
As control of the form are by default private you cannot access it directly in another class.
you can use bellow method to access textbox in another class.

C#
namespace Test
{
    public partial class Form1 : Form
    {
        public string strTextBox
        {
            get { return txtBox.Text; }
            set { txtBox.Text = value; }
        }
        public Form1()
        {
            InitializeComponent();
            
            
        }

        public class ClientPart : Form1
        {
            static void Zain(string[] args)
            {

                Form1 objform = new Form1();
                objform.strTextBox = "WCF Simple Demo";
                
            }
        }
        
    }
    
    
}


Please mark as solution if its solved your problem
 
Share this answer
 
Comments
WurmInfinity 8-Dec-11 5:57am    
The partial component makes it inaccessible. When I remove the partial I get the error as mentioned above "Missing partial modifier on declaration of type 'WCFWindowsF.Form1'; another partial deceleration of this type exists.". :/
sagar15modi 8-Dec-11 7:13am    
Partial means that your class was divided, so your Form class has more components than the one in your Form.cs file. VS just put the automatic generated code in one file and your code in another. So you can not eliminate the partial unless you move all the generated code into a single file, but if you do that and use the Form Designer to change a label font, then you are doom!
Since that you are using the same Namespace, make sure you are using an Access modifier Internal or Public in your Class

Take a look at this:
http://msdn.microsoft.com/en-us/library/ms173121.aspx[^]

Regards,
Eduard
 
Share this answer
 
Comments
WurmInfinity 8-Dec-11 5:36am    
So I changed the 'ClientPart' class to public. It gave me access to the txtBox however now it says 'Missing partial modifier on declaration of type 'WCFWindowsF.Form1'; another partial deceleration of this type exists.
[no name] 8-Dec-11 5:46am    
oh i see. will be posting a new solution for this :)
I took a step back and realised what I was trying to do was basically like opening a door using a lemon. All I had to do was put my code in a button or another interactive object. My bad thanks for all the help guys :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900