.NET Framework - WinForm Dynamic Context Menu
Asked By jp2msf
08-Oct-08 01:41 PM

I've got one Context Menu named mi_EasterEggs with three (3) menu items:
* mi_FontArial
* mi_FontCourier
* mi_RawData
All menu items have their Visible properties set to False when the form loads.
This context menu is included in one (1) ListView control and one (1)
TextBox control.
When one of these controls is right-clicked (to bring up the Context Menu),
I want to make menu items viisble depending on what item was right-clicked.
I tried this code, but the sender is always the mi_EasterEggs Menu Item:
private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
if (sender.Equals(ListView1) == true) {
mi_RawView.Visible = (0 < ListView1.Items.Count);
} else {
mi_FontArial.Visible = sender.Equals(TextBox1);
mi_FontCourier.Visible = sender.Equals(TextBox1);
mi_RawView.Visible = false;
}
}
I searched google and found a suggestion by Plausibly Damp where he
mentioned usingt the .SourceControl field, so I modified my listing to this:
private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
if (mi_EasterEggs.SourceControl == ListView1) {
mi_RawView.Visible = (0 < ListView1.Items.Count);
} else {
mi_FontArial.Visible = (mi_EasterEggs.SourceControl == TextBox1);
mi_FontCourier.Visible = (mi_EasterEggs.SourceControl == TextBox1);
mi_RawView.Visible = false;
}
}
It still doesn't work, though.
What am I doing wrong?
Also, I'd like to still have the default Context Menu for the Controls, but
this is a bonus and is not necessary.
First thing is first, right?
ListView1.Items.Count
(1)
EasterEggs.SourceControl
(1)
CancelEventArgs
(1)
FontCourier.Visible
(1)
FontArial.Visible
(1)
RawView.Visible
(1)
ListView
(1)
TextBox
(1)
CiaranODonnel replied...
From memory, I think the contect menu wont open unless it has something to
display, change the logic so at form load the items are visible, then hide
the non-required ones when opening the menu rather than the other way round.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
the following code to filter the list: private void Filter(string value) { for (int i = listView1.Items.Count - 1; -1 < i; i- -) { if (listView1.Items[i].SubItems[1].Text.StartsWith(value) = = false) { listView1 restore the old contents of the listbox. I have tried using: ListViewItem[] listarray = new ListViewItem[ListView1.Items.Count]; What happens though is that I get an error explaining that the items cannot exist info he is looking for. C# Discussions ListViewItem (1) ListView (1) StartsWith (1) Filter (1) ListView1.Items.Count (1) Text.StartsWith (1) Remove (1) Clone (1) You cannot have the same ListViewItem
Text = "3" Danke für Tipps und Links. Grüße Oliver VB.NET - German Discussions System.ComponentModel.CancelEventArgs (1) Microsoft.DirectX.Vector3.Length (1) NumberStyles.AllowDecimalPoint (1) Windows.Forms.ErrorProvider (1) Microsoft.DirectX Sub Private Sub Textbox1_Validating _ (ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs _ ) Handles Textbox1.Validating Dim TB As TextBox = DirectCast(sender, TextBox) Dim Result As Double 20 Textboxen Private Sub txtHoehe_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtHoehe.Validating Dim TB As TextBox = DirectCast(sender, TextBox) Dim ep As New Windows kommen. Private Sub Textbox1_Validating _ (ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs _ ) Handles Textbox1.Validating Dim TB As TextBox = DirectCast(sender, TextBox) Dim Result As Double Grüße Oliver - -- - Private Sub txtHoehe_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtHoehe.Validating Dim TB As TextBox = DirectCast(sender, TextBox) Dim ep As New Windows
Public Class usrSpin Public Event AddValidate(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) . . .normaler Code. . . Private Sub mySpin_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mySpin.Validating RaiseEvent AddValidate(sender, e) End Sub End Class Beispiel für Aufruf und und an) Private Sub myAddValidating( ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs, Optional ByVal _IsKeyF2 As Boolean = false) Handles _ myWlbPh0.AddValidate, myWlbPh1.AddValidate For a as End Sub Private Sub myAddValidating( ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs, Optional ByVal _IsKeyF2 As Boolean = false) Handles _ myWlbPh0.AddValidate, myWlbPh1.AddValidate End Sub VB.NET - German Discussions CancelEventArgs (1) VB (1) AddValidate (1) Validating (1) RaiseEvent (1) TuWas (1) Neuaufsetzen (1) Neuerstellen (1
message 1001 Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating TextBox1.BackColor = Color.White If TextBox1.Text.Length = 0 Then TextBox1.Select Can anybody tell me how to deal with it ? Thanks . VB.NET Discussions System.ComponentModel.CancelEventArgs (1) TextBox1.Text.Length (1) TextBox1.BackColor (1) CancelEventArgs (1) TextBox1.Select (1) TextBox (1) System.Object (1) Color.Yellow (1) Hello, from what be right. Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating TextBox1.BackColor = 3D Color.White If TextBox1.Text.Length = 3D 0 Then
want Time values Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtJanlight1on1.Validating If TxtJanlight1on1.Text <> "" AndAlso Not IsDate(TxtJanlight1on1.Text) Then MsgBox("Please enter do to reject dates and only accept valid time values ? VB.NET Discussions System.ComponentModel.CancelEventArgs (1) DateTime.TryParseExact (1) DateTime.ParseExact (1) CancelEventArgs (1) DateTimeStyles (1) TimeSpan.Parse (1) TryParseExact (1) TimeSpan (1) A DateTime value is any is as follows, Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtJanlight1on1.Validating Dim d As DateTime If Not DateTime.TryParseExact("17:00:00", "HH