Reputation: 123
Like in the title, how to change content in button from other window. I have main window i want to click on the button, this button open new window, and in this window i want to click on next button which one change content in the first button.
Upvotes: 1
Views: 91
Reputation: 239
Set button modifier property to Public in parent form,In the child form try something like this:
(this.Parent as ParentFormName).ButtonName.Text = "Your text";
Upvotes: 1
Reputation: 1675
You can use events if you don't want to use binding (which is event driven as well). In the second window add an event to the secondary window and assign a handler in the main window to perform the change. This is a pretty good tutorial on events.
Make a public class so that everything can access the arguments:
public class ChangeButtonEventArgs : EventArgs
{
//... arguments. For instance text;
public string NewContent;
}
Then to your secondary window add within the window class:
// this is essentially a delegate to give the form of the method
// EventHandler<SomeClass> says the method will look like Method(object,SomeClass)
public event EventHandler<ChangeButtonEventArgs > ChangeButton;
//This is what you will call in the secondary window to fire the event.
//The main window will not see this code.
protected virtual void OnChangeButton(object sender, ChangeButtonEventArgs e)
{
EventHandler<ChangeButtonEventArgs > handle = ChangeButton;
if (handle != null)
{
handle(this, e);
}
}
Now you have an event that can fire when you want. In the main window create a handler.
//this should happen when you create the secondary window object.
WindowObject.ChangeButton += new EventHandler<ChangeButtonEventArgs>(ChangeButtonMethodMainWindow);
private void ChangeButtonMethodMainWindow(object sender, ChangeButtonEventArgs e)
{
Button1.Content = e.NewContent;
}
Now you need to fire the event in the secondary window when something happens and you want it to perform something in the main window, all you have to do is call the method OnChangeButton
with the arguments your main window needs stored in an object of type ChangeButtonEventArgs
.
ChangeButtonEventArgs args = new ChangeButtonEventArgs();
args.NewContent = "SomeString";
OnChangeButton(this,args);
Upvotes: 0