|
Oh no! Don't do it like this!!!
use
Control.TopLevelControl
property!!!
|
|
|
|
|
Yeah, Luc Pattyn's reply[^] reminded me of it. Of course a direct reference to the Form is better, that's what I said in my reply, I just forgot that the reference is already there as the TopLevelControl property provided by the framework.
Eslam Afifi
|
|
|
|
|
as the others said, however you could take advantage of Control.TopLevelControl to get to the parent Form.
|
|
|
|
|
Hehe, very true. See, this is why I love CP. Learn something new everyday! :-P
|
|
|
|
|
Thanks Guys,
With your help i have found a working solution for me. .NET 2.0 framework. The var i cannot use yet
private void butSave_Click(object sender, EventArgs e)
{
try
{
bool YesNo = xxxSaveData();
if (YesNo)
{
if (Parent is Form)
{
Form myParent = (Form)Parent;
myParent.Close();
}
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
}
Kind regards,
Marcel Vreuls
MarcelVreuls.com
<<A good idea can change your life>>
|
|
|
|
|
Haven't you read the comments?
Change to:
private void butSave_Click(object sender, EventArgs e)
{
try
{
bool YesNo = xxxSaveData();
if (YesNo)
{
Control ctrlSender = sender;
(ctrlSender.TopLevelControl as Form).Close();
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
}
or your code depends on the control beeing placed on the form directly.
|
|
|
|
|
Hi All,
For a few weeks now i am strungling with an anoying issue. I have a MDI Parent form. From this form i have child forms (normal winforms) which i open. These forms are opened withing the MDI parent form. So far so so good. And know my problem. From this childform i open a new form through a button. There is for me no way to open this form as mdi child form because its parent is also an mdi child. I have looked for looping throug form collections but cannot find the solution to my problem.
How can i detect the MDI Parent of my application from within all open forms.
Thanks Marcel Vreuls
Kind regards,
Marcel Vreuls
MarcelVreuls.com
<<A good idea can change your life>>
|
|
|
|
|
Well, since you should know the parent form's class name this is what I've done recently. I can't guarantee that it is the best method of doing it but I also haven't found any other solution:
private void Button1_Click(object sender, EventArgs e)
{
((AppForm)this.MdiParent).SomeMethodToOpenChild();
}
Casting the current child form's 'Form MdiParent' to the application's form type, then calling a method that opens the necessary child window. Like I said, I don't know how safe that is. But I believe you could check ahead of time like this:
if (this.MdiParent is AppForm)
((AppForm)this.MdiParent).SomeMethodToOpenChild();
I hope this at least gets you going in the right direction.
|
|
|
|
|
If the form is an MdiChild, you can get a reference to its parent using the MdiParent property. If you want to set another form as a child of the same parent, it's pretty easy
If you want a MdiChild form to be both a MdiChild to one Parent and a MdiParent to another child form, you can't. It's not supported. A form cannot be both a MdiChild and MdiParent at the same time.
|
|
|
|
|
Great answer Dave.
The mind is like a parachute. It doesn’t work unless it’s open.
|
|
|
|
|
hi i am trying to do malloc in vs.net 2005 c# window form as i am trying to dynamically allocate memory for a array which i dont know the size.
am i missing a reference or something?
or is there some other way to do it?
please advice
|
|
|
|
|
neodeaths wrote: is there some other way to do it?
A simple declaration might look like this;
string[,] names = new string[5,4];
More information and examples can be found on MSDN[^].
--edit
If you don't know the size beforehand, there's two options;
I'd go for a generic list; it's dynamic and can be easily converted[^] to an array.
I are Troll
|
|
|
|
|
if you don't know the size, how on earth is malloc going to be any good?
may I suggest you buy and study a beginners book on C# (or any language you choose), so you get the basics explained in a thorough and consistent way.
|
|
|
|
|
The only reason to use malloc would be if you're passing stuff to unmanaged code. If all you want is an array, you just need to declare it. you don't have to worry about memory management the way you do with C.
|
|
|
|
|
use a dynamic List<> or an ArrayList or any of the other nice things build into the .Net Framework (can be found under the System.Collections and System.Collections.Generic namespaces)
And then start thinking in .Net! Buy a book, learn the basics. Don't get confused by the "C-style syntax" of C# - this IS NOT C in any way!
|
|
|
|
|
Hi there,
Check this out. It happends with all programs i have installed. I have WinXP SP2. When i open any program, lets say Winamp for example, the program starts with 11.000 KiB of memory usage, i minimize the program and immediatly restore it and now it uses 1.500 KiB.
Why is this happening? Do applications need to start minimized and then manually restored to make use (and show in the task manager) of the proper memory space?
Again, it happends with every windowed app. This issue is kind of important to me right now because i have to show the efficiency of a demo, in terms of response time and memory usage.
Any thoughs about this? Please try it.
Thanks.
|
|
|
|
|
Heinz_ wrote: Why is this happening?
There's an amount of memory reserved to be used by your application
I are Troll
|
|
|
|
|
First, stop looking at Task Manager to get your memory stats for your app. It's lying to you.
Next, I suggest reading up on .NET Memory Management and Garbage Collector to understand how it really works.
A .NET application runs in something like a virtual machine, sort of like Java does. What Task Manager is showing you is the memory that is RESERVED for the process, NOT what your app is actually using. Since the .NET CLR handles memory management for your application, it has to get memory from Windows to allocate to your application. This chunk of memory is the Managed Heap and all of your application objects are allocated from it. When your app frees an object, the memory goes back into the Managed Heap, not back to Windows. This is why your app appears to use more memory than it actually does.
The .NET CLR tries to keep this pool of memory at a resonable level, constantly watching how your app allocates objects and trying to predict when it's going to need more managed heap. It doesn't take near as much time to allocate an object in the Managed Heap than if it had to go to Windows to request more memory, add it to the heap, then allocate your object. The reverse is also true. If the CLR determines that there is an over abundance of unused Heap, it can return some of that memory back to Windows. Or, can even release as much memory as possible if Windows starts to run low and needs it back.
When you minimize the app, it's no longer in the foreground. The .NET CLR trims back the Managed Heap to something closer to what your app is actually using and returns that released memory back to Windows.
|
|
|
|
|
Great answer Dave. I think every developer needs to get a basic understanding of the .Net CLR.
Sounds like you read my book. (I haven't wrote any books)
The mind is like a parachute. It doesn’t work unless it’s open.
|
|
|
|
|
That's OK. I don't read any books, so there's really no need to write one.
|
|
|
|
|
Richard Blythe wrote: Sounds like you read my book.
Dave only reads the back of cereal packets. His knowledge of IT is picked up by being in total harmony with the universe.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: Dave only reads the back of cereal packets.
How the hell did you know that?!
No, seriously?
|
|
|
|
|
I'll send you some box tops...
The mind is like a parachute. It doesn’t work unless it’s open.
|
|
|
|
|
Thanks for this clear and concise explanation. Sometimes one doesn't know what one doesn't know - if you know what I mean! 5 from me.
It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca
|
|
|
|
|
Thanks for the reply.
I'm not using the task manager to get the memory stats in my app, i'm using GetProcessMemoryInfo() from the Win32 API wich is the same i guess.
The real thing is: Try this minimize-freemem thing with WinRAR and Task Manager. WinRAR is not .NET (.NOT for me ). So, i think .NET is not the issue here. This also applies to Win32 and all subsequent wrappers.
Anyway, for my app i'm also using native Win32. Don't know why your reply is strictly related to .NET. "Windows Forms" is not .NET. WinForms is present also in COM programming (VB6 for example) and MFC so i guess i'm not in the wrong forum (unless this forum is specific for the System.Windows.Forms.dll .NET module).
.NET or not, my question has been answered anyway ("...What Task Manager is showing you is the memory that is RESERVED for the process, NOT what your app is actually using...").
Thank u very much.
-- Modified Monday, August 16, 2010 3:12 AM
|
|
|
|