Reputation: 40381
I created a methods in a "Cammon" class. This method's functionality is to check a setting variable, from there it open the correct form as a MID child.
Here is my method
public static void OpenMyForm(string sectionName, string[] keys, Form myform) {
//make sure there are no other forms of the ame type open
foreach (Form form in Application.OpenForms) {
if (form.GetType() == myform.GetType()) {
form.Activate();
return;
}
}
if (Settings._AuthenticationMode == "Thumbprint") {
var newMDIChild = myform;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = Main.ActiveForm;
// Display the new form.
newMDIChild.Show();
}
if (Settings._AuthenticationMode == "Single" && UserInfo.Autherized == true) {
var role = new Roles();
if (role.hasAccess(sectionName, keys)) {
var newMDIChild = myform;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = Main.ActiveForm;
// Display the new form.
newMDIChild.Show();
}
else {
Common.Alert("You do not have a permissions to perform this action!");
}
}
}
The issue here is when I call this method I get the exception error bellow.
However, when I call this method the new forms actually open just before I get the error.
I believe the issue is in the line below
newMDIChild.MdiParent = Main.ActiveForm;
I believe the way I am setting the parent is incorrect. The parent form name in Main()
Here is an example of how I am calling this method
Common.OpenMyForm("Vendors", new string[] { "add" }, new DepartmentsAdd());
How can I correct this issue?
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Telerik.WinControls.UI
StackTrace:
at Telerik.WinControls.UI.RadListElement.HandleMouse(Object sender, RoutedEventArgs args)
at Telerik.WinControls.UI.RadListElement.OnBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.DoMouseUp(MouseEventArgs e)
at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at Telerik.WinControls.RadControl.WndProc(Message& m)
at Telerik.WinControls.UI.RadPopupControlBase.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at RM.Program.Main()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Upvotes: 0
Views: 3661
Reputation:
Implement you application context class that derives from ApplicationContext:
class MyApplicationContext : ApplicationContext
{
public static MyApplicationContext CurrentContext;
public MyApplicationContext(Form mainForm) : base(mainForm)
{
//...implement any hooks, additional context etc.
CurrentContext = this;
}
}
Implementation to use your application context:
[STAThread]
static void Main(string[] args)
{
var context = new MyApplicationContext(new MainForm());
// Run the application with the specific context.
Application.Run(context);
}
To access your application context data like the MainForm:
MyApplicationContext.CurrentContext.MainForm
Thus:
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = MyApplicationContext.CurrentContext.MainForm;
Upvotes: 1