Visual Basic sender not coming back as control - winforms

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.

Related

Winform dialog show modal form wait for Dialogresult

I need to use "Show()" method for dialog winform using Ironpython
and i need to get result of button pressing and use it in another part of program
how can i wait for pressing OK in modal "Show()" dialog?
mform = Form3()
mform.Show()
# How to wait for button OK
Thx
Use ShowDialog() instead of Show().
Then check "if (mform.DialogResult == DialogResult.OK)"
Suppose you have a main form called Form1 and a secondary form called AddForm which presents some result when the [OK] button is pressed.
The main form then can subscribe to the FormClosed event of the secondary form and pull the result if the user clicked on the [OK] button.
This can all happen in the subroutine where the secondary form is shown
Private Sub Button1.Click(sender as Object, e as EventArgs) Handles Button1.Click
Dim dlg as New AddForm
AddHandler dlg.FormClosed, _
Sub(e,ev) TextBox1.Text = If(dlg.DialogResult = DialogResult.OK, dlg.Result, String.Empty)
dlg.Show()
End Sub
And in the secondary form make sure there is a property called Result which contains the results
The [OK] button has a handler like so
Private Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
Result = ...
DialogResult = DialogResult.OK
Me.Close()
End Sub
and the [Cancel] button has a handler as so
Private Sub Button2_Click(sender as Object, e as EventArgs) Handles Button2.Click
DialogResult = DialogResult.Cancel
Me.Close()
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.

Delaying opening of sub-menus in a menustrip

Our environment: Visual Studio 2010, c#, .net 4 client profile.
We have a Winforms application that contains a menustrip in its main form. The items of the menustrip contain both an image (64x64) and text. The main form also has a TabControl which contains 5 tabs. In the OnLoad() method of the main form, we hide the TabControl headers so that they are not visible and therefore not clickable. Instead, when the user clicks on an item in the menustrip, we switch the active tab.
However, our menus have many sub-menu items, and since we use the main menustrip to select the active tab, we would like the sub-menu items to appear only after the user clicks the menu item for a period of time, not instantaneously. Otherwise, whenever the user changes his/her active view (by selecting a tabPage), the sub-menus appear on the screen since he/she clicked a menustrip item that contains sub menus.
Is this possible?
I don't completely understand the rationale, but you can delay the display of a submenu using the MouseDown handler and sleep function, like this:
Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.MouseDown
System.Threading.Thread.Sleep(2000) ' wait two seconds
End Sub
======================
(Edit: Added second solution)
You can do this with a timer control and ShowDropDown/HideDropDown:
Private Sub FileToolStripMenuItem_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FileToolStripMenuItem.MouseDown
' show tab here'
FileToolStripMenuItem.HideDropDown()
Timer1.Interval = 500
Timer1.Start()
End Sub
Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click
FileToolStripMenuItem.HideDropDown()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
FileToolStripMenuItem.ShowDropDown()
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.

Windows Forms custom control: focus and cursor keys without UIPermissionWindow.AllWindows

I want to write a custom control (a text editor) for Windows Forms, which should include the following functionality:
Gets the keyboard focus, when you click on it with the mouse
Sees all keyboard input (including cursor keys), when it has the focus,
Can run in a semi-trusted environment, with UIPermissionWindow.SafeTopLevelWindows (i.e. it shouldn't require UIPermissionWindow.AllWindows)
Is there any example of doing this?
Some of the methods which I might want to use, like Control.Focus() and Control.InInputKey(), require UIPermissionWindow.AllWindows.
Is there any other way to get/implement the functionality, without using these methods?
The built-in TextBox control has this functionality (gets the focus and handles cursor keys).
Public Class UserControl1
Inherits TextBox
Private Sub UserControl1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
End Sub
Private Sub UserControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Debug.WriteLine("downed")
Debug.WriteLine(e.KeyValue)
Debug.WriteLine(e.KeyCode)
End Sub
Private Sub UserControl1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Debug.WriteLine("pressed")
Debug.WriteLine(e.KeyChar)
End Sub
End Class

Resources