|
Hello
I am using C# to create an mdi application. i have the parent form which has its own toolbar and file menu, and a child form that has it own file menu and toolbar. When l press a command button to show the child form l want the parent form's file menu and toolbar to be replaced by the childs.
I have found out how to replace the toolbar, just by making it invisible on the parent form. But l cannot not replace the parent's file menu with the childs file menu.
Many help in advance
Steve
|
|
|
|
|
I'm not too sure but there are some Merging options you can control. Perhaps an event fires to??? Good luck
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
The parent calls MergeMenu on its menu with the child Form's menu. Read the SDK documentation for Form.MergedMenu and read the current MSDN Online series about a document/view application in .NET by Chris Sells: http://msdn.microsoft.com/library/en-us/dnforms/html/winforms10162003.asp[^] (this is part 2 that discusses menu merging about half-way down the page).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Has anyone used the IUrlHistory Interfaces in C#? I've got working samples in C++, but cannot find any samples in C#. Yeah... You can probably guess my COM and Interop knowledge is not enough to build a class for myself!
Any samples or further reading (beyond MSDN) would be appreciated!
|
|
|
|
|
Dave Conn wrote:
You can probably guess my COM and Interop knowledge is not enough to build a class for myself!
Good sign to learn then! There are several articles here on CP, including a good one[^] by Nick Parker about it.
You don't actually have to wrap this interface yourself, though. You can use a little trick[^] where you create a typelib out of the urlhist.idl file, then use tlbimp.exe from the .NET SDK to create an interop assembly out of it. You can then either implement the class (if you're hosting the MSHTML control yourself or something) or get an instance to that interface in order to call methods on it. Tip: casting an object to an in interface in .NET is the same as QI'ing for it in COM (.NET does it under the covers).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi all,
I wonder if anyone can help with a problem I am having with an AddIn I am
writing for Visual Studio.NET
I am try to add a submenu to the Edit menu with two items in it.
I would assume this is done by creating a CommandBar of type
vsCommandBarTypeMenu and specifying the Edit menu as the parent, such as the
code below:
CommandBar EditMenu = (CommandBar)applicationObject.CommandBars["Edit"];<br />
.......<br />
.........<br />
CommandBar MySubMenu = (CommandBar)applicationObject.Commands.AddCommandBar("My Sub Menu", vsCommandBarType.vsCommandBarTypeMenu, EditMenu, 0);
However, when I try to add the CommandBar, a COMException is thrown with the
message "Class not registered. Looking for object with CLID {.........}".
What am I doing wrong here?
Regards,
Chris
|
|
|
|
|
|
Chris Morrison wrote:
However, when I try to add the CommandBar, a COMException is thrown with the
message "Class not registered. Looking for object with CLID {.........}".
It sounds as if the CLSID is not being found in the registry, thus it is not able to load the COM Server because it doesn't know it's location. Are you using a third-party component that hasn't been registered? If so, pull up a command prompt, navigate to the directory of your COM .dll and type the following:
regsvr32 "[YourComponent.dll]"
Not entirely sure this is your problem, however here are a few links that might be of some help:
COMException Class[^]
HRESULTs and Exceptions[^]
Nick Parker - DeveloperNotes.com
VB gets under people's skin because one can be ignorant of computer architecture and ASM and still
get a lot done. -Don Box
|
|
|
|
|
Nick Parker wrote:
Chris Morrison wrote:
However, when I try to add the CommandBar, a COMException is thrown with the
message "Class not registered. Looking for object with CLID {.........}".
It sounds as if the CLSID is not being found in the regisry, thus it is not able to load the COM Server because it doesn't know it's location. Are you using a third-party component that hasn't been registered? If so, pull up a command prompt, navigate to the directory of your COM .dll and type the following:
regsvr32 "[YourComponent.dll]"
That did not work and caused an error.
I would have thought that any registrations that were required would be performed when I build and debug the Add-In. Also I have added a floating toolbar to the IDE with no problems, and I can add new items directly to the Edit menu with out error, so I can't see why I am having problems adding a sub menu to it.
This is very strange.
Chris
|
|
|
|
|
Try running regasm.exe on your .NET assembly. It is equivalent to regsvr32.exe but is for managed assemblies. Since such assemblies do not export DllRegisterServer or DllUnregisterServer , regsvr32.exe is useless.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
Since such assemblies do not export DllRegisterServer or DllUnregisterServer, regsvr32.exe is useless.
I would have suggested that but since it was throwing a COMException I figured it was due to a problem on the other side of the fence.
Oh well, it's the weekend after all.
-Nick Parker
VB gets under people's skin because one can be ignorant of computer architecture and ASM and still
get a lot done. -Don Box
|
|
|
|
|
VS.NET uses the DTE components via COM (even if written in managed code), so if it can't find the registered class (either because it wasn't registered correctly or because he moved the DLL after registering it) it would throw such a COMException .
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi:
I am doing a project ,in which I must use journal-hook
But all the help fille and example-code I saw is about
local-hook,what should I do in C# .
thanks
|
|
|
|
|
Continueing this repost trend wont give you an answer. Wtf is journal hooks anyways? How is it done in C++?
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
In C#.NET I wrapped WOpenEnum, WCloseEnum and WEnumResource so that I may go through the network domains, servers, and shares much like one would with the Directories class... (Directories.GetDirectories(string path)... you know...)... anyway to the problem:
In the Console (my dev environment) runs like a champ... no errors, the amount of memory used or threads opened doesn't change... really stable... I incorporate this set of files in a Forms environment though and errors start occuring randomly, more often when I'm querying faster... errors such as infinite recursion, null referance is mscrolib...etc... I really want to say it has something to do with GC, but I'm not an expert... but because the console is garbageless (I think) and Forms are constantly getting rid of things the GC doesn't think you need I'm looking that way... I don't know though. I've tried the KeepAlive method and other things but the problem still comes. I'm really not much on low level wrappers, but like I said, in the Console window it's running great... and I peiced this thing together best I can but am open to suggestions and learning experiance. I just can't get it to work worth a darn otherwise outside the console and it's frustrating the heck out of me. If you can help please email me and I'll send you the code... I don't care if it's public or not... a lot of people I'm sure could use something that would help them get network resources easier... I'm just not sure how to post a file if you even can... I'm new here.
Thanks,
Brian J.
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
One word: Paragraphs!
I think (without reading your post) the problem is, that you cant write to the Console form another thread.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
I'm with leppie on this one - use paragraphs next time. It's very difficult to follow your problem.
Since I can't really distinguish what you're problem is, let me give you a lesson or two on the .NET Framework. The GC only collects objects to which no references exist. If something is referencing an object in memory, it will NOT be GC'd. As far as your wrapper class for the Network API, all the methods you P/Invoked are static, which means they are methods of the Type - not an instance - so it can't be GC'd. If you get back a handle from one of those methods and aren't storing it (hence, not making a reference to it in your instance), then it will be GC'd.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I'll try to clarify...
I made a set of classes that make interface with the WOpenEnum, WCloseEnum, and WEnumResources API functions simpiler. In that set of classes I also have a public class called Network that has static methods for retrieving the domains, servers, or shared folders in a network using the three API above (GetDomains(), GetServers(string domain), GetSharedFolders(string server). This set of classes also has three public structs (Domain, Server, SharedFolder) that I use for returning the information gathered in the three functions above. If you've worked with those three API before you know there are 8 items of information that come back, but only two that are important to the user, the 'Remote Name' and the 'Comment', this is what I put in those structs.
Now in a seperate project I make a reference to those set of class (which I just call EnumNetwork.dll)... With just a simple console project I can put it in a loop (normally wouldn't do this, nor use goto) as the following:
Start:
Server [] myServers = Network.GetServers("myDomain");
foreach(Server s in myServers)
{
Console.WriteLine(s.Name); //write the name of the server
}
goto Start:
And with this the servers will scroll out the console window forever without a glitch.
Now in a Windows Form I have a ListView object to make things look what users are use too... assume I already have a computer icon set for the second parameter of Item.Add below (the 0):
Server [] myServers = Network.GetServers("myDomain");
this.lstView.BeginUpdate();
this.lstView.Items.Clear();
foreach(Server s in myServers)
{
this.lstView.Items.Add(s.Name, 0);
}
this.lstView.EndUpdate();
What happens with this is that occasionally when closing the window itself you get 'Null Reference in mscrolib', or 'object not set to a reference of an object in unknown module'... and if running this code more that once in a 10 second period, or if there are many servers you get 'Infinite Recursion' and and 'Unhandled exception in sscrolib'...
Now I don't know what you mean by paragraphs unless you were saying I need to type better... in which case sorry for that... otherwise I'm ignorant to the meaning... but what I'm getting at is calling that method in the console window constantly results in no errors... over hours (yes I tested it for that long)... yet calling it in a method on a form results in unpredicatable errors... and try...catch statements don't do anything because the errors are caused in the Application.Run line... like I said, if you need the source code... not a problem... I'll email... thanks for any help, and thanks for the good info on the GC...
Brian
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
Structs are not reference types - they are values types. If you think that different classes have reference to the same struct, you'd be wrong. They each have a separate copy of the struct because it is, once again, a value type.
This might have something to do with the ListView you're using, too. Since the ListViewItem is representing something that is getting destroyed by the GC (once structs are out of scope and the GC has a slice of time it cleans them out - including the values in the struct). Instead, when adding the s.Name to the list, use s.Name.Clone().ToString() . A copy of the string will be used instead of the string itself - which will get destroyed by the GC and a string is a reference type. This might be your problem.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks again for you response...
Clone(), CopyTo(), and Copy() all tried and all have the same problem... I would be glad to send you the source code if you think you'd have time to look over it. As I've said I'm not the most experienced... everyday I'm learning, so if you want to tear my code apart and tell me I'm an idoit... I'm fine with that ... otherwise any other ideas would be more than welcome.
Brian
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
You didn't mention that you were getting a NullReferenceException when using the field - only when you closed the application. The solution to this is simple - and is necessary in any good application - error handling! If you're not sure about input (and never trust user input for that matter!), always check to see if something is null (only reference types can be null, so a string can be). Instead, change your loop to something like:
foreach (Server s in servers)
{
if (s != null && s.Name != null)
this.listView.Items.Add(s.Name);
} Likewise, you should always include exception handling in your code - especially when you can trust a source or that an object you didn't instantiate (like a parameter) isn't null. Always check your inputs, never trust user input (validate!), and always make sure that errors / exceptions are handled gracefully and - if possible - recovered. This is key in every programming language / framework.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Actually I do have that check in my code already:
if(myServers!=null)
{
foreach(Server s in myServers)
{
if(s.Name!=null)
{
this.lstViewNetwork.Items.Add(s.Name.Clone().ToString(), 0);
}
}
}
This is the most common error and the one you mentioned:
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.
When hitting break it goes to assembly... not a line of code as you normally get with null reference... when it does have the mscrolib error it get the green arrow break (don't know what the differance between that and the yellow is) but that is in the '}' underneath application.run(form)
Like I said, I'll send you the code if you have the time to look it.
Thanks again
Brian
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
Here is the code for the struct I use in the example:
<br />
public struct Server<br />
{<br />
private string n, c;<br />
public string Name<br />
{<br />
get { return n; }<br />
set { n = value; }<br />
}<br />
public string Comment<br />
{<br />
get { return c; }<br />
set { c = value; }<br />
}<br />
public Server(string name, string comment)<br />
{<br />
n = name;<br />
c = comment;<br />
}<br />
}<br />
Here is the code for the GetServers function, this function is part of the public class Network(), the dotNet_OpenEnum, dotNet_CloseEnum, and dotNet_EnumResources are static functions in a wrapper class for each of the API network functions... I did this versus calling the external function directly, the Flags class is simply const int's for various flags used when using the API network functions:
<br />
public static Server[] GetServers(string domainName)<br />
{<br />
int ret;<br />
IntPtr ptrBuffer;<br />
IntPtr ptrHandle = new IntPtr();<br />
int BufferSize = short.MaxValue;<br />
int NumberRequested = -1;<br />
NETRESOURCE objNR = new NETRESOURCE();<br />
objNR.lpRemoteName = domainName;<br />
object obj = (object) objNR;<br />
if(obj==null)<br />
{<br />
return null;<br />
}<br />
ret = dotNet_WNetOpenEnum.Execute(Flags.RESOURCE_GLOBALNET, Flags.RESOURCETYPE_ANY, Flags.RESOURCEUSAGE_ALL, obj, out ptrHandle);<br />
if(ret!=0)<br />
{<br />
if(ptrHandle!=IntPtr.Zero)
{<br />
ret = dotNet_WNetCloseEnum.Execute(ptrHandle);<br />
}<br />
return null;<br />
}<br />
ret = 234;<br />
ptrBuffer = Marshal.AllocHGlobal(0);<br />
while(ret==234 || ret==487)
{<br />
NumberRequested=-1;<br />
ret = dotNet_WNetEnumResource.Execute(ptrHandle, ref NumberRequested, ptrBuffer, ref BufferSize);<br />
if(ret!=0)<br />
{<br />
Marshal.FreeHGlobal(ptrBuffer);<br />
BufferSize*=2;
ptrBuffer = Marshal.AllocHGlobal(BufferSize);<br />
}<br />
}<br />
if((ret!=0) || (NumberRequested < 1))<br />
{<br />
if(ptrBuffer!=IntPtr.Zero)<br />
Marshal.FreeHGlobal(ptrBuffer);
if(ptrHandle!=IntPtr.Zero)
{<br />
ret = dotNet_WNetCloseEnum.Execute(ptrHandle);<br />
}<br />
return null;<br />
} <br />
Server [] servers = new Server[NumberRequested];<br />
int PtrMovement;<br />
NETRESOURCE nr;<br />
PtrMovement = ptrBuffer.ToInt32();<br />
for(int n=0;n<NumberRequested;n++)<br />
{ <br />
nr = (NETRESOURCE) Marshal.PtrToStructure(new IntPtr(PtrMovement), typeof(NETRESOURCE));<br />
servers[n] = new Server(nr.lpRemoteName, nr.lpComment);<br />
PtrMovement += Marshal.SizeOf(typeof(NETRESOURCE));<br />
}<br />
Marshal.FreeHGlobal(ptrBuffer);
ret = dotNet_WNetCloseEnum.Execute(ptrHandle);<br />
return servers;<br />
}<br />
Thanks for any help!
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
Heath -
So I took out all of the additions we made and put it back to square one and at the bottom of the function I added GC.Collect():
<br />
string domain="Domain1";<br />
this.lstViewNetwork.Items.Clear();<br />
Server [] myServers;<br />
myServers = Network.GetServers(domain);<br />
this.lstViewNetwork.BeginUpdate();<br />
if(myServers!=null)<br />
{<br />
foreach(Server s in myServers)<br />
{<br />
if(s.Name!=null)<br />
{<br />
this.lstViewNetwork.Items.Add(s.Name, 0);<br />
}<br />
}<br />
}<br />
count++;<br />
this.lstViewNetwork.EndUpdate();<br />
this.lblRefresh.Text = count.ToString();<br />
GC.Collect();<br />
}<br />
Now I know longer get errors, and everything is working perfectly (... some weird things still happen, when the screen saver started the form became the screen saver... weird... and once I got an error on closing the form, but after several attempts to get it to happen I haven't been able to, and something similiar when losing and regain focus to the form, it can now go throughs several thousand iterations before either of these errors happen whereas before we couldn't go more than 150. I'm not sure why GC.Collect() has made the error go away, but I know a 'fix to the problem' and 'fixing the problem' are two seperate worlds, and I was hoping maybe with this information and the other information I've given since your last reply will set some bells off in your head... any ideas?
Thanks again for all your help with this.
Brian
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
Are you executing this method in a separate thread? If so, you must make all UI updates on the main UI thread. See Control.InvokeRequired and Control.Invoke for more information. Basically, you invoke your method (using a delegate with a matching signature to this.lstViewNetwork.Items.Add(s.Name, 0) and call Control.Invoke on that. This can cause similar problems to what you're seeing if you do add items on a separate thread. This is just the way Windows works (and most other window managers that I'm familiar with).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|