|
I was suggesting there's no definitive way for VS to know where to place the new handler.
/ravi
|
|
|
|
|
Did you read the entire thread?
|
|
|
|
|
|
I have an application developed in Visual Studio 2022 that works with forms. In this application, there is a main form, from which other forms can be opened. In this main form, there is an item in the top menu (“selection”), whose name is M_Selecao, which must be activated or deactivated depending on which form was opened.
For example, when the “Expenses” form is opened, the menu must be activated (enabled).
When this secondary form is closed, the “selection” menu must be disabled. I can enable the menu, but I can’t disable it. I tried to make the main form’s menu “public” in the “TelaPrincipal.Designer.cs” file:
public System.Windows.Forms.ToolStripMenuItem M_Selecao;
This way I could disable the menu in the main form when closing the secondary form, but I get the message that "an object reference is required for the method or property 'TelaPrincipal.M_Selecao'. I also don't know how to do it from the main form. Can anyone help me? Thanks.
|
|
|
|
|
|
Ok, Richard. I'll try it. Thanks
|
|
|
|
|
OK, let's take this in two stages, starting with your error message:
An object reference is required for the non-static field, method, or property 'TelaPrincipal.M_Selecao' This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
The second part is what you are trying to do, and the whole way you are going about it - so let me summarise what you are trying to achieve:
You have a main form which opens a "child" form. When some of these forms are opened, a main form menu option must be enabled, and disabled again when it closes.
First off, don't make the menu item public so that the child forms can enable or disable it - that requires the child form to "know" about how it's parent works and even that a specific parent type exists - which is against the principles of OOPs design, and complicates your code.
Secondly, that's a pretty nasty things to do, because in order to activate the menu item, the child form cannot be modal - which means it must be opened by calling Show instead of ShowDialog . The difference is that a modal form (opened with ShowDialog ) does not allow the user to continue working with the main form until the child form is closed so that menu options can't even be selected. Opening it with Show does allow the user to continue working with the main form and thus selecting items from the menu, but ... it also allows the user to do whatever opened the child form in the first place!
Which means he can open a second copy of the child form, or worse of a form that should have the menu option disabled. What state should the menu option be in then? Enabled (because the original child is open) or disabled (because a different child is also opened)? From the perspective of the user that's confusing, and could lead to problems!
So first you need to think about what you are trying to do: open a form and enable the menu item - but forbid any other form from opening, or enable and disable the parent form menu item not just when the child form is open, but when it has the input focus as well! Both can be done, but they require changes in the parent form, and communications between the two forms - the child form must tell the parent what is happening and let it decide what to do instead of trying to make changes to it itself. The way that is done in C# is via events just like clicking a button raises an event your form handles. Those aren't actually difficult to work with - though they can seem confusing - all you have to do is know how to create and raise them.
When you have decided exactly what you want to happen have a look here: Transferring information between two forms, Part 2: Child to Parent[^] - it explains how to do talk from your child form to the main form and provides sample code. You can create events to ask the parent form to Enable or Disable the menu item and let it worry about everything else - the child form then raises the event as needed and the main form just handles it internally.
I know that all probably sounds confusing and you probably wanted us to just give you code which solved your problem but it's not that simple - you need to make decisions about how your app is supposed to work, and we can't do that for you!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Once again you help me, OriginalGriff. I'll Think about. Thanks.
|
|
|
|
|
Good afternoon,
a bit stumped with this one - any help would be greatly appreciated:
System.Void, System.Object, System.Inptr not defined or imported... a little bit stumped with this one... any help would be greatly be appreciated...
Regards,
Pieter Claassens
|
|
|
|
|
As you have been told repeatedly, a couple of words, taken out of context, does not give enough information to know what the problem is. Please show the actual code you are using and explain what you are trying to do.
|
|
|
|
|
Hi, Here is a sample exceprt of the code prudicing the erorr message :
public delegate void CallbackType(bool success, TReturnType returnValue);
Quite a lot of errors/warnings for a one-liner...
|
|
|
|
|
What is CallbackType , what is TReturnType . And did you write this code or are you trying to use something that someone else has written. As I keep saying, please show the full context, that means the code whare the erro(s) occur, and the actual error message(s). A single line out of context, with no accompanying detail, is still not enough. You may know what is showing on your screen terminal but we cannot see it.
|
|
|
|
|
Maybe he's paranoid and doesn't want anyone to see his code. Hence the obfuscation of the types.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Actually I think there is a much simpler answer.
|
|
|
|
|
To add to what Richard has said, the cause of a syntax error isn't always where the compiler reports a problem - it may be in lines before or even after the report!
For example, an additional "}" can cause some odd errors because the class and / o9r namespace has ben "closed" as far as the compiler is concerned, and it doesn't "know" what to do with your code that follows it.
So when you try to report a problem to us, you need to give us more than just one line and an error message - we need context for that and that means maybe a dozen lines either side.
This may help: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 1/4 hour for Chris to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!
I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Is possible selected the combobox item not all row but portion row during mouseover item?
best regards
|
|
|
|
|
Not in a standard combobox - it's a single row item which selects the entire item.
What are you trying to achieve that you think you need this: there may be a better way.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Anybody has use the PrintQueueWatch dll for monitor printJobs and printers.
I'm using the PrintQueueWatch dll for a simple app to monitor printers.
I wanna know how to use the function
PrintJob.Transfer.(string printerName, bool transfer)
The function is for transfer the print job to another printer, It's seems very easy to use, the function needs the printer name and a bool value to keep or not the print job in the original printer
but the problem is that always raises an exception
access denied
printQueueWatch collection = new PrintQueueWatch.PrinterCollection();
PrinterInformation printer = collection.Where(l=>l.printername == "the printer that I'm using")
printer.PrinterJobs.getJobById(Id).Transfer("the printer to transfer the printerJob", false) // false to keep the job in the original Printer.
The transfer function says access denied, it seems permissions to acces the new printer.
Thanks
|
|
|
|
|
You should ask whoever publishes the PrintQueueWatch DLL.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
At a guess - and I don't play with print queues so that's all it can be - it's that your app needs elevation, or you need specific permissions to access the two print queues involved.
"Access denied" means you don't have a required permission, so it's likely to be one or the other. Which makes a lot of sense, since the print queue may contain items from multiple users!
The simplest way to check which is to run your app in admin mode and see if the problem goes away.
If it does, then that's what you need - an elevated app.
If it doesn't, then you need to tell the printer queues "who you are" in order to change them. This may help: https://stackoverflow.com/questions/8348743/access-denied-trying-to-purge-printqueue-in-c-sharp[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm trying to detect when an ethernet cable is unplugged from a computer or plugged back in. I have some devices that when plugged in they come up as Unidentified Network(which is normal for the specific device). I'm not looking for internet connection or have anything to do with that. And I think this code works as a trigger:
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
private static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface n in adapters)
{
MessageBox.Show(n.Name.ToString() + n.OperationalStatus.ToString());
}
}
What I need to do now is to figure out which network adapter was unplugged. I intend to get the operational status of each network adapter. Store it, ask user to power cycle the device and then compare with the stored value(s) to figure out which one has changed from up to down. I'm not sure what the best method to do this would be. Should I do this as an array, list or is there an easier way?
|
|
|
|
|
Surely this return will aid in your finding a good answer to a problem:
CP Search[^]
|
|
|
|
|
In my application I use System.Windows.Forms.DataGrid (not DataGridView!) Unfortunately, I cannot change ColumnHeadersHeight - there is no such property for this control. And secondly, if I increase the row height, text is aligned to the top of row, but vertical alignment is needed. Is there any solution to this problem?
|
|
|
|
|
The old DataGrid wasn't very flexible. You don't have the control over its rendering like you do with the DataGridView.
The only way to SIMPLY get the functionality you want is to drop the DataGrid and replace it with the DataGridView. Otherwise, you're writing a whole bunch of drawing code and drawing the DataGrid yourself. You really don't want to do that.
|
|
|
|
|
When I execute a stored procedure with SqlCommand.ExecuteReader, how do I access the specific result set I need in the case that the stored procedure returns more than one result set?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|