Bad row index

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Bad row index

Post by gambafeliz »

Hello

I have a problem that I don't know how to solve.

I use the MouseDown event to change a popupmenu.

I do something like this.
Public Sub GridView1_MouseDown()   
   
   If Mouse.Right Then
      
      ' In the following line I get the error: GridView1.Row (Bad row index)
      Dim sSQL As String = "Select row1, row2 From Tabla1 Where row2 ='" & GridView1[GridView1.Row, 0].Text & "';"
      
      Dim resultado As Result = mG.gConn.Exec(sSQL)
      
      If resultado.Count < 1 Then
         GridView1.PopupMenu = Popup1.Name
      Else
         GridView11.PopupMenu = Popup2.Name
       Endif
   Endif
   
End
But when I try to do a previous query to see if you have records and thus determine whether to put a popup menu or another, it returns the following error.

And I don't know how to do the previous consultation to change the popupmenu.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: Bad row index

Post by thatbruce »

Try something like...


Public Sub GridView1_MouseDown()

If GridView1.Row < 0 Then
' either display some message like "No row selected" or just
Return
End If

If Mouse.Right Then ...

hth
b
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Bad row index

Post by gambafeliz »

The problem is that when I replace the MouseDown event with MouseUp everything works but the popup doesn't appear until I press the right click again. I mean, it forces me to repeat twice the right click on the row, this for the user is wrong.

but when I do the same with MouseDown, then since the click on the row has not occurred, it tells me the error, so I don't know what to do.

-- Maybe if someone tells me how to get the popup menu by code maybe this is the solution. --
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Bad row index

Post by gambafeliz »

Fixed.

I come from the previous version (3.12) of Gambas and in this I am new (3.15.2)

I notice that the Click() event changed to MouseDown and MouseUp but what I didn't know and it's very convenient is that now the event that occurs on the right click is now unambiguous with the Menu() event, now the right event is well separated from the left.

So it is solved thanks to the Menu() event where I detect completely clear that the user presses the right click and therefore I execute the same code and I know the clicked row and I open the appropriate popup.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Bad row index

Post by BruceSteers »

If you wish to stop an events default actions you can use the "Stop Event" instruction.

PS, you should always prefer the MouseUp event to the MouseDown one.

Using MouseUp and Mouse.Inside() you can do the thing where if you click on something you can move the mouse away from it before letting the button go to cancel the click.

Another possibility is to NOT use the
GridView1.PopupMenu = Popup1.Name
instead leave the PopupMenu blank so it does not try to do anything and use the command Popup1.Popup() or Popup2.Popup() instead. That way you are in control of the action.

like...
      If resultado.Count < 1 Then
         Popup1.Popup(Mouse.ScreenX, Mouse.ScreenY)
      Else
         Popup2.Popup(Mouse.ScreenX, Mouse.ScreenY)
       Endif
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Bad row index

Post by gambafeliz »

Thank you very much :)

Although I have solved it I believe well. You give me very interesting information to renew my code and update my knowledge.

Thank you, you are really very kind. Some day I will help you in your code, but it may not happen, since I suppose you are a better Gambas developer than me.

Greetings.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Bad row index

Post by BruceSteers »

gambafeliz wrote: Wednesday 5th October 2022 10:46am Thank you very much :)

Although I have solved it I believe well. You give me very interesting information to renew my code and update my knowledge.

Thank you, you are really very kind. Some day I will help you in your code, but it may not happen, since I suppose you are a better Gambas developer than me.

Greetings.
All roads lead to Rome they say :)

Just a note for the Menu event (that is also a good solution:) )
If you do not want the menu to pop up if nothing is selected (this will happen if right click not on a list item) then do the following..

Public Sub GridView1_Menu()   
    
  If GridView1.Row = -1 Then 
    Stop Event  ' cancel the popup event
    Return
  Endif

      ' In the following line I get the error: GridView1.Row (Bad row index)
      Dim sSQL As String = "Select row1, row2 From Tabla1 Where row2 ='" & GridView1[GridView1.Row, 0].Text & "';"
       
      Dim resultado As Result = mG.gConn.Exec(sSQL)
       
      If resultado.Count < 1 Then
         GridView1.PopupMenu = "Popup1"
      Else
         GridView11.PopupMenu = "Popup2"
       Endif
    
End
Happy coding :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply