Handle all of an array's events - arrays

So I have a class which contains a few controls for easy UI design and it has a custom event which is raised whenever the combo box inside the panel is changed:
Public Class BatInputLine
Inherits System.Windows.Forms.Panel
Public Event SelectionChanged As EventHandler
Protected Overridable Sub OnSelectionChanged(ByVal e As EventArgs)
RaiseEvent SelectionChanged(Me, e)
End Sub
Private Sub NameSet(sender As Object, e As EventArgs)
Handles_cboName.SelectedIndexChanged
PlayerName = _playerNames(_cboName.SelectedIndex)
SelectedIndex = _cboName.SelectedIndex
OnSelectionChanged(EventArgs.Empty)
End Sub
An array of these is declared and the user inputs a number according to how many of these they need on screen on a new Form.
ReDim _batInputs(GetNumberOfbatsmen())
I want to call a sub procedure whenever the SelectionChanged event is raised by any of the instances of BatInputLine in _batInputs(). If I try to write a handler e.g sub doSometing(sender as Object, e As EventArgs) Handles _batInputs(0).SelectionChanged I get an error saying that that the _batInput elements need to be declared with a WithEvents modifier, but I don't quite know how to do them.
a) How can I declare this array where all of the indexes get the WithEvents Modifier?
b) How can I assign a sub procedure that is called when these events are raised, which is in the new form?

Made use of the AddHandler keyword, which I didn't know existed.
for i = 0 to _batInputs.Length -1
AddHandler _batInputs(i).SelectionChanged, AddressOf HandleSelectionChangedEvent
next
Private Sub HandleSelectionChangedEvent
'do something
End Sub

Related

Mouse double click windowsformhost on a usercontrol with a spreadsheet

I have a usercontrol that contains a spread and I have this methods
Public Event DOBLECLICK()
Public Sub sp1_CellDoubleClick(sender As Object, e As FarPoint.Win.Spread.CellClickEventArgs)
RaiseEvent DOBLECLICK()
End Sub
and in the mainwindow.xaml in the function MainWindow_Loaded I have:
AddHandler host.sp1.CellDoubleClick, AddressOf host.sp1_CellDoubleClick
my question is, how can i use the event of double click and when clicked i hide the windowsformhost i know that i can hide it whit
WinFormsHost.Visibility = Windows.Visibility.Hidden but how to when clicked the doubleclick on the spread.
finally i got it,
in the user control put this:
Public Event Dobleclick()
Private Sub sp1_CellDoubleClick(sender As Object, e As FarPoint.Win.Spread.CellClickEventArgs) Handles spEmpresas.CellDoubleClick
RaiseEvent Dobleclick()
End Sub
and in the MainWindow.xaml:
Imports nameofyourprogram.Control
Public Class MainWindow
Dim host As New nameofyourprogram.Control
Public Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
AddHandler host.Dobleclick, AddressOf doubleclick
end sub
Sub doubleclick()
msgbox("now you can work whit and event in your mainwindow ")
'after this message i want to hide my windownforhost
WinFormsHost.Visibility = Windows.Visibility.Hidden
end sub
End Class

ShowDialog Form property is empty

I have two of the simplest forms, with a couple of Buttons and a TextBox. I click to open the second form (frmModal) using ShowDialog(), type some text into txtGreeting and press the Yes button. What should happen is that a MessageBox appears confirming the text that was entered into txtGreeting, but it is empty.
I understand that the Form's properties should be accessible until the form goes out of scope, but they disappear straight-away. I can't even read dialog.txtGreeting.Text.
Am I missing anything obvious please?
Public Class frmMain
Private Sub btnModal_Click(sender As Object, e As EventArgs) Handles btnModal.Click
Dim dialog As frmModal
dialog = New frmModal()
Dim result As DialogResult = frmModal.ShowDialog(Me)
If result = Windows.Forms.DialogResult.Yes Then
MessageBox.Show(dialog.Greeting)
End If
'dialog.Dispose()
End Sub
End Class
Public Class frmModal
Public Property Greeting As String
Get
Return txtGreeting.Text
End Get
Set(value As String)
End Set
End Property
Private Sub btnYes_Click(sender As Object, e As EventArgs) Handles btnYes.Click
MessageBox.Show(Greeting)
End Sub
End Class
The Yes button has it's DialogResult property set to Yes.
I've tried moving the dialog-declaration out of the click-event, using an (unnecessary) Dispose(), deliberately assigning the Greeting property in the Yes-click event..
You're effectively instantiating the form twice. By showing frmModal with ShowDialog and then asking for the Greeting value on the instance you created, named 'dialog'.
This should fix it.
Private Sub ModalTestButton_Click(sender As System.Object, e As System.EventArgs) Handles ModalTestButton.Click
Dim dialog As frmModal
dialog = New frmModal()
Dim result As DialogResult = dialog.ShowDialog(Me)
If result = Windows.Forms.DialogResult.Yes Then
MessageBox.Show(dialog.Greeting)
End If
End Sub

Accessing the WPF element in parent window from the user control

I have a custom menu control in the main window of my WPF application. In main window, I have a frame. I want to change the page in this frame based on the selection in the custom menu control. Below is the code I tried.
Private Sub NominationMenuItem_Click(sender As Object, e As RoutedEventArgs)
Dim parentWindow As Window
parentWindow = Application.Current.MainWindow
parentWindow.MainFrame.Navigate(New NominationSearch)
End Sub
I know that I can't directly access "MainFrame" control using the "parentWindow" object. How do I rewrite the last line to get a reference to the existing frame object.
It's not appropriate for UserControl to manipulate it's parent. You should let your MainWindow to listen for NominationMenuItem changes via events and then MainWindow itself do the navigation.
So first you need to define an event in your UserControl:
Public Event NominationMenuItemChanged As RoutedEventHandler
Private Sub NominationMenuItem_Click(sender As Object, e As RoutedEventArgs)
RaiseEvent NominationMenuItemChanged(Me, New RoutedEventArgs())
End Sub
And then listen to this event in your MainWindow:
Public Sub MainWindow()
AddHandler MyUserControl.NominationMenuItemChanged, AddressOf UC_NominationMenuItemChanged
End Sub
Private Sub UC_NominationMenuItemChanged(sender As Object, e As RoutedEventArgs)
MainFrame.Navigate(MyUserControl.NominationSearch)
End Sub

Visual Basic Saving Form info on hide

Im using Visual Basic 2008
I have 2 forms
Main, EditCustomerInfo
Main form contains the following
Public Class Main
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EditCustomerInfo.ShowDialog()
End Sub
EditCustomerInfo contains a text box and the following
Public Class EditCustomerInfo
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not CustomerIDTextBox.Text = "" Then
Me.Close()
Else : Me.Hide()
End If
End Sub
WHAT IT DOES:
So with this code alone when i debug the program it takes me to the main form and allows me to click a button to open the editcustomerinfo form
When im on the editcustomerinfo form i have a textbox and a button. If something is typed in the textbox and the button is clicked then the form hides, if nothing is typed in the textbox when the button is clicked then the form closes.
WHAT I WOULD LIKE IT TO DO:
If something is typed in the textbox i would like the button on the editcustomerinfoform to hide the editcustomerinfoform and also create a button on main form that allows the user to bring the editcustomerinfo form back up with what was typed in the text box.
Suggestions?
Automatic behavior, such as this, always worries me. How do you know when a user has completed their input without a lost focus event. If there are no other controls on the screen, then users won't readily know how to trigger the event. That being said, you can use a timer to delay the screen closing from a KeyPress event.
Public Class EditCustomerInfo
Private WithEvents userInputDelay As Timer = New Timer() With {.Interval = 1000} REM 1 second delay for next user input
Public ReadOnly Property CustomerId As String
Get
Return CustomerIDTextBox.Text
End Get
End Property
Private Sub CustomerIDTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CustomerIDTextBox.KeyPress
userInputDelay.Enabled = False
REM Reset the timer
userInputDelay.Enabled = True
End Sub
Private Sub userInputDelay_Tick(sender As Object, e As KeyPressEventArgs) Handles userInputDelay.Tick
If Not CustomerIDTextBox.Text = "" Then
Me.Close()
Else : Me.Hide()
End If
End Sub
End Class
Add a button (Button2) to your Main. The code below will hide Button1 when the EditCustomerInfo.Textbox1.Text value is null/blank/white space. Button2's visibility is always the inverse of Button1.
Private EditCustomerInfoInstance As New EditCustomerInfo
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EditCustomerInfoInstance.ShowDialog()
Button1.Visible = String.IsNullOrWhiteSpace(EditCustomerInfoInstance.CustomerId)
End Sub
Private Sub Button1_VisibleChanged(sender As Object, e As EventArgs) Handles Button1.VisibleChanged
Button2.Visible = Not Button1.Visible
End Sub

MVVM Handle all un handled keystrokes on ViewModel

I don't know if this is a good way to work but i need to handle all unhandled keystrokes on my ViewModel so my idea was to use a Behavior on my ShellView that would relay all unhandled keystrokes to the ViewModel..
But the problem is how do i get all unhandled key presses?
Here is my first try to just catch them
Public Class ForwardKeyBehavior
Inherits Behavior(Of DependencyObject)
Protected Overrides Sub OnAttached()
Keyboard.AddKeyDownHandler(Me.AssociatedObject, AddressOf OnKeyPressed)
Keyboard.AddPreviewKeyDownHandler(Me.AssociatedObject, AddressOf OnPreviewKeyPressed)
MyBase.OnAttached()
End Sub
Protected Overrides Sub OnDetaching()
Keyboard.RemoveKeyDownHandler(Me.AssociatedObject, AddressOf OnKeyPressed)
MyBase.OnDetaching()
End Sub
Private Sub OnPreviewKeyPressed(ByVal sender As Object, ByVal e As KeyEventArgs)
End Sub
Private Sub OnKeyPressed(ByVal sender As Object, ByVal e As KeyEventArgs)
If (Not e.Handled) Then
Trace.Write(e.Key.ToString())
End If
End Sub
End Class
But it seems that e.Handled always is false so what am i missing even if i press a key in a textbox?
You set e.Handled = True to tell the program that the event has been handled and to stop executing any other functions that are registered to that event.
For example, if you hook up two methods to the KeyPressed event, and the first one sets e.Handled = True, then the 2nd event will never get executed.
I am guessing that all you really need to do is make sure your UnhandledKeyPressedEvent comes last in the event sequence, and that any other KeyPressed events set e.Handled = True to prevent the UnhandledKeyPressedEvent from executing.
Check out MSDN
Pay attention to "The Concept of Handled" section, especially the handledEventsToo part.

Resources