Mouse double click windowsformhost on a usercontrol with a spreadsheet - wpf

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

Related

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

Handle all of an array's events

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

WPF Running a command on a windows form

I have a WPF Project to which i've added a Windows Form.
The windows form has a method runme(). But I'd like to call it from the WPF form.
Currently I've used
Dim wpfForm as new wpfForm
wpfForm.Show()
to display the the WPF form from a Windows form.
I'm not sure how to send a command back though.
I've tried:
Me.parent.runme()
But this gives me errors
The only way that I can think of to do what you are wanting is to create a Custom Constructor for your Winform and pass the reference to the Wpf Window to it. There appears to be no easy way to assign a WPF Window as an Owner/Parent of a Winforms Window.
Winforms Form
Public Class Form1
Dim myParent As System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(parent As System.Windows.Window)
myParent = parent
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CType(myParent, MainWindow).runMe()
End Sub
End Class
Wpf Form
Class MainWindow
Dim winForm As Form1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
winForm = New Form1(Me)
winForm.Show()
End Sub
Public Sub runMe()
Me.Background = Brushes.Red
End Sub
End Class
Eventually what worked was to create an interface as suggested by Cyborgx37
Interface ILogOut
Sub DoIt()
End Interface
Class WinFormWindow : Implements ILogOut
Public Sub DoIt() Implements ILogOut.DoIt
MsgBox("Message Received!")
End Sub
End Class
You can then use the ILogOut interface to send a message to the Windows forms window from a WPF window

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