Opening a view via another event - c#

I have several views that need to be opened, but I don't want to repeat the code. Currently each button triggers something like this:
private void icon_Add(object sender, RoutedEventArgs e)
{
viewName var = new viewName();
var.Show();
Close();
}
Ideally, I'd like something like this (but don't know what type of variable/object I should use):
private void icon_Add(object sender, RoutedEventArgs e)
{
OpenView(viewName)
}
private voice OpenView(?????? newView)
{
newView var = new newView();
var.Show();
Close();
}
Any help would be appreciated (yet again).

You could solve it with Reflection by using Activator.CreateInstance.
private void icon_Add(object sender, RoutedEventArgs e)
{
OpenView(typeof(viewName));
}
private void OpenView(Type newView)
{
if(typeof(Window).IsAssignableFrom(newView)) {
Window window = (Window)Activator.CreateInstance(newView);
window.Show();
window.Close();
}
}
This means you'll pass the Type of Window you'd like to open to the OpenView method. This method creates an instance by using the default constructor. It will then call Show and Close on the created Window. The IF before all that is meant to check whether the passed Type is a Window.
It is also possible to pass constructor arguments to CreateInstance, in case you don't have a default constructor on every Window.
I hope that makes some sense.

Related

How do I call the void on another page when I click on the button?

I have a Module.cs, I want to run void which is inside of the module when I click the button. How can I call the void?
Module.cs:
public void CreateInventorApplication()
{
// İnventorün Yüklenmesini Bekle
while (!InvApp.Ready)
{
System.Windows.Forms.Application.DoEvents();
}
DefaultCaption = InvApp.Caption;
InvApp.Visible = true;
WatchTimer.Enabled = true;
StatusType = StatusTypeEnum.Online;
StatusMessage = "InventorPreparing";
}
Form1.cs:
private void button1_Click(object sender, EventArgs e)
{
}
You have phrased this a little ambiguously, but I'll assume you want to run "CreateInventorApplication" when you click the button.
Your click event method needs a reference to an instance of your Module class. You will need to create an instance of Module, make that instance available to your button1_Click method, and then call it. You could do something like this.
private void button1_Click(object sender, EventArgs e)
{
// Create an instance of the Module class that the button click event can access.
Module myModule = new Module();
// Call the void function using the instance we just created.
myModule.CreateInventorApplication();
}
Your void needs to be public, otherwise it is inaccessible by other classes and files.
public void button1_Click(object sender, EventArgs e)
{
}

c# execute a method from Form in the MainForm [duplicate]

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();

Close page within Frame in a WPF

I have a window and a Frame within it. in that Frame, i open many pages which i would like to close by clicking on "Close" button ,
the problem is the page can't see my frame i tried to write in an event a close button in a page method
to execute another event in a main window because it's easy on a main window to see a frame , but it's not working. here is my code in a page
private void closebt_MouseDown(object sender, MouseButtonEventArgs e)
{
var main = new MainWindow();
main.Exitbt_PreviewKeyDown(main.Exitbt, e);
}
and here is a code in a main window
internal void Exitbt_PreviewKeyDown(object sender, MouseButtonEventArgs e)
{
ProjectorFrame.Content = "";
MessageBox.Show("done");
}
Although the message show but it's not close page
please help me.
I don't know why you create another MainWindow instance inside the closebt_MouseDown hander, but I hope the following code would helpful for you:
private void closebt_MouseDown(object sender, MouseButtonEventArgs e)
{
MainWindow main = Application.Current.MainWindow as MainWindow;
if (main != null)
{
main.Exitbt_PreviewKeyDown(main.Exitbt, e);
main.Close();
}
}
Edited:
I supposed that the main Window object of your application is MainWindow, so I thought that the previous code could get your application window close.
But as you commented, Application.Current.MainWindow is different from MainWindow, and the main became null.
Therefore, I think the simple way to get the main Window object is to create the following constructor in your page class to keep the reference:
class YourPageClass
{
public YourPageClass(MainWindow mainWindow)
{
main = mainWindow;
}
private MainWindow main;
(snip)
}
Then, create this instance with passing main Window object:
// somewhere in MainWindow code where instantiate your page object
var page = new YourPageClass(this);
By doing that, you can get the main Window object.
So now, you can close your Window object as follows:
// in YourPageClass code
private void closebt_MouseDown(object sender, MouseButtonEventArgs e)
{
if (main != null)
{
main.Exitbt_PreviewKeyDown(main.Exitbt, e);
main.Close();
}
}

WPF - C# - open parent window only once

I´m programming a simple thing in C# and WPF. I have a MainWindow with a button. If I trigger the button it opens a secon window:
private void btnF4_Click(object sender, RoutedEventArgs e)
{
SecondWindow second = new SecondWindow();
second.Show();
}
Naturally if I trigger the button three or four times, I have three or four windows open. I don´t want to use ShowDialog(), but I want to open my second window only once. I mean if I trigger the button and the window is already open, should nothing happen.
Thank you!
Make second an instance variable to the parent window class and only create a new window if it hasn't been created.
Of course you need to make sure to null the instance variable when the second window is closed.
public class ParentWindow ...
{
private SecondWindow m_secondWindow = null;
....
private void btnF4_Click(object sender, RoutedEventArgs e)
{
if (m_secondWindow == null)
{
m_secondWindow = new SecondWindow();
m_secondWindow.Closed += SecondWindowClosed;
m_secondWindow.Show();
}
}
public void SecondWindowClosed(object sender, System.EventArgs e)
{
m_secondWindow = null;
}
}
This might be shortened to the following:
public class ParentWindow ...
{
private SecondWindow m_secondWindow = null;
....
private void btnF4_Click(object sender, RoutedEventArgs e)
{
if (m_secondWindow == null)
{
m_secondWindow = new SecondWindow();
}
m_secondWindow.Show();
}
}
However, I'm never sure whether you can actually "reopen" a window that was closed before. If you need to initialize the window all over upon reopening, use the first code. If you can live with the window starting up showing the previous content, use the second.
Declare SecondWindow in Parent Window class instead of a method.
public class MainWindow : Window {
SecondWindow second = new SecondWindow();
private void btnF4_Click(object sender, RoutedEventArgs e) {
if (!second.IsActive) {
second.Show();
}
}
}
Declaring second in the method makes the second window local to the method, which means every time you click the button it will create a new instance of that class (window)

getting System.ObjectDisposedException: Cannot access a disposed object.Modeless Dialogs in C#

I'd like to generate a modeless dialog box, whenever I close the box and want to open it again I am getting an error saying
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'TransactionHistoryDialog'.
at System.Windows.Forms.Control.CreateHandle()
here is my code for creating the modeless dialogbox
public partial class TransactionHistoryDialog : Form
{
private static TransactionHistoryDialog instance;
private TransactionHistoryDialog()
{
InitializeComponent();
}
public static TransactionHistoryDialog CreateForm()
{
if (instance == null)
{
instance = new TransactionHistoryDialog();
}
return instance;
}
private void TransactionHistoryDialog_FormClosing(object sender, FormClosingEventArgs e)
{
instance = null;
}
private void buttonClose_Click(object sender, EventArgs e)
{
instance = null;
}
private void buttonTransactionHistoryClose_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
then in my main form whenever the transactionHistory button is clicked transaction dialog shows up : here is my code for event of clicking transaction button
private void buttonTransferHistory_Click(object sender, EventArgs e)
{
TransactionHistoryDialog transactionHistory = TransactionHistoryDialog.CreateForm();
transactionHistory.updateTextBox();
transactionHistory.Show();
}
I have search a lot, but could not find where the problem is. can any one please give me some hints ?
Because closing the Window disposes it. You need to create a new one after it has closed, you cannot show the same Window after it has closed. If you want to show the same one don't close it and in a handler set Visibility to Hidden instead then add another method, e.g. UnHide(), set it back to Visible when wanting to show the same instance again.
I prefer just creating a new one:
TransactionHistoryDialog openTransactionHistoryDialog;
private void buttonTransferHistory_Click(object sender, EventArgs e)
{
if(openTransactionHistoryDialog == null)
{
openTransactionHistoryDialog = new TransactionHistoryDialog();
openTransactionHistoryDialog.updateTextBox();
openTransactionHistoryDialog.Closed += OnTransactionHistoryDialogClosed;
}
openTransactionHistoryDialog.Show();
}
private void OnTransactionHistoryDialogClosed(object sender, EventArgs e)
{
openTransactionHistoryDialog = null;
}
UPDATE: There is an "official" example of a modeless dialog box at the bottom of this page: http://msdn.microsoft.com/en-us/library/aa969773(v=vs.110).aspx.

Categories