CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2001
    Location
    Wisconsin, USA
    Posts
    150

    Right Click Menus in Windows

    I am building an application in VB 6.0 that will allow a user to add, edit, change, and delete client information
    that is stored in the database. Part of this information is user telephone information. A user can have more
    than one telephone number, so when I retrieve this information I load it into an MS Flex Grid control. Now, the
    person who is performing the maintenance should be allowed to add, change, and delete telephone information. My question
    is this:

    How do you build popup menus? I need to have a menu come up when the user right-clicks on the grid when the app is in
    add or edit mode. I know how to code the event firing, but how do you create the menu?

    Also, is there a way to make the flex grid editable? Like when the user selects a cell, let the user enter information
    directly into that cell?

    Spectre


  2. #2
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Right Click Menus in Windows

    To create popup menus, use the menu editor just like you normally would to create a menu along the top of the window. Lets say you make a menu called File (mnuFile) that has a few options under it. To pop that menu up with the right click event of the mouse, under the MouseUp event of your form or control, add the code:


    If Button = 2 then
    PopupMenu mnuOptions
    End If




    play around with that and see if it works. there is no inherent way to make a flexgrid editable, but you can make it look that way with a little trick- what you have to do is create a textbox that is hidden. when a user clicks on a given cell, move the textbox to that cell's position with its height and width, make sure the textbox has no bordering, then make it visible. sound fun? i thought it was pretty cool anyway. If you need source for this just let me know, i'm sure i have it around here somewhere! good luck!

    Jeff


  3. #3
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Right Click Menus in Windows

    oops! i just noticed that i refered to the popup menu as mnuFile in my text and then copied code that pops up mnuOptions. i meant to say mnuFile there too! sorry!


  4. #4
    Join Date
    Aug 2001
    Location
    PA
    Posts
    150

    Re: Right Click Menus in Windows

    Choose the mousdown event of your msflegrid and put this code in it. I am doing the same thing. This will allow a popup menu when you right click anywhere within the msflexgrid


    Private Sub nameofgrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

    On Error Resume Next

    If Button And vbRightButton Then
    PopupMenu nameofyourmenue
    End If
    End Sub

    Thanks
    Hisham
    Thank You, Hisham

  5. #5
    Join Date
    Aug 2001
    Location
    LA
    Posts
    2

    Re: Right Click Menus in Windows


    I'm doing the same thing - right click menu when you click on the grid. But how do you create a popup menu? Is 'PopupMenu' buildin flexGrid function or I need to create my own popup. I use divs to create a popup menu but apparently, with z-index, the popup won't go above the grid so mypopup menu is invisible when I right click on the grid

    Thanks


  6. #6
    Join Date
    Aug 2001
    Location
    LA
    Posts
    2

    Re: Right Click Menus in Windows

    where can i get the menu editor to create a popup menu???

    Thanks,


  7. #7
    Join Date
    Aug 2001
    Location
    PA
    Posts
    150

    Re: Right Click Menus in Windows

    Hi,

    In VB go to tools and select menue ditor, or just press ctrl + E. You will get the menu builder. Then start to create your menu there. The name you give it is the name you should use in the previous code I have sent you.



    Thanks
    Hisham
    Thank You, Hisham

  8. #8
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Right Click Menus in Windows

    Do it the same way you create any menu in VB(Tools... Menu Editor) then set the Visible property of the Top level item to False.




  9. #9
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Right Click Menus in Windows

    The MSFlexGrid doesn't inherently allow you to edit data in a cell. Here is the code that illustrates a workaround, using a textbox.

    Place a textbox on a form and set the following properties

    Appearance = 0 'Flat
    BorderStyle = 0 'None
    Height = 375
    Left = 1560
    TabIndex = 1
    Text
    Top = 240
    Width = 1455

    And a MSFlexGrid control with the following properties

    Height = 2895
    Left = 600
    TabIndex = 0
    Top = 720
    Width = 4215
    Rows = 5
    Cols = 5

    Now paste the following code into the form



    private Sub MSFlexGrid1_DblClick()
    GridEdit Asc(" ")
    End Sub

    private Sub MSFlexGrid1_KeyPress(KeyAscii as Integer)
    GridEdit KeyAscii
    End Sub
    Sub GridEdit(KeyAscii as Integer)
    'use correct font
    Text1.FontName = MSFlexGrid1.FontName
    Text1.FontSize = MSFlexGrid1.FontSize
    Select Case KeyAscii
    Case 0 to Asc(" ")
    Text1 = MSFlexGrid1
    Text1.SelStart = 1000
    Case else
    Text1 = Chr(KeyAscii)
    Text1.SelStart = 1
    End Select

    'position the edit box
    Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Text1.Width = MSFlexGrid1.CellWidth
    Text1.Height = MSFlexGrid1.CellHeight
    Text1.Visible = true
    Text1.SetFocus
    End Sub

    private Sub MSFlexGrid1_LeaveCell()
    If Text1.Visible then
    MSFlexGrid1 = Text1
    Text1.Visible = false
    End If
    End Sub

    private Sub MSFlexGrid1_GotFocus()
    If Text1.Visible then
    MSFlexGrid1 = Text1
    Text1.Visible = false
    End If
    End Sub

    private Sub Text1_KeyDown(KeyCode as Integer, Shift as Integer)
    Select Case KeyCode
    Case vbKeyEscape
    Text1.Visible = false
    MSFlexGrid1.SetFocus
    Case vbKeyReturn
    MSFlexGrid1.SetFocus
    Case vbKeyDown
    MSFlexGrid1.SetFocus
    DoEvents
    If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 then
    MSFlexGrid1.Row = MSFlexGrid1.Row + 1
    End If
    Case vbKeyUp
    MSFlexGrid1.SetFocus
    DoEvents
    If MSFlexGrid1.Row > MSFlexGrid1.FixedRows then
    MSFlexGrid1.Row = MSFlexGrid1.Row - 1
    End If
    End Select
    End Sub

    private Sub Text1_KeyPress(KeyAscii as Integer)
    'noise suppression
    If KeyAscii = vbKeyReturn then KeyAscii = 0
    End Sub






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured