Handling event's doesn't work from code in WPF - wpf

I am working on a project and I have encountered problem I can't seem to resolve myself. I have simplified code as much as possible and started a new small project to see if this is not caused by any interference with the rest of bigger project.
This is what I have got:
XAML:
<Window Loaded="Window_Loaded" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MainWindow">
<Label MouseDown="Label1_MouseDown" Content="y u no work?!" Name="Label1"/>
</Window>
CODE:
Class MainWindow
Private Sub Label1_MouseDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)
MsgBox("md1")
End Sub
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs)
' Doesn't work
Label1.AddHandler(Mouse.MouseDownEvent, Sub() MsgBox("md2"))
' Doesn't work neither
Mouse.AddMouseDownHandler(Label1, Sub() MsgBox("md3"))
End Sub
End Class
"md1" pops up, as expected. "md2" and "md3" doesn't. Where do you think I made a mistake?

The following line is wrong and throws a silent "Handler type is mismatched" exception.
Label1.AddHandler(Mouse.MouseDownEvent, Sub() MsgBox("md2"))
As a result, the next line that is actually perfectly good does not run.
So the following works fine.
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Mouse.AddMouseDownHandler(Label1, Sub() MsgBox("md2"))
Mouse.AddMouseDownHandler(Label1, Sub() MsgBox("md3"))
End Sub
I personally would have used the following to add the handlers but I am not sure if there is any advantage or difference other than, in my opinion, improved readability.
AddHandler Label1.MouseDown, Sub() MsgBox("md4")
I hope this is helpful,
Sam.

Related

Vb.NET instruction execution not triggered by event

I have a very basic question on vb.NET, so basic that I didn't find any answer elsewhere.
I've written with VB express a code that is a simple form proposing various choices through checkboxes.
These choices have to be registered in an array, which I convert later into a textfile to be Perl-processed.
I'm searching a way to zeroise this big array with loops before use, but in fact I don't know how to execute instructions which wouldn't be triggered by events in my main form.
The frame looks like that :
Public Class Form1
'Variables declaration...
'Several boxes like that :
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
'Instructions...
End Sub
End Class
To launch this application, I simply click on the associated .exe file.
Basically, my question is :
is there a way to execute instructions that wouldn't be launched by an user event, but immediatly launched when the main Form1 is shown ?
Sorry for being retarded, and thank you in advance if you can help me.
The Form.Load event for instance gets launched as soon as the form is about to be shown :)
The Form will raise a Shown event when it is shown simply handle that:
Private Sub Form_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Shown
'Instructions...
End Sub
It can be better to do initialisation in the constructor right after the Winforms designer adds the comment: 'Add any initialization after the InitializeComponent() call

WPF forward/back button

I would like to make back and forward button in my app in WPF (Visual Basic 2010 Express). I have six tabitems and this two buttons will be used to navigate between tabitems.
I have used following code, but it doesn't work. what am I doing wrong?
Public ReadOnly Property CanGoForward As Boolean
Get
CanGoForward = True
End Get
End Property
Private Sub forward_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles forward.Click
TabIdentiPodatki.goforward()
End Sub
Private Sub TabIdentiPodatki_CanGoForwardChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles forward.CanGoForwardChanged
forward.IsEnabled = TabIdentiPodatki.cangoforward
End Sub

Selected Index Change on WPF combobox?

I'm converting an app of mine from WinForms to WPF. I came across this problem where in WinForms if I CLEAR a combobox, that does not fire combobox.selectedindexchange. It only fires when the user actually changes the index.
That's the functionality I need. However in WPF I can only find combobox.SelectionChanged. This unfortunately fires on both index change and clearing a combobox (any change).
In WPF how can I only trigger an event when the user changes the index? I'm assuming there's a solution I'm missing that's like the WinForms solution. I'm trying not to play games with global variables and having to track what was previously selected....that's a mess.
Mouseleave is also a mess.
I would greatly appreciate any help!
In your case you can implement in SelectionChanged event:
Private Sub OnSelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim combo = TryCast(sender, ComboBox)
If combo.SelectedItem IsNot Nothing Then
'do something
End If
End Sub
You can remove event handler, invoke clear method and add again event handler.
Example code:
<StackPanel>
<ComboBox Name="myCB"
SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem>test1</ComboBoxItem>
<ComboBoxItem>test2</ComboBoxItem>
<ComboBoxItem>test3</ComboBoxItem>
</ComboBox>
<Button Content="Clear" Click="Button_Click" />
</StackPanel>
MainWindow class:
Class MainWindow
Private Sub ComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Console.WriteLine("Selected index: {0}", CType(sender, ComboBox).SelectedIndex)
End Sub
Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
RemoveHandler Me.myCB.SelectionChanged, AddressOf ComboBox_SelectionChanged
Me.myCB.Items.Clear()
AddHandler Me.myCB.SelectionChanged, AddressOf ComboBox_SelectionChanged
End Sub
End Class

trying to learn animation in WPF with CompositionTarget.Rendering

I am trying to figure out how to animate/move elements in WPF with VB.NET. It was suggested to me to try using CompositionTarget.Rendering but I am having difficulty getting started. Below is my code and below that is the error message I am getting. Could someone please tell me where I am going going wrong.
Code:
Imports System.ComponentModel
Class MainWindow
Dim flake0 As New flake
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
canvas1.DataContext = flake0
AddHandler CompositionTarget.Rendering, AddressOf compo
End Sub
Private Sub compo(ByVal sender As Object, ByVal e As EventArgs)
flake0.move()
End Sub
End Class
Error:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types
I had another method that accidentally got "handles CompositionTarget.Rendering" tagged to the end of it.

WPF form to form update

I am trying to update a label from one form to another. the code is compiling fine but not updating?
Class MainWindow
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim frm As New Window1
frm.Show()
End Sub
End Class
second form:
Public Class Window1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
My.Windows.MainWindow.Label1.Content = "dsfdsfsdf"
My.Windows.MainWindow.Label1.UpdateLayout()
End Sub
End Class
it doesn't update the main form's label... hope that makes it clearer
So here's the code that you actually need:
Public Class Window1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
DirectCast(Application.Current.MainWindow, MainWindow).Label1.Content = "test"
End Sub
End Class
I'm not sure what the My.Windows collection is, but the Application.MainWindow gives you a reference to the window that is set as the start up object in the project properties (or the one you set in your app.xaml.cs file). Previously you were probably getting a reference to a different instance of the Window1 class, hence it was running and not throwing an exceptions, but since it wasn't the actual visible instance of the window then you didn't see any changes.

Resources