blah blah blah is here! blah blah » Close

up1down
link

Hi

I am toying with an APP and I open an XMLDocument in the main form.

I am trying to access the already opened XML document from a userControl... However I tried using a getter/setter (loadedXDoc) in Form1 then using Form1 f1 = new Form1(); in the userControl.

Then tried f1.loadedXDoc and when it enters the getter setter the values are null? Yet when i step into this get/set method during load of the document i see these values set correctly...

I believe this to be down to the new Form1 method but am quite stuck on this any assistance welcome.

Hope i explained well enough.

last answered 6 months ago

1 answers

link

The problem is that when you execute this line from the UserControl:

Form1 f1 = new Form1();

it creates a new instance of Form1 rather than obtaining a reference to the existing instance.

If the UserControl is on Form1, then this code should do what you want:
Form1 f1 = this.FindForm() as Form1;
XmlDocument doc = f1.loadedXDoc; // assuming its a public or internal property

However, if you want to use the UserControl on a different form you'll need to remember to remove or modify this code, otherwise it won't compile.

psymon25
152

Hi Vulpes have not tried this but managed to keep on trying and the following also seems to work... Which would you say was the better approach the above or this: [code] class xBaseLoaded { private static string baseFile; public string thisBase { get { return baseFile; } set { baseFile = value; } } } [/code]

vulpes
12813

That approach might be better as it removes the dependency between the main form and the XmlDocument. However, you'll still need access to the xBaseLoaded class if the UserControl is deployed elsewhere.

Feedback