?
Solved

Change ContextMenuStrip ToolStripMenuItem Text on the fly

Posted on 2009-04-19
17
Medium Priority
?
1,154 Views
Last Modified: 2013-11-26
I am trying to change the text of a ToolStripMenuItem before the menu is displayed after a right-click.  I would like to include the DataGridView row KEY, so that it's more clear to the user what line they actually clicked.  I've attempted a number of different methods with no success.
(Visual Studio 8 - vb)
eg.  
ContextMenuStrip1
              ToolStripMenuItem1    = 'Edit Item 1007'
              ToolStripMenuItem2    = 'Delete Item 1007'

not sure what method to use here: ToolStripMenuItem1.text = "Edit Item " & row_item
 
    Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        cell_row = e.RowIndex
        cell_col = e.ColumnIndex
        row_item = DataGridView1.Rows(e.RowIndex).Cells("ItemNo").Value
    End Sub

Open in new window

0
Comment
Question by:inkineu
  • 9
  • 8
17 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24182381
I had this code to work on my system, so I can only assume that your error is in the name of the cell. try it using the index number and see if that works.

And you should add  .ToString to ensure that you are setting a string to a string as the .Value property is an object that can be anything not just a string. If you have option strict On by default it would pick things like that up.
    Private Sub DataGridView1_cellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
 
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.EditXxxToolStripMenuItem.Text = "Edit " & DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
        End If
 
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:inkineu
ID: 24183221
I'm not acutally getting an error, it just doesn't change the menu display when I do the right click.  Almost as if it needs to repaint.  The code is hitting the me.toolstipmenuitem.text setting in the cellmousedown, but the change does not appear in the contextmenu display.  Not sure why.  
0
 
LVL 1

Author Comment

by:inkineu
ID: 24183297
I think by the time the cellmousedown event fires, the contextmenu layout has already occurred.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 15

Expert Comment

by:ChloesDad
ID: 24183696
It works for me, can you post you complete event handler code as the code you originally posted doesnt set the text property of the menu item in the event handler
0
 
LVL 1

Author Comment

by:inkineu
ID: 24186195
Here you go.  In my ContextMenuStrip collection, ToolStripMenuItem1 is set to "TEST".  The "TEST" never changes to read "Edit Item ..."
    Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.ToolStripMenuItem1.Text = "Edit Item " & DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
        End If
    End Sub
 
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        If DataGridView1.SelectedRows.Count > 0 Then
            DataGridView1.ClearSelection()
        End If
 
    End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24186689
If you put a breakpoint on

            Me.ToolStripMenuItem1.Text = "Edit Item " & DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString

does the code stop at that point after you right click on a cell?
0
 
LVL 1

Author Comment

by:inkineu
ID: 24187697
Yes, after I right click the cell, it stops at the me.ToolStripMenuItem1.Text line.
When I do the breakpoint at the 'End If', it says the me.ToolStripMenuItem1.Text was set to 'Edit Item 100'.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24188073
Ok, thats good news.

Right click on another cell, and see what the value if before it is set second time around, it should be edit item 100.

If it is then check that toolstripmenuitem1.visible is set to true. It should be but better to check when things dont appear as planned
0
 
LVL 1

Author Comment

by:inkineu
ID: 24189642
Yes it is.  Visible is set to True.  I just deleted the control and recreated it.  Same problem.  
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24189849
Is it possible to post the complete form here as what you are doing seems to be correct, and my code here works as we want it to.
0
 
LVL 1

Author Comment

by:inkineu
ID: 24190212
Here you go.  I stripped it down and it still gives me the same result.

Microsoft Visual Studio 2008
vers 9.0.21022.8 RTM
Imports MySql.Data.MySqlClient
 
Public Class frmMain
    Dim myData As New DataSet
    Dim myDataView As New DataView
    Dim myDataTable As New DataTable
    Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand
    Dim myAdapter As New MySqlDataAdapter
    Dim myReader As MySqlDataReader
    Dim conn_string As String = "server=xxxxx ... etc"
    Private Sub PopulateDataGridView()
        Dim strSQL As String
 
        conn.ConnectionString = conn_string
 
        Try
            conn.Open()
 
            strSQL = "SELECT ItemNo from tbltest"
 
            cmd.CommandText = strSQL
            cmd.Connection = conn
 
            myAdapter.SelectCommand = cmd
            myAdapter.Fill(myDataTable)
            DataGridView1.DataSource = myDataTable
 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
    End Sub
 
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PopulateDataGridView()
        DataGridView1.ClearSelection()
    End Sub
 
    Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.DeliveryToolStripMenuItem.Text = "Edit " & DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
        End If
    End Sub
 
End Class

Open in new window

0
 
LVL 1

Author Comment

by:inkineu
ID: 24191269
Just a thought, I have the ContextMenuStrip attached to a specific Datagridview column rather than to the Datagridview.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24191574
I have this line of code in my Form load event, although it could go at the bottom of public sub new as well

        DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1

and it wrks on all columns

when I changed it to

        DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip1

it only worked on the first column, all others had no effect and I then added a second context menu.

        DataGridView1.ContextMenuStrip = Me.ContextMenuStrip2

The first column gave me the contents of contextmenu1, the rest gave me the contents of contextmenu2. ToolstripmenuItem1 is on context menu1, so when I see contextmenu2 I dont see toolstripmenuitem1

You dont have two context menus do you?

0
 
LVL 1

Author Comment

by:inkineu
ID: 24191787
No, I only have 1 context menu.  I actually wanted the menu to appear only if the 3rd column was right clicked.  It seems that it only works for the 1st column.  
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24191911
I'll change mine to the third column when I get home later and report my results
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 920 total points
ID: 24193147
I have check this at home and found the following
changing

        DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip1

        DataGridView1.Columns(2).ContextMenuStrip = Me.ContextMenuStrip1

means that only when I right click on a cell in column 3 I get the correct context menu.

I did have something a while ago where menu items were not updating when I added new items etc.

http://www.experts-exchange.com/Microsoft/Development/Visual_Studio_Express/Q_23788677.html

I never got to the bottom of it, but it might give you a clue

0
 
LVL 1

Author Comment

by:inkineu
ID: 24235001
Thanks for your help.  Appreciate it.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question