question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C#: Event handler assignments are placed above InitializeComponent

See original GitHub issue

Repro instructions

  1. Create a new VB WPF Application (.NET Framework) project.
  2. Fill MainWindow.xaml as follows:
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <Button x:Name="btn" />
</Window>
  1. Fill MainWindow.xaml.vb as follows:
Class MainWindow
    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        MsgBox("Window loaded")
    End Sub

    Private Sub btn_Click(sender As Object, e As RoutedEventArgs) Handles btn.Click
        MsgBox("Button clicked")
    End Sub
End Class
  1. Run the program and note that everythings works as expected: The first message box is shown immediately, and the second one upon clicking the (full-size) button.
  2. Convert the project to C# using the Visual Studio Extension.
  3. Fix the namespace in the XAML file (depends on your project name, in my case it was x:Class="MainWindow" -> x:Class="WpfApp1.MainWindow").
  4. Run the program.

Erroneous output

Neither the window’s nor the button’s event handler gets called, because (a) the XAML file is left unmodified and (b) the Handles clauses from the VB code are gone without replacement. This is what MainWindow.xaml.cs looks like:

using System.Windows;
using Microsoft.VisualBasic;

namespace WpfApp1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Interaction.MsgBox("Window loaded");
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Interaction.MsgBox("Button clicked");
        }
    }
}

Expected output

There are multiple ways to fix this:

Solution 1: Add the event handler names to the XAML file. This would be the most idiomatic solution, but requires parsing and modifying the XAML file, which is probably a lot of work.

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800"
        Loaded="MainWindow_Loaded">
    <Button x:Name="btn" Click="btn_Click" />
</Window>

Solution 2: Add the event handlers in code in the constructor.

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += MainWindow_Loaded;
            btn.Click += btn_Click;
        }

Solution 3: Inform the user that there is still work that needs to be done:

        // TODO: Attach the event handler to its event. VB version was: "Handles Me.Loaded"
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Interaction.MsgBox("Window loaded");
        }

        // TODO: Attach the event handler to its event. VB version was: "Handles btn.Click"
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Interaction.MsgBox("Button clicked");
        }

Details

  • Product in use: VS extension
  • Version in use: 9.0.4.0

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
GrahamTheCodercommented, Dec 16, 2022

I’ve found that if people use the WPF designer to add events, they get added in the xaml even in VB, so people will only face this issue if manually adding Handles statements. So yeah if it’s slightly rarer that’s even less reason to dabble with mutating xaml.

It looks like the first part of the reason the event hookups aren’t added is to do with partial classes that WPF generates in the obj folder to represent the xaml. This gets selected as the best place to add the initialization. I’ll see if I can stop that happening. (The files have the .g.i.vb and .g.vb extensions if you’re interested)

0reactions
GrahamTheCodercommented, Mar 16, 2023

Thanks, I’ve reopened this and will try to take another look when I get time

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visual Studio vs. #Develop - Default event handlers
Visual Studio and SharpDevelop do not both set up delegates to handle events in the same way. The way they are set up...
Read more >
Thread: Form events like Load , Initialize , Activate etc
The InitializeComponent method is where the components you added in the designer are initialised. It is called from the form constructor, so it ......
Read more >
[RESOLVED] What happens before Form Load Event?
vb file, you'll see that the InitializeComponent method sets up all your controls and any properties you have assigned in the "design mode"....
Read more >
Updated Modern Code Generation for WinForm's ...
When you design a WinForms Form, it gets generated into a method called InitializeComponent. When you reopen that Form, it gets recreated by ......
Read more >
How to subscribe to and unsubscribe from events - C# ...
To subscribe to events by using the Visual Studio IDE · If you cannot see the Properties window, in Design view, right-click the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found