Ctrl + Click on any Control on form run different function - wpf

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

Related

make enter button of textbox

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

Visual Basic sender not coming back as control

I have a windows application with 2 forms.
frmMain has a button (btnEditAgent) on it that opens frmEditAgent:
Public Sub btnEditAgent_Click(sender As Object, e As EventArgs) Handles btnEditAgent.Click
frmAgentEdit.ShowDialog()
End Sub
Then on frmEditAgent load:
Private Sub frmAgentEdit_Load(ByVal sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(sender.Name, vbOKOnly, "verify")
End Sub
The sender comes back as "frmEditAgent" and not "btnEditAgent"
I do not understand why this is happening. In order for the rest of my code to work I need to know which button opened frmEditAgent. Why is sender referring to the same form?
The sender of the Load event is the Form that was just loaded. The semantics of sender wouldn't make sense otherwise.
Think of it this way: the button opens the form, but the form does its own loading, and so calls its own Load event with itself as the sender.
If you want to know which button opened the form, then you can add an instance variable to your dialog form's class and then just set that variable in btnEditAgent_Click.
frmAgentEdit can then just use that instance variable to know which button caused it to be opened.

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.

Can I assign a unique ID to a button that I can get when it is clicked?

I have a data-grid that list items from my table. I have added a column that has a button for each row that if clicked I would like the user to be able to edit that item.
As I am creating the table is their a way to assign the ID of that item to the button so that I can reference that in the button click and then query the database and retrieve the record I need to be edited?
I am using Visual Studio 2012 with VB.Net 4.0
So you should be able to do something like this unless I am missing something. Assigning the ID would happen where you created the table row and the button and probably not in page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
//create your button then assign the id
myButton.ID = "123"
// assign a generic event handler for all the buttons in the table.
AddHandler myButton.Click, AddressOf myButtons_Clicked
End Sub
Protected Sub myButtons_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Button thebtn = CType(sender, Button)
string btnID = thebtn.ID
// pass of the ID to whatever method is doing your processing
End Sub

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.

Resources