0

I have not found any solution for this issue. I have a WinForms application that is under development in Visual Studio 2017.

  • Event handlers are not showing up in the WinForm Designer.

  • Event handlers are present in the code and work. So this is not an immediate show stopper.

  • Event handlers were added through the WinForms Designer. I then went to the source code and fleshed out the body of the event handlers.

  • However, they do not appear in the Windows Form Designer list of events.

Region "Event Handlers"

    Private Sub OnCellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles RosterDataGrid.CellValueChanged

        Dim changedRow As DataRow = _excelRoster.RosterDataSet.Tables(0).Rows().Item(e.RowIndex)

        changedRow.Item(1) = "Updated"
        _rosterControl.Updated = _rosterControl.Updated + 1

    End Sub

    Private Sub OnRowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles RosterDataGrid.RowsAdded

        MsgBox("Row Added")

    End Sub

#End Region
  • But do not show up in the Windows Form Designer. Note that OnCellValueChanged that handles CellValueChanged does not show up at all in the Events property of the Windows Form Designer.

enter image description here

In the image above, the events for the selected DataGridView are not present in the Windows Form Designer.

enter image description here

The image above shows the result of the OnCellValueChanged event handler firing. The Change Type column is stamped with "Updated" and the Updated count for Aetna is set to 1.

The image below shows the OnRowsAdded event firing. There is no entry shown in the Windows Form Designer for the RowsAdded event for this DataGridView.

enter image description here

This could be an impediment to other developers who may need to work on this code in the future. Problems like this are indicative that something is fundamentally wrong and could lead to serious problems.

  • Control Property settings are showing up.

  • I am using the version 15.9.55 of Visual Studio 2017

  • Additionally, the "Report a Problem" item in Visual Studio 2017 is not working.

How I am adding event handlers

Below, I am adding an event handler for the RowsRemoved event of my DataGridView control.

  1. In the Windows Form Designer, I locate the RowsRemoved event and enter OnRowsRemoved. enter image description here
  2. This adds the following code.

    Private Sub OnRowsRemoved(sender As Object, e As DataGridViewRowsRemovedEventArgs) Handles RosterDataGrid.RowsRemoved

    End Sub

Note that the text OnRowsRemoved does not exist in the InitializeComponent method in the Designer code. No AddHandler was added to InitializeComponent.

  1. When I switch back to the Windows Form Designer, there is no entry for the RowsRemoved event.

enter image description here

What I have tried thus far

  • Repair from the Visual Studio Installer. This does not fix this issue.

  • "Report a Problem" in Visual Studio 2017 does not work. So I could not Report the Problem.

  • Cleared the Visual Studio 2017 cache by:

  1. Close Visual Studio (ensure devenv.exe is not present in the Task Manager)
  2. Delete the %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\15.0\ComponentModelCache directory.
  3. Restart Visual Studio.

Note: There is no option to Clear the Cache under the More button in the Visual Studio Installer.

Questions

  • Where does the WinForms designer obtain information on event handlers for controls?

  • The event handlers are present in code and work.

I do not see any event handlers in the Designer code file at all. This is the reason for the question on where designer is looking for handlers.

Notes

  1. The Windows Form Designer adds a Friend WithEvents declaration to the Designer source - so individual event handlers are not added to the Designer source control. See stackoverflow.com/a/44265658/10024425 as per
    Tu deschizi eu inchid in the comments below.
Doug Kimzey
  • 1,019
  • 2
  • 17
  • 32
  • You didn't provide any information. Where *did* you set the event handlers and how? What does `Event handlers are present in the code` mean, where? The designer works fine. If there was such a basic problem millions of developers would have noticed 21 years ago. There wouldn't even be a Visual Studio now if the designer didn't work. WinForms and WebForms and their designers were the most important features of Visual Studio – Panagiotis Kanavos Jun 15 '23 at 11:05
  • Where are all those quotes from? If they're your own comments, don't format them as quote. Unless they help describe the problem, don't include them at all. – Panagiotis Kanavos Jun 15 '23 at 11:06
  • As for where the designer saves *everything*, it's in the `.Designer.cs` partial file, in the `InitializeComponents` method. Everything done on the designer modifies the contents of that method. When you load a form, that method actually gets executed to create all components. When you add an event in the designer, it adds the method to your codebehind file and then registers that method as an event handler in `InitializeComponents`. – Panagiotis Kanavos Jun 15 '23 at 11:10
  • See my edits above. The screenshots show that the event handlers are not shown in the Windows Form Designer yet the code for the event handlers is being called. This is a potential issue for any developers who need to maintain the code in the future. – Doug Kimzey Jun 15 '23 at 11:50
  • Where do you create the event handlers? Naming a method `OnCellValueChanged` doesn't make it an event handler. VB can register event handlers by name, if the method follows the `ComponentName_EventName` convention. `Oxxx` are the virtual methods that raise the events, not the event handlers – Panagiotis Kanavos Jun 15 '23 at 12:01
  • For example, in the [Textbox](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox?view=windowsdesktop-7.0) class, `OnTextAlignChanged` is the method that raises the `TextAlignChanged` event. If you create your own component that inherits from `TextBox` you could override `OnTextAlignChanged` to handle the event in the component itself before raising it to subscribers. For other componets to handle that event though, they'd have to subscribe to `TextBox.TextAlignChanged`, either by calling `AddHanlder` or using `+=` notation – Panagiotis Kanavos Jun 15 '23 at 12:25
  • If you double-clicked on the `TextAlignChanged` of `MyTextBox` in the designer, the designer would generate `MyTextBox_TextAlignChanged` in your code-behind and add `.AddHandler` in `InitializeComponents` in `MyForm.Designer.vb`. You could add a handler yourself without the designer if you used `.AddHandler` or `.TextChanged += MyHandler` *after* the call to `InitializeComponent()`. – Panagiotis Kanavos Jun 15 '23 at 12:28
  • See the additional section on how I am adding event handlers above. There is no AddHandler call added in InitialzeComponents. – Doug Kimzey Jun 15 '23 at 12:38
  • I have used the designer in the past - for years - without this issue. I'm looking for the `AddHandler` and not finding it. When I do a `Find In Files` for the newly added `OnRowsRemoved`, I only find the event handler added by the WinForms Designer. – Doug Kimzey Jun 15 '23 at 12:44
  • You didn't poste any event handlers. `OnCellValueChanged` and any `OnXXX` methods are *not* event handlers – Panagiotis Kanavos Jun 15 '23 at 12:45
  • Scroll to the right in the code above. You will see the `Handles ...`. – Doug Kimzey Jun 15 '23 at 12:46
  • The following may be of interest: https://stackoverflow.com/a/44265658/10024425 – Tu deschizi eu inchid Jun 15 '23 at 13:00
  • The event handler registration is done by the `Handles` clause in the method declaration. – jmcilhinney Jun 15 '23 at 13:27
  • I have added the link from Tu deschizi eu inchid to the text above. Very useful. On the `Handles` clause, my methods all have a `Handles` clause, however, they are not being picked up by the Windows Forms Designer. Are there special rules or locations where the designers must reside to be recognized? – Doug Kimzey Jun 15 '23 at 13:31
  • The issue that you're facing isn't clear. Are you new to VB.NET? If so, the very end of this [post](https://stackoverflow.com/a/76416772/10024425) may contain some useful information. – Tu deschizi eu inchid Jun 15 '23 at 13:52
  • Does this only happen in the current project or does it happen in a new project too? Is there any possibility of upgrading to a newer version of VS? I don't recall having had this issue in VS 2017 but I don't have it installed to test now. If it's all projects and VS is having other issues, maybe a repair of VS is in order. That said, I'm not sure whether VS 2017 is still under support so, if not, that might explain the report option not working. – jmcilhinney Jun 15 '23 at 23:58
  • I am not new to VB.NET. I have used it extensively for more than 15 years. I have also used C#, F# and some Q#. Put simply, event handlers for controls that are present in the code-behind of a form do not appear in the Visual Studio 2017 Windows Form Designer. The events do fire. In the last image in the body of this post, the event handlers are not listed (see lower-right hand corner of image). Event handlers added through the Designer are written to the code-behind but not shown in the Form Designer Events list. This will be confusing for anyone who needs to maintain the code in the future – Doug Kimzey Jun 19 '23 at 12:28
  • The article [Report a problem with the Visual Studio product or installer](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-report-a-problem-with-visual-studio?view=vs-2022) has some instructions how one can report a problem from _Visual Studio Installer_ rather than from _Visual Studio_. However, the first thing they are probably going to ask you to do is to [Repair Visual Studio](https://learn.microsoft.com/en-us/visualstudio/install/repair-visual-studio?view=vs-2022). – Tu deschizi eu inchid Jun 19 '23 at 14:10

0 Answers0