|
Annoying, isn't it?
I've never found a solution to that either, and I've been using VS for 15 years ...
"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!
|
|
|
|
|
YES VERY VERY ANNOYING lol!! Kind of glad I'm not alone! Come on MicroSoft. Geez a whiz. It puts it in a random spot in the middle of nowhere. Where's the add event by line number?? That would make so much sense. Why MicroSoft Whyyyyy??? It would also be nice to apply a static do not expand my code unless I absolutely say so clause. And my last rant of the day is the 3 word line of code that can be simplified warning? Ok it's 3 words how much simpler can it be? I have a few more rants, but I'll table them for another day. Other than that it's a great product. And if I knew someone that was professional at this to tell me where and why's of doing things I would probably rant a lot less. But as it is all I have is Youtube and a lot of books. And nice forums like this one. So let's all be friends and give us our event handler by line number !
|
|
|
|
|
geomeo123 wrote: Where's the add event by line number?
Well ... no. Line number isn't helpful, and it's far too easy to add it in the wrong place and get code that doesn't compile - which might be quite difficult to spot! I'd rather have to move it to the right region of the file manually - which isn't hard to do - than have it appear in the middle of an XML comment, or a switch block for example.
"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!
|
|
|
|
|
Quote: and it's far too easy to add it in the wrong place and get code that doesn't compile
That would be pretty impossible. Since any time you click for the eventHandler MicroSoft prompts you exactly where your event code has landed so that you can start writing what you want to do when the event has occurred. I'm fairly sure choosing the line number rather than have that done for us would work.
|
|
|
|
|
You can manually add the handler name to your button in the properties window, and then add the actual handler code wherever you want.
|
|
|
|
|
Can you explain? You lost me on the word manually? Do you mean manually type it in to the event handler property text box then manually write it to whereever I want? Screenshots would be nice.
|
|
|
|
|
It seems that things have changed since I last did this, and even typing the event name into the event handler, VS automatically adds the handler skeleton at the end of the Form class.
|
|
|
|
|
Find a program with over 1500 lines of code. It doesn't add to the end of the code anymore. (which would be nice too).
|
|
|
|
|
Where would VS know where to definitively add the handler, other than at the end of the class definition in the first file of the class?
The code for a Form class could be spread across several files, so this makes identifying a target location harder. I organize my code in #region elements and have gotten (there - I said it ) used to moving the handler to an appropriate location.
/ravi
|
|
|
|
|
I don't understand your question?
|
|
|
|
|
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!
|
|
|
|
|