Click here to Skip to main content
16,020,182 members
Home / Discussions / C#
   

C#

 
GeneralRe: button event handler where it drops it? Pin
geomeo12325-Sep-24 9:20
geomeo12325-Sep-24 9:20 
GeneralRe: button event handler where it drops it? Pin
Ravi Bhavnani26-Sep-24 4:42
professionalRavi Bhavnani26-Sep-24 4:42 
GeneralRe: button event handler where it drops it? Pin
geomeo12326-Sep-24 7:37
geomeo12326-Sep-24 7:37 
GeneralRe: button event handler where it drops it? Pin
Ravi Bhavnani26-Sep-24 15:15
professionalRavi Bhavnani26-Sep-24 15:15 
QuestionC# Enable or disable menu in a form frm another form Pin
Ismael_199924-Aug-24 4:36
Ismael_199924-Aug-24 4:36 
AnswerRe: C# Enable or disable menu in a form frm another form Pin
Richard Andrew x6424-Aug-24 5:34
professionalRichard Andrew x6424-Aug-24 5:34 
GeneralRe: C# Enable or disable menu in a form frm another form Pin
Ismael_199926-Aug-24 4:40
Ismael_199926-Aug-24 4:40 
AnswerRe: C# Enable or disable menu in a form frm another form Pin
OriginalGriff24-Aug-24 18:18
mveOriginalGriff24-Aug-24 18:18 
OK, let's take this in two stages, starting with your error message:
Error
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!

GeneralRe: C# Enable or disable menu in a form frm another form Pin
Ismael_199926-Aug-24 4:42
Ismael_199926-Aug-24 4:42 
QuestionSystem.* not defined or imported Pin
Pieter Claassens15-Aug-24 4:22
Pieter Claassens15-Aug-24 4:22 
AnswerRe: System.* not defined or imported Pin
Richard MacCutchan15-Aug-24 4:56
mveRichard MacCutchan15-Aug-24 4:56 
GeneralRe: System.* not defined or imported Pin
Pieter Claassens15-Aug-24 5:03
Pieter Claassens15-Aug-24 5:03 
GeneralRe: System.* not defined or imported Pin
Richard MacCutchan15-Aug-24 5:26
mveRichard MacCutchan15-Aug-24 5:26 
GeneralRe: System.* not defined or imported Pin
Richard Andrew x6415-Aug-24 11:21
professionalRichard Andrew x6415-Aug-24 11:21 
GeneralRe: System.* not defined or imported Pin
Richard MacCutchan15-Aug-24 22:13
mveRichard MacCutchan15-Aug-24 22:13 
AnswerRe: System.* not defined or imported Pin
OriginalGriff15-Aug-24 19:17
mveOriginalGriff15-Aug-24 19:17 
QuestionCombobox item selection Pin
Member 95057569-Aug-24 22:46
Member 95057569-Aug-24 22:46 
AnswerRe: Combobox item selection Pin
OriginalGriff10-Aug-24 2:30
mveOriginalGriff10-Aug-24 2:30 
QuestionPrintQueueWatch Pin
Jorge Carlos Iglesias Alvarez2-Aug-24 6:44
Jorge Carlos Iglesias Alvarez2-Aug-24 6:44 
AnswerRe: PrintQueueWatch Pin
Richard Andrew x642-Aug-24 7:28
professionalRichard Andrew x642-Aug-24 7:28 
AnswerRe: PrintQueueWatch Pin
OriginalGriff2-Aug-24 19:49
mveOriginalGriff2-Aug-24 19:49 
Questionnetwork change event/Compare Pin
geomeo1232-Aug-24 4:47
geomeo1232-Aug-24 4:47 
AnswerRe: network change event/Compare Pin
RedDk2-Aug-24 8:58
RedDk2-Aug-24 8:58 
QuestionDataGrid - Change column Headers height and row vertical alignment text. Pin
Member 1340835626-Jul-24 7:50
Member 1340835626-Jul-24 7:50 
AnswerRe: DataGrid - Change column Headers height and row vertical alignment text. Pin
Dave Kreskowiak26-Jul-24 13:20
mveDave Kreskowiak26-Jul-24 13:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.