Home > Back-end >  Reference object in main window from another script? (VS, C# and WPF)
Reference object in main window from another script? (VS, C# and WPF)

Time:10-26

Firstly my apologies for my stupid question. I am very new to this.

I have created my first ever windows application, yay, but now I am stuck again and google doesn't seem to be any help and smothering me with Unity based forums, which I followed the solutions to but doesn't seem to be helping me.

Ok so my issue is that I need to reference objects that are in the main window of Visual Studio from another script. Basically, as a start, I want a selection in the task bar menu to know if a tickbox (WPF: CheckBoxName.IsChecked) is true from my checkbox script.

Clearly I have to reference the script in my taskbar script, but how? My main window objects were dragged in from the asset selection and stored in MainWindow.XAML. (I'm using VS22, or Blend or whatever you crazy kids are calling it now days)

The answer shouldn't be too complicated, I'm just new and dumb. Any help will be gladly appreciated :)

CodePudding user response:

if i get this correctly: You want to access a variable / an object that is stored in another form/class?

  1. for the variables make them public:

string myName = "Faylasouf";
public string myName = "Faylasouf"

  1. for an object:

a. click the object, then click properties, change the modifiers property to "Public".

  1. Now you can go to the other form/class (the one will access the variable/object):

Class/Form_Name myClass/myForm = new myClass/myForm();

ex:

MainForm mainForm = new MainForm();

then you can use:

mainForm.ObjectName; or mainForm.VariableName;

  • Related