Delaying opening of sub-menus in a menustrip - winforms

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

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

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.

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

Showing and closing multiple windows

Say I have two WPF Windows. WindowA and WindowB. Each Window has two buttons. A Open button which will SHOW the other Window and a Close button that will Close the other Window (vice versa).
Example WindowA I have the following click event and sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
WindowB.Show()
CloseWeAidWindow()
End Sub
Public Sub CloseWeAidWindow()
Dim CloseWindow = Window.GetWindow(Me)
If CloseWindow IsNot Nothing Then
CloseWindow.Close()
GC.Collect()
End If
End Sub
However it closes the entire application and not just WindowA. WindowB opens for about .5 seconds.
How can I open another window and close the current window?
Sounds like your Application.ShutdownMode is set to OnMainWindowClose. If you want to the app to remain open as long as any window is visible try OnLastWindowClose.

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