Skip to main content

Microsoft Silverlight

Answered Question Access a control from another classRSS Feed

(0)

imukai
imukai

Member

Member

0 points

2 Posts

Access a control from another class

This might be a newbie question - apologize if it is.

I have a page (MainPage) that's inside my namespace (TestNS).

I have a control on that page - a textbox (TestText).

Within the MainPage.cs I can access TestText with no issues.

However, if I have another class within the same namespace, I cannot access TestText.  For that matter, I cannot access any public properties that I create in MainPage.  Intellisense ignores them.  I've tried MainPage.TestText, I've even tried TestNS.MainPage.TestText and it simply cannot find it.

What am I missing?

 

Helnix
Helnix

Member

Member

29 points

3 Posts

Re: Access a control from another class

for example the project will start at the main class and you do have a textbox on the main class

on class2 create a function accepting a textbox control

class2 myclass2 = new class2()

mainclasstextbox = myclass2.sendcontrol(mainclasstextbox)

you pass mainclass textbox control to other class

 

//Class2 function

public textbox sendcontrol(textbox mainclasstextbox)

{

//you do somthing with the mainclasstextbox

 return mainclasstextbox;


}

 

something like that.

Thanks 

 

 

 

 

 

imukai
imukai

Member

Member

0 points

2 Posts

Re: Access a control from another class

I really don't want to go this route, since it forces MainPage to be aware of Class2.  I want Class2 to be able to alter the public properties of MainPage without MainPage having to do anything special for Class2.

Is there something in Silverlight's UserControl that prevents this from working?  MainPage is, in fact, a UserControl.

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15884 points

1,541 Posts

Answered Question

Re: Access a control from another class

Hi,

TestText is not static field, you need access it from MainPage instance. for example:

MainPage myInstance = new MainPage();

myInstance.TestText.Text="Hello";

Thanks,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities