Link to home
Start Free TrialLog in
Avatar of jampost
jampostFlag for United States of America

asked on

WPF code view vs Windows form code view

      I created an identical scenario in both a windows form and a WPF form, but only one of them had a problem; the WPF form. It is all centered around 2 Textboxes and one Label. Here is the very straight forward code.  I need to use the WPF form so I can get a form height that is not limited by my monitor.


      Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

        TextBox13.Text = TextBox17.Text

    End Sub

In case you are wondering (and I wouldn’t blame you) I did enter the code in the code view of the WPF form. If you know what code to enter in XAML to do the job, I would love to see that.

The Label1 click event where    TextBox13     copies from     TextBox17  worked fine in the windows form, but I got an error in the WPF form.

This error:
Handles clause requires a WithEvents variable defined in the containing type or one of it’s base types.
I also noticed that I had to declare the two text boxes in WPF, but did not in the windows form.

I guess the remedy is just a little beyond my experience level.
Avatar of MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

There is no Click event on a label in WPF, use a button control instead of the label, you can always use a template to change the appearance of it.
The event can be wired up either by using 'Handles ' on the event procedure or by coding the 'Click' attribute in XAML to define the evet procedure there (as shown below for Button2), I tend to prefer the latter.
The Name attribute is required on a TextBox if you want to refer to it in code.
<Window x:Class="Window1"
    Title="Window1" Height="300" Width="300">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="70,62,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="70,105,0,0" Name="TextBox2" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="76,146,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        <Button Content="Button Too" Click="Button2_Click"
                Height="23" HorizontalAlignment="Left" Margin="78,184,0,0" Name="Button2" VerticalAlignment="Top" Width="75" />

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        TextBox1.Text = TextBox2.Text
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Do things
    End Sub

Open in new window

Avatar of jampost



  Thank you. I am going to take your code for a test drive right now.
Avatar of jampost



      First of all  --  Thank you for your help.   You are right  --  the VB event in WPF is a pain. It wants a with event variable every time and I am not sure about the syntax. In the XAML  --  I don't see any thing in your XAML code that looks like "Copy from TextBox17 and Past to TextBox13".  Or maybe you were just telling me that it can be done. I have gotten on first base with VB in the Windows forms and I just need to get there with WPF also.  
Avatar of MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial