Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When converting a WPF form, the Handles command in the code behind file is skipped. #1141

Open
mjohansson1 opened this issue Oct 17, 2024 · 6 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@mjohansson1
Copy link

VB.Net input code

    Private Sub TextBoxConnectionString_ValueChanged(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles TextBoxTab3Item.TextChanged, TextBoxTab3Detail.TextChanged
        Changes.Tab3 = True
    End Sub

Erroneous output

        private void TextBoxConnectionString_ValueChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            Changes.Tab3 = true;
        }

Expected output

        private void TextBoxConnectionString_ValueChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            Changes.Tab3 = true;
        }
...
Then, elsewhere in the file:
this.TextBoxTab3Item.TextChanged+= FormEditConfig_Closing;

Details

When converting a WPF form, the Handles command in the code behind file is skipped meaning that no events ever fire.

@mjohansson1 mjohansson1 added the VB -> C# Specific to VB -> C# conversion label Oct 17, 2024
@mjohansson1
Copy link
Author

I checked around and none of the VB->C# converters are doing this. They all just leave it for you to do manually. Manually does not work for me with a million+ lines of code. I just asked ChatGPT to please do this for me and it did. So, workaround is working.

@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Oct 27, 2024

Thanks for the report. There is a whole bunch of code in the converter about dealing with Handles clauses in various different situations. While it'll be hard to perfect the code and I don't have time to take it on right now, you may find that running the converter in debug mode looking at the state around here allows you to tweak things to work for you situation and inform a fuller bugfix:

@nick2893
Copy link

I will add one more possible solution... not promising I'll have time to dive into the code. If I begin with:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
    <Grid>
        <Button Name="btn1"  Content="Button" HorizontalAlignment="Left" Margin="76,76,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>
Class MainWindow

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles btn1.Click
        MessageBox.Show("Hi!")
    End Sub

End Class

The XAML doesn't get changed, and the code-behind converts to:

using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow
    {

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hi!");
        }

    }
}

From my memory ... the VB above used to be the default generated code in some older version of Visual Studio - it would add "handles" rather than dealing with it in XAML. What VB does today is the same as what C# does:

<Button Name="btn1" Content="Button" HorizontalAlignment="Left" Margin="76,76,0,0" VerticalAlignment="Top" Click="Button_Click"/>

From what I'm seeing so far, this is causing most (all?) of my conversion failures with the "handles" code.

@GrahamTheCoder
Copy link
Member

Thanks for the extra info. If you do end up diving into the code, just ping any questions on here and I'll do my best to assist

@mjohansson1
Copy link
Author

mjohansson1 commented Feb 26, 2025 via email

@GrahamTheCoder
Copy link
Member

@mjohansson1 This thread is about WPF. The code I mentioned goes to significant effort to try to fix up winforms cases already. It's a difficult problem to fully solve, so if you're seeing a remaining winforms problem, please open an issue or add to an existing closely related one with your repro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

3 participants