make enter button of textbox - wpf

i have textbox multi way to fill it
i can fill it from keyboard normally and the other way by clicking button i created on forum something like calculator
when i click button 1 the textbox fill by number 1 and so
and i have button name and work should be like enter key
i have event keydown to send the value i fill it on the text box and work fine
but i need the button that called enter work as the event keydown of the textbox
in short word i need to use touchscreen to enter number to textbox and button to send the value of the textbox to my work

If you want to execute the same code at two different occasions you should put your code in a separate method and then call that at will.
By dividing your code like this you improve both readability and debuggability, and you make your code easier to understand and update.
Here's a brief example:
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) Handles TextBox1.KeyDown
If e.Key = Key.Enter Then
UpdateStatus("Enter was pressed.") 'Call custom method.
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
UpdateStatus("Button1 was pressed. Hello World!") 'Call custom method.
End Sub
Private Sub UpdateStatus(ByVal Text As String) 'Custom method declaration.
TextBox1.Text = Text
'In here is where you'd put your code.
End Sub

Related

Ctrl + Click on any Control on form run different function

How can this be achieved:
I want to be able to run specific function only by CTRL + left click on any form element, just left clicking that element should retain original handler and function.
Equivalent to:
If My.Computer.Keyboard.CtrlKeyDown Then
...
Else
...
End If
but in my case i want to send element.Name to another function.
Is there a way to implement it on whole form (wpf form) so that if I ctrl+click any control on form i get a messagebox showing me x:name of that control
You could do something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Control.ModifierKeys = Keys.Control Then
MessageBox.Show("Do Ctrl-Click logic")
Else
MessageBox.Show("Do Click logic")
End If
End Sub

host WPF windows in another WPF window

here is my problem: i have created a main window called mainWindow in XAML (vb.net) and inside i have 2 buttons (valid and stop) and a grid in the center.
I have two others little windows (valid window and stop window) written in XAML (vb.net) which have buttons, textbox...
I want, when i click on valid button or stop button, display the valid window or the stop window inside the grid of my mainWindow, so i have this code in my mainWindow.vb:
enter code here
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
valid_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
Private Sub stop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles stop.Click
Dim content As Object = stop_.Content
stop_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
So when i click on button valid in my mainWindow, it's ok it displays the valid window in my grid.
First problem: then when i click on button stop in my mainWindow, the stop window is placed just above the valid window, it is not nice, is there a way to clear the grid before display this second window?
And finally, the biggest problem: i need to click many times on valid button or stop button but when i click the second time i have a null reference exception: Me.Grid.Children.Add(content) content is null after the first call so i am only able to click one time on my button.
How can i fix it in order to click many times on my buttons please?
I give you thanks.
Yet again, someone writes some invalid code and then says why isn't this code working? If you look at your code, you should see something:
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
Me.Grid.Children.Add(content)
End Sub
I'm guessing that you have a private variable for your Valid Window as you call it: valid. On this line, what are you doing to your variable?:
valid_.Content = Nothing
That's right! You're setting its Content property to null. Therefore, I'm not really sure why you were surprised that it was null after the first attempt.
How can i fix it in order to click many times on my buttons please?
Try removing the line that sets the Content property to null.
UPDATE >>>
Your problem is really caused by the fact that you cannot display any UI element in two places at once. Your whole idea of copying the Window.Content to your DataGrid is entirely wrong, but in the name of brevity, your fix is simply to move your content back to the Window, rather than setting it to null each time. Try something like this:
Private Sub valid_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles valid.Click
If Me.Grid.Children.Count > 0 Then 'Put content back where it belongs
Dim oldContent = Me.Grid.Children(0)
stop.Content = oldContent
Me.Grid.Children.Remove(oldContent)
EndIf
Dim content As Object = valid_.Content /*Classe valid_ (a window in xaml)*/
valid_.Content = Nothing
Me.Grid.Children.Add(content)
End Sub
Please forgive any code mistakes, as I don't write VB.NET. Also, you'll need to update your other method likewise.

How to Close a Window Containing a Frame, NavigationService and Timer

In my project I've got a MainWindow that opens up a second Window. Inside the second Window there is a Frame and I start a navigationservice inside the Frame. Also in the second Window I've got a KeyDown method that calls Me.Close when the user presses the Escape key. Anyway, when the second Window closes a System.Windows.Threading.DispatcherTimer() inside one of the pages in the navigation service doesn't end. Any ideas on how can I close the second Window and terminate the DispatcherTimer inside the navigationservice?
Thanks
Mike
p.s. I can supply source code if anyone wants to look at what I've got...
(Hey EkoostikMartin - this is a follow up to your comments..)
So I've made some progress on this. I've add:
AddHandler Me.KeyDown, AddressOf Page_KeyDown
AddHandler Me.PreviewKeyDown, AddressOf Page_PreviewKeyDown
to the Page that has the timer. And inside the page I've defined both methods like:
Private Sub Page_KeyDown(sender As Object, e As KeyEventArgs)
If e.Key = Key.Escape Then
dTimer.Stop()
MessageBox.Show("Exit Page")
End If
End Sub
Private Sub Page_PreviewKeyDown(sender As Object, e As KeyEventArgs)
If e.Key = Key.Escape Then
dTimer.Stop()
MessageBox.Show("Exit Page")
End If
End Sub
And the second Window has this:
Private Sub Window_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs)
'Escape Key Exits Program
If e.Key = Key.Escape Then
Me.Close()
End If
End Sub
So when I'm in the navigation service and navigated to the page with the Timer and I press "Esc" I get the message "Exit Page" and then the Window closes. This is good!
(I don't think I need both the KeyDown and PreviewKeyDown. When I press "Esc" I'm actually getting two "Exit Page" pop-ups)
There is a problem though: It seems like the Page doesn't get the KeyDown events unless I move the focus to a textbox or combobox and if I don't do this pressing the "Esc" key calls the second Window's Window_KeyDown and not the Page's KeyDown event which means the timer on the page doesn't get stopped even after the second Window is closed. Does anyone know a way to get the page focus when the page loads so that I can get the KeyDown event without manually changing the focus to a control on the Page?
Thanks!
Ok - I finally resolved this situation by way of a work around. In my second Window I've created a list of type DispatcherTimer:
Public clndTimer As New List(Of System.Windows.Threading.DispatcherTimer)
I can access this list from a Page that is inside my Navigation service. Here's the code in the Page:
Dim dTimer As New DispatcherTimer()
dTimer.Start()
Dim wSecondWindow As New SecondWindow
wSecondWindow = Window.GetWindow(Me)
If wSecondWindow IsNot Nothing Then
wSecondWindow.clndTimer.Add(dTimer)
End If
And then I capture the key event in the second Window. This is the method in the second Window:
Private Sub Window_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs)
'Escape Key Exits Program
If e.Key = Key.Escape Then
For Each dt In clndTimer
dt.Stop()
Next
Me.Close()
End If
End Sub
Doing it this way I don't need either the Page_KeyDown or PreviewKeyDown methods in my Page which is good because they were behaving unreliably. (see answer above)
So what do you think? I'm not totally sure about the way I get the Second window or how I check it for null in the Page but otherwise this seems to make sense.
Thanks!

WinForm validation: How do you tell if the form contains a control that did not pass validation?

I have a composite usercontrol that contains a dropdown "Country" control and a checkbox. If the checkbox is selected, I want to display a validation icon with a tooltip msg that informs the user that a country selection is required.
If the user tries to save the changes, I want to check the entire form, including this composite usercontrol, for errors and, if found, cancel the save.
I expected that, in the form, I would be able to call the Me.Validate function and that the function would recursively check for any controls on the form at any level and return a value indicating whether there are errors or not. Instead, the function appears to fire the validation event for all the controls (I guess this is OK) and UNCONDITIONALLY return TRUE.
Calling the Validate method on the composite userControl also behaves the same.
Do I have to write my own recursive function to check or errors on this form?
I included my code in order for people to offer general suggestions, too.
Private Sub ComboOutOfCountry_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboOutOfCountry.Validating
ValidateComboOutOfCountry()
End Sub
Private Sub ValidateComboOutOfCountry()
If CheckOutOfCountry.Checked AndAlso _
(ComboOutOfCountry.Value Is Nothing OrElse ComboOutOfCountry.Value = DBCodeConstants.Omited) Then
ErrorProvider1.SetError(ComboOutOfCountry, "Country is required when ""Out of Country"" is selected")
Else
ErrorProvider1.SetError(ComboOutOfCountry, "")
End If
End Sub
Private Sub CheckOutOfCountry_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckOutOfCountry.CheckedChanged
If Not CheckOutOfCountry.Checked Then
ErrorProvider1.SetError(ComboOutOfCountry, "")
End If
End Sub
Private Sub ComboOutOfCountry_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboOutOfCountry.ValueChanged
ValidateComboOutOfCountry() 'Clear error icon immediately if they selected a country
End Sub
You can easily subclass the ErrorProvider to achieve this - see http://dotnetslackers.com/Community/blogs/dsmyth/archive/2007/10/12/custom-error-provider.aspx for an example.

WPF: Detect the shift button

This code should fire when is pressed, but not +. What change do I need to make it work correctly?
Private Sub cmdDeleteRow_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
If e.Key = Input.Key.Tab Then
'DO stuff
End If
End Sub
Hmh, yor question title doesn't match the content (no reference to the shift key)!
Are you trying to avoid "Do stuff" when Shift+Tab is pressed? If so you need to check the modifier keys, whether Shift is pressed or not. I don't know the exact code for vb.net (you can easily google for it), but in c# there's a Keyboard.Modifiers property that can be accessed...

Resources