Check icon in menu - c#

I have been developing a windows application. I had built a menu bar. When I click on the menu option once, it show a checked sign. But again when I click on the same option the check sign does not goes off.
The code I had written is:
nmviewtextbox.Checked = !nmviewtextbox.Checked;
but its showing an error

I understand that you are calling this code from the CheckedChanged event and thus you are provoking an endless loop (this event is called every time nmviewtextbox.Checked is changed). In that case, delete this line (the method would continue working without any problem).
Otherwise (outside the event triggered when .Checked is changed), this code should work fine.
Bear in mind that, as suggested by Cody Gray, you can disable the check-on-click functionality (and thus allow this line of code to work without triggering any error).

Related

Locating an EventHandler through Application Usage C#

I'm converting a program from visual basic to C#, and in order to test it I need to change the configs so that when a menu option from a menu that appears after a right-click is pressed it calls the C# code I created rather than the old Visual Basic code. Obviously, that means locating the event handler. Unfortunately, I don't know the name of the menu option being pressed and have yet to guess what it is. Is there a way for me to run the application, and have the program pause (like a breakpoint) at the very first thing it does after the menu-option is pressed in the application? Right now I have a breakpoint on the first line of the method that is run. But is there a way to find the eventHandler that triggers that method, by running the program? Or is there another option to help me find what's triggering it?

Cannot edit after using 'Break All', although break points work fine

This seems to be a WPF-specific issue.
If I set a breakpoint in the code, the program will pause when it reaches that line, and I can edit/add/remove code, and then continue - it runs the newly edited code (ie, it behaves as expected).
However, if I hit 'Break All', I get moved to window that says "Your App has entered a Break State...". If I try to edit my code, nothing happens. I try to type but nothing happens - no text appears, and there's no errors that pop up or anything.
I don't have this problem in WinForms applications - just WPF. If I create a basic WPF project from the template, I still have the issue.
It's a very frustrating issue! My ugly hacked solution is to add a button to my program's UI called 'Break', which executes a single line of code that has a breakpoint on it, basically recreating the behaviour that 'Break All' should have.
Weirdly, it's not an issue if I'm on a WPF project which uses multiple threads. Hitting 'Break All' in this case acts as if there's a breakpoint on the line of code where the background thread is set up.
I am not sure..but you can give a try.
To enable/disable Edit and Continue. In the Options dialog box, expand the Debugging node, and select Edit and Continue. In the Options dialog box Edit and Continue page, select or clear the Enable Edit and Continue check box. The setting takes effect when you restart the debugging session
source:https://msdn.microsoft.com/en-us/library/ms164926.aspx

getting the debugger to break at the next click event

in a .net windows forms project which has 100s of forms, and all those forms has countless custom made controls with base classes involved, its very difficult for me to know where a particular button is, i mean whats the form name which I'm looking at while i'm running the application, and where exactly is the button click event, in code, of the button that I just clicked. Is there a debugging feature in Visual Studio, which would just break the execution for me to the line where the click happened. Can I tell VS to break at which ever Click event comes next?
(running visual studio 2012/13 these days).
thanks.
Just before you click the button in the program do this:
Go to visual studio and pause the program. Just press the pause button.
Then press F11 (Step Into).
Now press the button in the program, and you should be taken into the event handler.
For web projects, the technique suggested by Jakob Olsen will not really work, because you have no active thread in between the calls, and hence no thread to resume upon the next action. However what worked for me was:
Find some code (any code in your app) you know for sure it gets executed and set a breakpoint
Trigger this breakpoint, use SHIFT-F11 to step out until you're out of all methods
Now do the action of which you don't know what code is executed, and it will break
I can suggest partial solution.
If your click events are named like "Button_Click", open Breakpoints windows while in debug and create New breakpoint.
Click OK and you will see list of functions. Check them and click OK. On every function that you have selected will be created a breakpoint.

Dialog Box Delay / Not Displayed Fully for 2 Seconds While Application is Starting Up

I am new to Winforms development and I do not see a solution yet on Stackoverflow, but may have missed it.
I have a dialog box that comes up, but due to application startup processing, it is only half displayed for the first 2 seconds or so (i.e. shows border and the background except where controls will be shown). The control locations are white until controls are displayed after that initial 2 seconds.
I understand I could put a delay in the application while it is starting up, but would prefer something like a Suspend() / Resume() pair in strategic locations. I have tried putting in the load event, but that had no effect. Also, it looks like Refresh() breaks the Suspend/Resume. Ideas appreciated since I would like to use this strategy elsewhere in the application as well. I am wondering what is an approach that will work for this and other areas that flicker badly (or outright show a long delay before fully displaying like this startup dialog box as described).
Try putting your long-running code in the Load event handler instead. By putting it in the Shown event handler, it causes the form to freeze until it's done loading because the shown event handler is not letting other events in the message loop, e.g. the Paint event -- get processed. At least if you put it in the Load event, all the long running code will occur before anything gets displayed at all.
If you don't like having any delay, consider putting the long running code in a timer that kicks off in the Shown event.
Then there's always the BackgroundWorker if you want to get more advanced with long-running code.

Preventing user to click on a particular button when an action is being performed

Here's the scenario:
Platform: Windows
IDE: Microsoft Visual Studio 2008
Language: C#
.NET framework: 3.5
My application contains 2 buttons - "Load Data" and "Stop Loading Data" and a multi-line textbox. Upon clicking "Load Data" button some data starts getting loaded in the textbox. To prevent the user clicking on the "Load Data" button multiple times, I have disabled that button once it is clicked. When the entire data gets loaded in the textbox then the "Load Data" button gets activated again. On the other hand on clicking "Stop Loading Data" button the loading of data is stopped (if user wishes to stop it before loading the entire content).
As stated earlier, to prevent the user clicking on the "Load Data" button multiple times, I have disabled that button with the intention that user can only click on "Stop Loading Data" button or else wait for the entire data to be loaded in the textbox. I implemented this. At first glance it seemed to work well. But while testing I found that even though the "Load Data" button is disabled, if the user clicks on that button, although nothing happens at that instant but as soon as the entire data gets loaded and the button becomes enabled again, that click made during the disabled state is found to be executed. As if the program was recording the keystrokes and mouse clicks and waiting for the button to become active again. But there are no such keystrokes or mouse-clicks recording facility in my program. What is causing such an activity? How can I prevent such behavior?
Thanks.
One option would be to work with a reentrancy sentinel:
You could define an int field (initialize with 0) and update it via Interlocked.Increment on entering the method and only proceed if it is 1. At the end just do a Interlocked.Decrement.
To make it visible for the user you can disable the button at the beginning of execution and enable it when the execution is finished...
BTW: long-running tasks should be done async (via a separate thread for example)...
if you make your call a synchronous one, it will lock up the entire page until loading finishes.
Otherwise you'd be just doing the method you've already tried, i'd like to see your code for the disabled state, because something tells me you just made it appear to be disabled, and it was still a functional button
Check out this post:
http://www.codeguru.com/forum/showthread.php?t=480279
My first thought was removing the event handler and rebinding it at the end of the click event. This thread suggests using a BackgroundWorker and making it async.
I bet that you can't even move the form until data is loaded, and you also can't stop loading data. The problem is that the whole form freezes until loading is done. You must move the loading part in separate thread.
Well, another silution again:
On begin load simply hide a load button and in place of it (say) show a progress bar. On finish of loading or on stop loading click make load button visible again. In this case you avoid "chain clicks" management you complains about.
Or manage one button. First it is load, on click, instead, becomes stop load. Solution like this you would find often in mobile environment, considering the limited screen space. But I think it can be applied to desktop with great sucess too. Why not?

Categories