Hi Guys -
This is my first C# app, but I have some knowledge of a bunch of languages like VB6 and PHP, so I'm picking it up rather quickly. I've been pulling my hair out on this one for about 4 hours now.
I have a DirectoryInfo on my parent form: (extra bits chopped out...) -
public partial class frmMain : Form
-
{
-
public DirectoryInfo diTemp;
-
-
private void frmMain_Load(object sender, EventArgs e)
-
{
-
diTemp = Directory.CreateDirectory("c:\\temp");
-
}
-
-
private void btn1B_ExportCSV_Click(object sender, EventArgs e)
-
{
-
frm1B_ExportCSV f = new frm1B_ExportCSV();
-
f.ShowDialog(this);
-
}
-
-
public string setWorkingDir(string strSubdir)
-
{
-
diTemp.CreateSubdirectory(strSubdir);
-
return diTemp.FullName + "\\" + strSubdir;
-
}
-
}
-
And when I try to access it on my child form: -
public partial class frm1B_ExportCSV : Form
-
{
-
private string strWorkingDir;
-
-
private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");
-
}
-
}
-
it throws a System.NullRefe renceException when opening the child form at run time.
I wish I would have kept better records of what all I've tried, but I got frustrated. I can tell you I've tried accessing the DirectoryInfo directly, and several variations of things like this -
//Cast MDIParent to your MDIParent Form
-
theMDIParentForm theParent = (theMDIParentForm) this.MDIParent;
-
-
//Now you can access objects on the MDI Parent form from MDI Children
-
theParent.myTreeViewControl.Nodes[0].Add(new TreeNode("New Child Node"));
-
I'm sure it's something incredibly easy that I'm missing, any help would be appreciated.
8 5260 Plater 7,872
Recognized Expert Expert
I think it is happening because you derived class does not call the contructor of your base class, which is where the DirectoryInfo instance is created.
Call the base class's constructor first, then try to use it?
I think it is happening because you derived class does not call the contructor of your base class, which is where the DirectoryInfo instance is created.
Call the base class's constructor first, then try to use it?
Thanks for your reply... Sorry but that's a little above my head with C#. Also, would your method have the effect of creating a new DirectoryInfo object or referencing the existing one on the parent form (which is what I'm trying to do)?
Is any of these what you mean, or close at least?
Change - public partial class frm1B_ExportCSV : Form
To - public partial class frm1B_ExportCSV : frmMain
(didn't work)
I found this also and tried it: -
public partial class frm1B_ExportCSV : Form
-
{
-
private frmMain frmParent;
-
-
public frm1B_ExportCSV(frmMain fFrm)
-
{
-
InitializeComponent();
-
frmParent = fFrm;
-
}
-
-
private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
strWorkingDir = frmParent.setWorkingDir("B2");
-
}
-
}
-
And got this error: "No overload for method 'frm1B_ExportCS V' takes '0' arguments"
so I tried: -
public frm1B_ExportCSV()
-
{
-
InitializeComponent();
-
frmParent = (frmMain)this.MdiParent;
-
}
-
but frmParent.setWo rkingDir("B2"); still throws NullReferenceEx ception
I feel like I'm closer but I know I'm missing an important concept or something. What else should I be trying? Am I setting the parent correctly when I call the form like this? -
frm1B_ExportCSV f = new frm1B_ExportCSV();
-
f.ShowDialog(this);
-
Plater 7,872
Recognized Expert Expert
Going back to your original code (I had mis-read it before sorry):
Have you tried something like this: -
private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
if(this.ParentForm!=null)
-
{
-
MessageBox.Show(this.ParentForm.GetType().Name.ToString());
-
}
-
//strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");
-
}
-
This should popup a messagebox that shows the type of the parentform if it is not null. (It should say "frmMain" in the box if you setup your child windows correctly)
Going back to your original code (I had mis-read it before sorry):
Have you tried something like this: -
private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
if(this.ParentForm!=null)
-
{
-
MessageBox.Show(this.ParentForm.GetType().Name.ToString());
-
}
-
//strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");
-
}
-
This should popup a messagebox that shows the type of the parentform if it is not null. (It should say "frmMain" in the box if you setup your child windows correctly)
Thanks for the help Plater. I tried this: -
if(this.ParentForm!=null)
-
{
-
MessageBox.Show(this.ParentForm.GetType().Name.ToString());
-
}
-
else
-
{
-
MessageBox.Show("Null parent form");
-
}
-
-
(tried same thing with this.MdiParent by the way, and i tried it in OnShown and frm1B_ExportCSV )
And I get the message box, so the parent form definitely isn't getting set. But when I create the form like -
frm1B_ExportCSV f = new frm1B_ExportCSV();
-
f.ShowDialog(this);
-
when I pass "this" to ShowDialog, it's supposed to create the parent/child relationship, right? Unless I'm mis-understanding the VS2005 documentation?
I can access variables inside the child form before it's disposed like this -
string strReturnValue = f.strReturnValue;
-
string strReturnError = f.strReturnError;
-
And it works just fine.
I tried setting the parent before showing the form like this -
frm1B_ExportCSV f = new frm1B_ExportCSV();
-
f.Parent = this;
-
f.ShowDialog(this);
-
Which throws this error: System.Argument Exception - Top-level control cannot be added to a control.
I can't figure out how to set the parent of the child form on the child form (this.Parent = ???) at run-time, but I'll keep trying.
Any idea if f.ShowDialog(th is) is the proper way to set parent of the child? Or maybe I'm missing something somewhere that would set it?
From your original post - public partial class frmMain : Form
-
{
-
public DirectoryInfo diTemp;
-
-
private void frmMain_Load(object sender, EventArgs e)
-
{
-
diTemp = Directory.CreateDirectory("c:\\temp");
-
}
-
-
private void btn1B_ExportCSV_Click(object sender, EventArgs e)
-
{
-
frm1B_ExportCSV f = new frm1B_ExportCSV();
-
f.ShowDialog(this);
-
}
-
-
public string setWorkingDir(string strSubdir)
-
{
-
diTemp.CreateSubdirectory(strSubdir);
-
return diTemp.FullName + "\\" + strSubdir;
-
}
-
}
And when I try to access it on my child form: - public partial class frm1B_ExportCSV : Form
-
{
-
private string strWorkingDir;
-
-
private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
strWorkingDir = ((frmMain)this.ParentForm).setWorkingDir("1B");
-
}
-
}
the calling form :
frm1B_ExportCSV f = new frm1B_ExportCSV ();
f.ShowDialog(th is);
the dialog Form: (you need to use the Owner property) - private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
strWorkingDir = ((frmMain)this.Owner).setWorkingDir("1B");
-
}
From your original post - private void frm1B_ExportCSV_Load(object sender, EventArgs e)
-
{
-
strWorkingDir = ((frmMain)this.Owner).setWorkingDir("1B");
-
}
That was it exactly. I knew it had to be something really small like that. Thanks a million Shashi!
Plater 7,872
Recognized Expert Expert
I was wondering about how you were calling it, the parent form and stuff only applies to mdi children, not owned dialogs. Glad an answer was found.
I was wondering about how you were calling it, the parent form and stuff only applies to mdi children, not owned dialogs. Glad an answer was found.
Probably some misnomers on my part, I'm still getting familiar with the terminology. Thanks Plater.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Terrence |
last post by:
I am doing some of the C# walkthroughs to transition from
VB to C#. When I try to execute
static void Main()
{
Aplication.Run(new Form1())
}
I raise a 'System.NullReferenceException" in
system.windows.forms.dll
|
by: Rafael |
last post by:
Hi, I hope I can find some help for this problem
IDE: Visual Studio.NET 2003 Developer Editio
Language: C#
Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll. Additional information: Object reference not set to an instance of an object.
Visual Studio's IDE is not allowing me to develop projects in C#... I've trying to create a simple Windows Forms project using Visual Studio.NET...
|
by: Maurice Mertens |
last post by:
Hi,
I'm having some troubles with a .NET application.
I have a MDI-parent form and MDI-child forms. When I press a shortcut key
in a MDI-child form (i.e. <ALT> + <D>) I get the following error:
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
|
by: Andreas Wirén |
last post by:
Hi I'm a C# .NET newbie doing a minor school project but I'm getting a
strange error message.
'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object."
As my references look I seem to be using the old framework(1.0.3705) for
this, but re-referencing to the newer framework(1.1.4322) dll's produces the
same error. This happens under all...
|
by: Raed Sawalha |
last post by:
i have a windows form(Main) with listview, when click an item in listview i
open other window form (Sub) which generate the selected item from parent
window in as treeview items when click any item in treeview i display the
content item in axWebBrowser, i close the sub form normally when i close the
main the following error is generated
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
...
| |
by: Peter Afonin |
last post by:
Hello,
I'm using this code to access a network share from an asp.net page:
Dim dir As DirectoryInfo = New DirectoryInfo("\\10.0.0.150\FormLib\")
Dim files() As FileInfo = dir.GetFiles("*.eps")
When I try to do it, I get this error:
System.UnauthorizedAccessException: Access to the path
|
by: Glenn Venzke |
last post by:
I have an aspx page that is set up to copy backed-up DB files from a shared
directory to a local folder. For some reason, it is being denied access to
the network share. I have the web app running under a domain account that I
know for a fact has access. it works fine when I log on to the network and
browse the directory manually. Even when I grant full control to everyone on
the share and the underlying folder itself, I still can't access...
|
by: Jeff |
last post by:
hey
asp.net 2.0
The line "e.Item.Parent.ChildItems.Remove(e.Item);" gives this error:
'e.Item' threw an exception of type 'System.NullReferenceException'
System.Web.UI.WebControls.MenuItem {System.NullReferenceException}
The is the code where the exception occur:
protected void mnuSidebar_MenuItemDataBound(object sender, MenuEventArgs e)
|
by: nygiantswin2005 |
last post by:
Hi
I am trying to resolve this bug that I have in this application.
The code is below. It will generate this Exception
System.UnauthorizedAccessException: Access to the path is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.FileInfo.MoveTo(String destFileName)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |