Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i do have 2 Forms. Form1 and Form2. Form2 is called by Form1 (button click).

On Form2 i have a WPF UserControl. This User control has a Textbox on it (Mytext).

Now i am creating 2 instances of Form2 from Form1 (by button click).

I am Naming both Form2 instances differently "FormNr2" and "FormNr3".

Now i want to call the TextChange() Method i have on the WPF UserControl that
is running on Form2 instance Called "Form3".

So first i will identify the Form3 by its Name and i will access a Method on this
Form3 that is called ChangeTextinUserControl();
---------------------------------------------------------------------------------

Form1 .....

C#
if (System.Windows.Forms.Application.OpenForms["FormNr3"] != null)
}
   (System.Windows.Forms.Application.OpenForms["FormNr3"] as
   ChangeTextinUserControl();
}


---------------------------------------------------------------------------------
Form2 ......

C#
public void ChangeTextinUserControl()
 {

    UserControl1 UC = new UserControl1();

    UC.TextChange();

 }


Here i do get the problem that i have to make an object because i cant access
the TextChange() method directly (non static error).

---------------------------------------------------------------------------------

UserControl1 .......

C#
public void TextChange()
{
   Mytext.Text = "This is an new Text";

}



Well when i run my code i will get no errors, but also my Textbox on the Usercontrol of Form3 is not changing. I guess because i made a new instance of the UserContorl and told this one to start the TextChange() Method and not the running
instance that is currently on Form3. Someone knows how to access this WPF UserContorl instance on Form3?

Thanks,

Hank
Posted
Comments
Chris Ross 2 29-Jan-16 8:24am    
I trust you've run your code in the debugger - and have confirmed that execution arrives at your TextChange() method. If it does, then the problem lies in the construction of UserControl1; if it doesn't, the problem lies in the code of Form1 or Form2.

To help you more, it would be helpful to see the XAML of UserControl1.
J. Calhoun 29-Jan-16 8:57am    
It seems to me that you are trying to catch a text changed "Event", and your problem is that you are trying to just call a public function to get that event. The problem here is that every time you rebuild the control you are using the "new" keyword. It would be my suggestion that you would set up a delegate/event that would fire when the user control uses its text changed function. This is known as the observer pattern. You could then register these events on the forms where you would want to listen for that text changed event.
Member 10398870 29-Jan-16 11:50am    
Ok , i found it. I have to use the Usercontrol object that was created when Form2 is called. With this instance of UserControl i can easily run my TextChange() method.
ryanba29 24-Feb-16 16:49pm    
Will you mark as answer found?

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