Windows forms: Finding code that belongs to a button - winforms

i have a very simple question but i just can't find the answer. I have an existing Windows forms project which shows me an interface with lots of different buttons when i run it in Visual Studio 2019.
Now i want to find a way to find the code belonging to a button. I already tried clicking the buttons, but nothing happens.
So my question is: How can i automatically jump to the code that is related to one of the buttons?
Thanks a lot!

I am assuming that when you say "the code belonging to a button", you are referring to the code that runs when the button is clicked. In this case there are several ways of getting to this code.
In Winforms (not sure about anything else) you should be able to double click on the button and visual studio should navigate automatically to the code in the Button.Click event. If there is no Button.Click event handler for the button, it will create one.
Another way to get there is to select the button in the designer and go to the properties window(press F4). In the properties window go to the events list(see images), then double click on the "Click" event in this list and you should be directed to the code in the Button.Click event handler. If there is none, it will create one.
I hope this helps. If not, provide more details about your issue.

Related

Page in WPF losing focus

I have a Page in WPF which has a text box. This text box is set to be focused on load.
There is an application running in the background which takes a string value of a biometrics scan result and places it in that text box. This is a 3rd party application, and we have been told that it looks for a text box with a certain name, in an application with a certain name and if that text box is focused on, it will write that string in.
This works perfectly fine, but below is the problem I am having.
We have another piece of software which is a custom on screen keyboard. The text box can also be written into so the user can click a button to bring up this keyboard then start typing. However when this keyboard comes up, it seems to lose focus to my application and it doesnt come back again. For the lifecycle of our application running, the biometrics doesn't work because it doesn't see my application as the one currently in focus.
How do I force my application to come back in focus, but only when I want it to. I don't want it ALWAYS to be in focus and on the top, else the custom keyboard would never show.
I looked into Application.Activated Event with no joy, and even tried the following
public static extern bool SetForegroundWindow(IntPtr hWnd);
Getting a but stuck, does anyone have any advice?
Have you tried Activate() and Focus() methods?
This thread has a bunch of answers on topic Bring a window to the front in WPF
This particular one worked for me https://stackoverflow.com/a/7559766/305020

WPF Popup permanently displays in Visual Studio design-time

I’ve met strange behaviour for WPF design-time in Visual Studio 2010: after an instance of the Popup class was created, and I switched the code tab in Visual Studio to a different file, the Popup still remains on the screen!
I have a piece of code, which allows to reproduce this, but I am not sure if I should paste it here (it's not so short), so maybe I'll just give a link to it: here.
For unknown reasons beyond mere mortals' comprehension, Microsoft has decided this is the default behavior of the Popup class in WPF. You have to implement the "hiding" logic yourself. I suggest handling the Window.LocationChanged, Window.Activated and Window.Deactivated events of the Window containing the Popup and close it yourself.
Edit: To clarify myself, the Window events you need to handle are the events of the window that contains the Popup's PlacementTarget element. Usually when you create a popup, you set it relative to some element contained in an application's Window (similar to how the tooltips work). If this is your case, then my solution is correct, but I forgot to mention this point about the PlacementTarget.
In your code behind; you can simple check this boolean:
DesignerProperties.GetIsInDesignMode(this);
"this" represent the object containing the popup. For example the Window.
If true you can say:
myPopUp.IsOpen = false;
For Store Apps/WinRT:
Windows.ApplicationModel.DesignMode.DesignModeEnabled

Tool to know Windows Form Application's Form fields

I am working on a WinForm Application.
The Form has many fields/components but is poorly built.
for example a field is used as user name on one case and used as folder path on the other case. Code is quite poorly maintaned.
Is is possible that when i run the application and GUI appears, i can use a tool like 'spy++' which can show me 'names' of the components (not ids). For instance the name of a button or name of a label.
Or if i can use SPY++ for 'names' please tell me?
I would solve the problem by adding a ToolTip control to your form and iterating over each control and adding a Tool Tip message to each control that is the name of the control.
First, add a ToolTip object to your form (from the Tools section of the designer.) You can rename it, but for the sake of my demo, I left it as the default name toolTip1.
Next, add a method similar to the one I'm posting below to the code page of your form. (I'm assuming this is for C# but the code is simple and can easily be modified for VB or C++).
public void AddNameToToolTip(Control c)
{
toolTip1.SetToolTip(c, c.Name);
foreach (Control child in c.Controls) AddNameToToolTip(child);
}
Finally, from within the Form constructor, add the following line of code after the call to InitializeComponent().
AddNameToToolTip(this);
This will add a ToolTip message to each control in your form. All you should have to do is hover your mouse over each control and the ToolTip will pop up a message after a second or two displaying the name of the underlying control.
Alternatively, you can recursively adding a MouseHover event to each control and when the event is fired, write the name of the control to the debugger. This would also work if you are already using a ToolTip control within your form.

Disable a right click (press and hold) in WPF application.

I am working on the touch screen application which is running on Windows XP Standard. With current hardware to invoke a right click user has to click and hold for couple of seconds, but this might interfere with other actions like holding a repeat button in the scrollviewer, so I have decide to disable a right click.
I would ideally wan't to disable a right click on the application level, but if it is not possible, disable right click on windows level would also work for me.
The OnPreviewMouseRightButtonDown/Up approach did not work for me.
There is a property Stylus.IsPressAndHoldEnabled on UIElement however. Set that to false to get rid of the press and hold right click behavior. I tested this on Windows 7 with .NET 4.0, but the property is available from .NET 3.0.
<RepeatButton Stylus.IsPressAndHoldEnabled="false" ... />
There is also a blogpost here that provides a code sample for a similar disabling of press and hold at window level. But with this in place, the PreviewTouchDown and TouchDown events will not be raised as soon as the finger touches the screen (which would be necessary for a RepeatButton I guess), only later. Read the 'Remarks' on this msdn page.
You can override the OnPreviewMouseRightButtonDown on the Window and set Handled to true. You also need to handle OnPreviewMouseRightButtonUp (thanks to Vitalij for pointing this out)
That should do the trick.

Navigate to Event Handler Does not work in WPF Application

I am studying a code base developed using .NET WPF. I am using Visual Studio 2008 IDE. In the XAML code, I have a line as follows:
<MenuItem Header="About" Click="Main_Window_ContextMenu_About_Click">
Clicking right mouse button, I see a option called "Navigate to Event Handler". However, clicking it does not take me to the Event Handler definition. In fact, this action seems to have no effect.
Why is this happening and how can I fix this issue?
If you're using ReSharper, hit F12 while the cursor is on the event handler name (assuming you're using ReSharper's default shortcuts)
That shortcut in the XAML designer isn't very reliable - it only works when the event handler method is defined in the code-behind for the XAML file. For example, if you have "Window.xaml" it will only work if the handler is in "Window.xaml.cs".
This breaks, for example, if the event handler is defined in a second partial class file like "Window_EventHandlers.cs". This particular one even generates a new blank event handler in the code-behind file which promptly fails to compile on a duplicate method definition - yuck!
If you can't find it, your best bet is probably a solution or project-wide search for the method name.
This is a known issue. There are several ways to fix it, including re installation.

Resources