I have a button in a window that basically does a rerun through some code (retry button)
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnRetry_Click(object sender, RoutedEventArgs e)
{
//TODO retry function
//MainWindow.Connect();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
I basically want to use the connect method in my main window class. However I can't do that unless it's a public static. However, if I do change Connect() to a public static, all the controls in it require to be static. I'm trying to minimize the amount of static controls I have; can anybody please help with a retry method?
Another solution is to make Window1.btnRetry visible to the parent form
public MainWindow : Window
{
private void ShowWindow1()
{
var window1 = new Window1();
window1.btnRetry.Click += OnRetryClicked;
window1.ShowDialog();
}
private void OnRetryClicked(object sender, EventArgs e)
{
// will be called when window1.btnRetry is clicked.
// retry the connection.
Connect();
}
}
First, you do need to make the method public (or internal) so that Window1 can use the method. However, do not make it static.
Instead, accept an instance of MainWindow in the Window1 constructor, and store it so that you can invoke the Connect() method on it later.
For example:
public partial class Window1 : Window
{
private MainWindow mainWindow;
public Window1(MainWindow mainWindow)
{
if (mainWindow == null) {
throw new ArgumentNullException("mainWindow");
}
this.mainWindow = mainWindow;
InitializeComponent();
}
private void btnRetry_Click(object sender, RoutedEventArgs e)
{
mainWindow.Connect();
}
// ...
}
Then, if constructing the instance from inside of MainWindow change your constructor invocation from new Window1() to new Window1(this).
Related
In this example, MainWindow has a button that opens Window2.
Window2 has a button that writes "Hello, World!" to MainWindow textblock.
Project source: https://www.dropbox.com/s/jegeguhycs1mewu/PassData.zip?dl=0
What is the proper way to pass data from Window2 to MainWindow?
private MainWindow mainwindow;
public MainWindow mainwindow { get; private set; }
public Window MainWindow { get; set; }
private object mainwindow { get; private set; };
private MainWindow mainwindow = ((MainWindow)System.Windows.Application.Current.MainWindow);
this.mainwindow = mainwindow;
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Open Window 2
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2(this);
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
}
Window 2
public partial class Window2 : Window
{
private MainWindow mainwindow;
public Window2(MainWindow mainwindow)
{
InitializeComponent();
this.mainwindow = mainwindow;
}
// Write Message to MainWindow
private void buttonMessage_Click(object sender, RoutedEventArgs e)
{
mainwindow.textBlockMessage.Text = "Hello, world!";
}
}
The 'proper' way usually depends on what your needs and circumstances are. But in general, using a delegate to pass data between windows is a common and standard practice.
Lets say the data you want to pass is a string. In your Main window, you want to create a delegate that lets you pass a string. Then you create an instance of that delegate type and subscribe a method that matches. Then when you open your secondary window, you pass that delegate to your secondary window.
public delegate void DataTransfer(string data);
public partial class MainWindow : Window
{
public DataTransfer transferDelegate;
public MainWindow()
{
InitializeComponent();
transferDelegate += new DataTransfer(DataMethod);
}
public void DataMethod(string data)
{
// Do what you want with your data.
}
private void button1_Click(object sender, EventArgs e)
{
Window2 win = new Window2(transferDelegate);
win.Show();
}
}
Now, when you invoke that delegate in your secondary window, the DataMethod() of your Form1 gets called, and so you can pass information between windows.
Your secondary window implementation should look like this:
public partial class Window2 : Window
{
DataTransfer transferDel;
public Window2(DataTransfer del)
{
InitializeComponent();
transferDel = del;
}
private void button1_Click(object sender, EventArgs e)
{
string data = "Hello, World!"; // Your string data to pass.
transferDel.Invoke(data);
}
}
As you can see, when you invoke the delegate that was passed, it calls the corresponding method in your main program.
One stand out advantage of this method is that you don't need to pass an instance of MainWindow to your Window2, you simply use delegates and subscribed methods to pass data between the two instances of windows.
The answer you're looking for is very implementation-based and depends heavily on what you want Window2 as a class to do.
private MainWindow mainwindow;
This is acceptable.
public MainWindow mainwindow { get; private set; }
This would work but doesn't respect naming conventions because it's a property. Usually you'd use this for encapsulation of a field or for easy access to a computed value.
public Window MainWindow { get; set; }
This is not acceptable in your context because Window does not contain a textBlockMessage.
private object mainwindow { get; private set; };
This also wouldn't work for the same reason as above.
private MainWindow mainwindow = ((MainWindow)System.Windows.Application.Current.MainWindow);
This would work and would even let you not keep a field for the reference to the MainWindow instance in your Window2 instances. Still needs to get that MainWindow everytime you click the button however.
Another interesting way to do what you're doing however is to simply pass the handler to the child windows at instanciation:
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Open Window 2
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2(); // No need to give a reference to the child window anymore
window2.setClickHandler((obj, ev) => {
textBlockMessage.Text = "Hello, world!"; // Direct access to the textblock.
});
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
}
Window2
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
public void setClickHandler(RoutedEventHandler handler)
{
// The handler is given to the click event.
buttonMessage.Click -= handler;
buttonMessage.Click += handler;
}
}
And with that your Window2 class has no need to know MainWindow.
You could declare a delegate in Window 2 and pass a function that can be executed when the button is pressed in window 2.
public delegate void SendMessage(string Message);
public SendMessage mainWindowDel = null;
Make your constructor that accepts the delegate
public Window2(SendMessage del)
{
mainWindowDel = del;
}
create window 2 by passing a function as the parameter
public void showMessage(string Message)
{
if(!string.IsNullOrWhiteSpace(Message))
textBlockMessage.Text = Message;
}
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
// Open Window 2
//Window2 window2 = new Window2(this);
Window2 window2 = new Window2(showMessage);
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
call the delegate when they press the button
// Write Message to MainWindow
private void buttonMessage_Click(object sender, RoutedEventArgs e)
{
mainWindowDel("Hello, world!");
}
You could probably set the datacontext of the MainWindow to an object that you could also pass to Window2 when it's created and set it's datacontext to the same object.
In that object could create a string property that could be used in both windows. And if you implement the INotifyPropertyChanged interface both windows would know when that string is updated.
Another Approach
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private bool DisplayText(string displayText)
{
txt_Main.Text = displayText;
return true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window2 win2 = new Window2(DisplayText);
win2.ShowDialog();
}
}
Window2
public partial class Window2 : Window
{
private Func<string, bool> mainWindowMethod;
public Window2(Func<string, bool> displayMethod)
{
InitializeComponent();
this.mainWindowMethod = displayMethod;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.mainWindowMethod("Hello World");
}
}
I have a main window and it has a button. When I press the main window button, it will open sub-window. In sub window, I have a text box and when I enter the text and click the add button in sub-window, the text should display on the main window. How do I achieve this? Thanks in advance.
You can create parameterized constructor of MainWindow class and pass value of Textbox from subwindow to main window and in MainWindow you can set content to that label.
Here is the implementation
MainWindow.xmal.cs
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
}
public MainWindow (string text) : this()
{
label.Content = text;
}
private void button_Click (object sender, RoutedEventArgs e)
{
Window1 win1 = new Window1();
win1.Show();
this.Close();
}
}
Here is the code for subWindow i.e. Window1.xaml.cs
public partial class Window1 : Window
{
private string text;
public Window1 ()
{
InitializeComponent();
}
private void button_Click (object sender, RoutedEventArgs e)
{
text = textBox.Text;
MainWindow mainWindow = new MainWindow(text);
mainWindow.Show();
this.Close();
}
}
You can simply modify the constructor of the second window :
public partial class Window1 : Window
{
string text;
public Window1 (string _text)
{
InitializeComponent();
this.text = _text;
}
}
I would add an event to your 'dialog' subwindow which the MainWindow can subscribe to as it instantiates it.
DialogWindow: (subwindow):
public class DialogInputEventArgs : EventArgs
{
public string Input { get; set; }
}
public partial class DialogWindow : Window
{
public event EventHandler<DialogInputEventArgs> InputChanged = delegate { };
private void SubmitInputButton_Click(object sender, RoutedEventArgs e)
{
InputChanged(this, new DialogInputEventArgs() { Input = this._inputTextBox.Text });
}
}
MainWindow:
private void ShowDialogButton_Click(object sender, RoutedEventArgs e)
{
DialogWindow dw = new DialogWindow();
dw.InputChanged += OnDialogInputChanged;
dw.Show();
}
private void OnDialogInputChanged(object sender, DialogInputEventArgs e)
{
// update the MainWindow somehow using e.Input (the text submitted in dialog)
}
If you need this mechanism for multiple windows I would go with something more generic, like a messagebus or observerpattern thing.
I'd suggest you to use CaliburnMicro framwework to achieve easier and better communication between your controls (assuming you are using MVVM pattern). You don't have to implement all features of CaliburnMicro, just EventAggregator to manage sending and handling messages between your controls.
In this example, MainWindow has a button that opens Window2.
Window2 has a button that writes "Hello, World!" to MainWindow textblock.
Project source: https://www.dropbox.com/s/jegeguhycs1mewu/PassData.zip?dl=0
What is the proper way to pass data from Window2 to MainWindow?
private MainWindow mainwindow;
public MainWindow mainwindow { get; private set; }
public Window MainWindow { get; set; }
private object mainwindow { get; private set; };
private MainWindow mainwindow = ((MainWindow)System.Windows.Application.Current.MainWindow);
this.mainwindow = mainwindow;
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Open Window 2
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2(this);
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
}
Window 2
public partial class Window2 : Window
{
private MainWindow mainwindow;
public Window2(MainWindow mainwindow)
{
InitializeComponent();
this.mainwindow = mainwindow;
}
// Write Message to MainWindow
private void buttonMessage_Click(object sender, RoutedEventArgs e)
{
mainwindow.textBlockMessage.Text = "Hello, world!";
}
}
The 'proper' way usually depends on what your needs and circumstances are. But in general, using a delegate to pass data between windows is a common and standard practice.
Lets say the data you want to pass is a string. In your Main window, you want to create a delegate that lets you pass a string. Then you create an instance of that delegate type and subscribe a method that matches. Then when you open your secondary window, you pass that delegate to your secondary window.
public delegate void DataTransfer(string data);
public partial class MainWindow : Window
{
public DataTransfer transferDelegate;
public MainWindow()
{
InitializeComponent();
transferDelegate += new DataTransfer(DataMethod);
}
public void DataMethod(string data)
{
// Do what you want with your data.
}
private void button1_Click(object sender, EventArgs e)
{
Window2 win = new Window2(transferDelegate);
win.Show();
}
}
Now, when you invoke that delegate in your secondary window, the DataMethod() of your Form1 gets called, and so you can pass information between windows.
Your secondary window implementation should look like this:
public partial class Window2 : Window
{
DataTransfer transferDel;
public Window2(DataTransfer del)
{
InitializeComponent();
transferDel = del;
}
private void button1_Click(object sender, EventArgs e)
{
string data = "Hello, World!"; // Your string data to pass.
transferDel.Invoke(data);
}
}
As you can see, when you invoke the delegate that was passed, it calls the corresponding method in your main program.
One stand out advantage of this method is that you don't need to pass an instance of MainWindow to your Window2, you simply use delegates and subscribed methods to pass data between the two instances of windows.
The answer you're looking for is very implementation-based and depends heavily on what you want Window2 as a class to do.
private MainWindow mainwindow;
This is acceptable.
public MainWindow mainwindow { get; private set; }
This would work but doesn't respect naming conventions because it's a property. Usually you'd use this for encapsulation of a field or for easy access to a computed value.
public Window MainWindow { get; set; }
This is not acceptable in your context because Window does not contain a textBlockMessage.
private object mainwindow { get; private set; };
This also wouldn't work for the same reason as above.
private MainWindow mainwindow = ((MainWindow)System.Windows.Application.Current.MainWindow);
This would work and would even let you not keep a field for the reference to the MainWindow instance in your Window2 instances. Still needs to get that MainWindow everytime you click the button however.
Another interesting way to do what you're doing however is to simply pass the handler to the child windows at instanciation:
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Open Window 2
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2(); // No need to give a reference to the child window anymore
window2.setClickHandler((obj, ev) => {
textBlockMessage.Text = "Hello, world!"; // Direct access to the textblock.
});
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
}
Window2
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
public void setClickHandler(RoutedEventHandler handler)
{
// The handler is given to the click event.
buttonMessage.Click -= handler;
buttonMessage.Click += handler;
}
}
And with that your Window2 class has no need to know MainWindow.
You could declare a delegate in Window 2 and pass a function that can be executed when the button is pressed in window 2.
public delegate void SendMessage(string Message);
public SendMessage mainWindowDel = null;
Make your constructor that accepts the delegate
public Window2(SendMessage del)
{
mainWindowDel = del;
}
create window 2 by passing a function as the parameter
public void showMessage(string Message)
{
if(!string.IsNullOrWhiteSpace(Message))
textBlockMessage.Text = Message;
}
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
// Open Window 2
//Window2 window2 = new Window2(this);
Window2 window2 = new Window2(showMessage);
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
call the delegate when they press the button
// Write Message to MainWindow
private void buttonMessage_Click(object sender, RoutedEventArgs e)
{
mainWindowDel("Hello, world!");
}
You could probably set the datacontext of the MainWindow to an object that you could also pass to Window2 when it's created and set it's datacontext to the same object.
In that object could create a string property that could be used in both windows. And if you implement the INotifyPropertyChanged interface both windows would know when that string is updated.
Another Approach
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private bool DisplayText(string displayText)
{
txt_Main.Text = displayText;
return true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window2 win2 = new Window2(DisplayText);
win2.ShowDialog();
}
}
Window2
public partial class Window2 : Window
{
private Func<string, bool> mainWindowMethod;
public Window2(Func<string, bool> displayMethod)
{
InitializeComponent();
this.mainWindowMethod = displayMethod;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.mainWindowMethod("Hello World");
}
}
here on Button click to close mordenwindow..thats doesn't work
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// this MainWindow is like this --> <mui:ModernWindow x:Class="FirstFloor.ModernUI.App.MainWindow1" ....>
MainWindow1 mw = new MainWindow1();
// this is my login Page..
Login lg = new Login();
lg.Show();
mw.Close(); //here code is not working
}
What you did in that Button_Click_1 event is you created a new ModernWindow1 then you closed that newly created ModernWindow1, . Now, you technically have two ModernWindow1 in the start of that event. What you need is to close the currently running ModernWindow1, and not the newly created ModernWindow1. to do that, you need to reference the old ModernWindow1 before going to another window.
This is the Second ModernWindow
public partial class ModernWindow2 : ModernWindow
{
public dynamic ReferencedWindow2; //you will put the original Window here
public ModernWindow2()
{
InitializeComponent();
}
public ModernWindow2(dynamic referencedWindow) // second constructor with a parameter
{
InitializeComponent();
ReferencedWindow2 = referencedWindow; // the original modernwindow being put in here
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
ReferencedWindow2.Close();
}
}
THIS IS THE ORIGINAL OR PRIMARY MODERNWINDOW
public partial class ModernWindow1 : ModernWindow
{
public ModernWindow1()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
/*
this will show the second modernwindow using the second constructor with parameter
*/
ModernWindow2 newWindow2 = new ModernWindow2(this);
newWindow2.Show();
}
}
I am working with windowsFrom in c#. I am trying to call mainfrom method in one of the from in user control.
I have mainfrom like this
namespace Project
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public void TempCommand()
{
StartTemp();
}
}
}
I have the button click in the user control. When i click that button then it will open another form. I have the code like this in the user control.
private TempCalib _tempCalib = new TempCalib();
private void calibBtn_Click(object sender, EventArgs e)
{
_tempCalib.Show();
}
it will open another from and i have one button in that from. I need to call mainfrom method when i click "Ok" button in this from.
namespace Project
{
public partial class TempCalib : Form
{
public TempCalib()
{
InitializeComponent();
}
private void OkButton_Click(object sender, EventArgs e)
{
// I need to call the mainfrom "TempCommand" method here.
this.Hide();
}
}
}
Can anyone help me how to do this.
Thanks.
Quick answer
Just add a reference to the primary form in your secondary form:
public partial class TempCalib : Form
{
private MainForm _main
public TempCalib(MainForm main) : this()
{
_main = main;
}
/// Other stuffs
}
Then assign value when you construct your secondary form:
private TempCalib _tempCalib;
private void calibBtn_Click(object sender, EventArgs e)
{
if (_tempCalib == null)
_tempCalib = new TempCalib(this);
_tempCalib.Show();
}
If calibBtn_Click isn't inside MainForm (but it's inside a UserControl on it) then you can replace _tempCalib initialization with:
_tempCalib = new TempCalib((MainWindow)FindForm());
You'll be then able to call the primary form:
private void OkButton_Click(object sender, EventArgs e)
{
_main.TempCommand();
this.Hide();
}
Notes: this is just one option, you may create a property to hold MainForm reference (so secondary form can be reused and it'll be more designer friendly) moreover TempCalib is not an UserControl but a Form (pretty raw but for an UserControl you may just check its parent Form and cast it to proper type).
Improvements
Such kind of references are often an alert. Usually UI components shouldn't not be so coupled and a public Form's method to perform something very often is the signal that you have too much logic in your Form. How to improve this?
1. DECOUPLE CONTROLS. Well a first step may be to decouple them a little bit, just add an event in TempCalib and make MainForm its receiver:
public partial class TempCalib : Form
{
public event EventHandler SomethingMustBeDone;
private void OkButton_Click(object sender, EventArgs e)
{
OnSomethingMustBeDone(EventArgs.Empty); / TO DO
this.Hide();
}
}
Then in MainForm:
private TempCalib _tempCalib;
private void calibBtn_Click(object sender, EventArgs e)
{
if (_tempCalib == null)
{
_tempCalib = new TempCalib();
_tempCalib.SomethingMustBeDone += _tempCalib_SomethingMustBeDone;
// In _tempCalib_SomethingMustBeDone you'll invoke proper member
// and possibly hide _tempCalib (remove it from OkButton_Click)
}
_tempCalib.Show();
}
2. DECOUPLE LOGIC FROM CONTROLS. UI changes pretty often, logic not (and when it changes probably isn't in parallel with UI). This is just the first step (now TempCalib isn't aware of who will use it). Next step (to be performed when too much things happen inside your form) is to remove this kind of logic from the form itself. Little example (very raw), keep TempCalib as before (with the event) and change MainForm to be passive:
public partial class MainForm : Form
{
public event EventHandler Calibrate;
protected virtual void OnCalibrate(EventArgs e)
{
// TODO
}
}
Now let's create a class to control the flow and logic:
public class MyTaskController
{
private MainForm _main;
private TempCalib _tempCalib;
public void Start()
{
_main = new MainForm();
_main.Calibrate += OnCalibrationRequested;
_main.Show(); // Or whatever else
}
private void OnCalibrationRequested(object sender, EventArgs e)
{
if (_tempCalib == null)
{
_tempCalib = new TempCalib();
_tempCalib.SomethingMustBeDone += OnSomethingMustBeDone();
}
_tempCalib.Show();
}
private OnSomethingMustBeDone(object sender, EventArgs e)
{
// Perform the task here then hide calibration window
_tempCalib.Hide();
}
}
Yes, you'll need to write much more code but this will decouple logic (what to do as response to an action, for example) from UI itself. When program grows up this will help you to change UI as needed keeping logic unaware of that (and in one well defined place). I don't even mention that this will allow you to use different resources (people) to write logic and UI (or to reuse logic for different UI, WinForms and WPF, for example). Anyway IMO the most obvious and well repaid benefit is...readability: you'll always know where logic is and where UI management is, no search, no confusion, no mistakes.
3. DECOUPLE LOGIC FROM IMPLEMENTATION. Again you have more steps to perform (when needed). Your controller is still aware of concrete types (MainForm and TempCalib). In case you need to select a different form at run-time (for example to have a complex interface and a simplified one or to use dependency injection) then you have to decouple controller using interfaces. Just an example:
public interface IUiWindow
{
void Show();
void Hide();
}
public interface IMainWindow : IUiWindow
{
event EventHandler Calibrate;
}
public interface ICalibrationWindow : IUiWindow
{
event EventHandler SomethingMustBeDone;
}
You could use a custom event that is declared in your UserControl. Then your form needs to handle this event and call the method you want to call. If you let the UserControl access your form, you are hard-linking both with each other which decreases reusability of your UserControl.
For example, in TempCalib:
public delegate void OkClickedHandler(object sender, EventArgs e);
public event OkClickedHandler OkClicked;
private void OkButton_Click(object sender, EventArgs e)
{
// Make sure someone is listening to event
if (OkClicked == null) return;
OkClicked(sender, e);
this.Hide();
}
in your mainform:
private void Mainform_Load(object sender, EventArgs e)
{
_tempCalib.OkClicked += CalibOkClicked;
}
private void CalibOkClicked(Object sender, EventArgs e)
{
StartTemp();
}
You create an event in your usercontrol and subscribe to this in the mainform.
That is the usual way.
Form1 Code:
UserControl1 myusercontrol = new UserControl1();
public void TabClose(Object sender,EventArgs e)
{
int i = 0;
i = tabControl1.SelectedIndex;
tabControl1.TabPages.RemoveAt(i);
}
private void Form1_Load(object sender, EventArgs e)
{
myusercontrol.Dock = DockStyle.Fill;
TabPage myTabPage = new TabPage();
myTabPage.Text = "Student";
myTabPage.Controls.Add(myusercontrol);
tabControl1.TabPages.Add(myTabPage);
myusercontrol.OkClick += TabClose;
}
UserControl1 Code:
public delegate void OkClickedHandler(Object sender, EventArgs e);
public partial class UserControl1 : UserControl
{
public event OkClickedHandler OkClick;
public UserControl1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
if (OkClick == null) return;
OkClick(sender, e);
}
}
Try this:
From user control try this:
MainForm form = this.TopLevelControl as MainForm;
form.TempCommand();