Making a Class Based on a TableView

Post your Gambas programming questions here.
Post Reply
GerardBuzolic
Posts: 14
Joined: Tuesday 14th May 2019 8:00pm

Making a Class Based on a TableView

Post by GerardBuzolic »

I am new to Gambas, coming from Xojo, and trying to come to terms with making a new class based on a TableView. I am missing something, probably quite simple. I've created a class called SearchBox. I have put in the line Inherits TableView. I've added a SearchBox to the main form and it looks like a TableView and shows data okay. I would like my SearchBox to respond to the KeyPress event but I have had no luck so far. I have looked in examples from the Software Farm but haven't found anything (that I understand). I have the following Sub in the SearchBox class code, and it was working fine before I moved it into a class. I am guessing Me_KeyPress is not the right name for the handler. I'd be grateful for some help.
Private Sub Me_KeyPress()

  'Message("Hello")
  Select Case Key.Code
    Case Key.Esc, Key.BackSpace, Key.Del
      ss = ""
      Me.UnSelectAll
    Case Key.Enter, Key.Return
      Raise EnterOnLine 'action on pressing Enter
      ss = ""
      Me.UnSelectAll
    Case Key.Tab
      SearchDown
    Case Key.BackTab
      SearchUp
    Case Else
      ss &= Key.Text
      SearchDown
  End Select

End
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Making a Class Based on a TableView

Post by Quincunxian »

Hi GB,
Right click on the form with the table view and 'mouse over' the 'Event' selection in the menu list.
This will show you all the events that are accessible for the form ( works the same with any control as well).
If any events have code already written, they will have a tick mark against them.

Select 'KeyPress' and it should drop you to the editor with the correct syntax to trap key events.

in this case it will be:
Public Sub Form_KeyPress()
 
{your code goes here}

End
Cheers - Quin.
I code therefore I am
GerardBuzolic
Posts: 14
Joined: Tuesday 14th May 2019 8:00pm

Re: Making a Class Based on a TableView

Post by GerardBuzolic »

Thanks Quin. I think you are saying that the KeyPress handler has to be in the form, and duplicated for every instance of the class SearchBox that I have. Which is what TableViews do, and I should have thought of that. I can simplify things by calling the code I typed in my first post "CheckKey" and put that in the SearchBox class, and then the form would have short handlers like
Public Sub SearchBox1_Keypress()
  CheckKey
End

Public Sub SearchBox2_KeyPress()
  CheckKey
End
That clears things up. I'll give that a go. Thanks.
GerardBuzolic
Posts: 14
Joined: Tuesday 14th May 2019 8:00pm

Re: Making a Class Based on a TableView

Post by GerardBuzolic »

That worked fine. I dragged an instance of SearchBox from the toolbox of classes onto the form. (I found I had to have the word EXPORT in at the start of the class and run the program for it to appear in the toolbox.)

However, when I tried to instantiate the SearchBox in code using
Public sb1 As New SearchBox(FMain)
the sb1_Keypress and sb1_Click and sb1_Save handlers in the form's code don't do anything.

So there is still something I am missing. What else do I need to do when I instantiate a class in code? Thanks.
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Making a Class Based on a TableView

Post by Quincunxian »

From what I'm reading it seems like you are creating a 'new' component rather than just instantiating a user created class.
Read some detailed information about how to do that here: http://gambaswiki.org/wiki/dev/gambas

From your initial post, I had thought that you were just creating a helper/toolbox class for a standard TableView as this would be the simplest way. My Bad.

Something to note; You can pass a control ByRef to a class subroutine as a parameter and then act on it as desired.
ie: Set the alignment of the GridView columns ( same as a TableView but not editable )
If the gridview has 3 columns that you want set as: center, center, right, you would pass these parameters.

GridViewSetAlignment("C,C,R",MyGridview)
Public Sub GridViewSetAlignment(InAlign As String, ByRef InGridView As Gridview)

  Dim TmpAry As String[]
  Dim TmpInt As Integer

  If Trim(InAlign) <> "" Then
    InAlign = UCase(InAlign)
    TmpAry = Split(InAlign, ",")
    For TmpInt = 0 To TmpAry.Max
      If TmpAry[TmpInt] = "L" Then InGridView.Columns[TmpInt].Alignment = Align.Left
      If TmpAry[TmpInt] = "R" Then InGridView.Columns[TmpInt].Alignment = Align.Right
      If TmpAry[TmpInt] = "C" Then InGridView.Columns[TmpInt].Alignment = Align.Center
      If TmpAry[TmpInt] = "N" Then InGridView.Columns[TmpInt].Alignment = Align.Normal
    Next
  End If

Catch
  Message(Error.Text)

End
Cheers - Quin.
I code therefore I am
GerardBuzolic
Posts: 14
Joined: Tuesday 14th May 2019 8:00pm

Re: Making a Class Based on a TableView

Post by GerardBuzolic »

Thanks, Quin. I shall read up on components, though I don't think I am ambitious enough to make any of those. I am not quite ready to add to Gambas itself. I am satisfied if I can add to a program. ;) I understand your code about passing a class ByRef to a sub.

Your earlier advice answered my earlier question perfectly. Events have to have handlers in the form. It is no use having the handler in the class. So obvious. The class holds various subs that you can call upon, but the calling has to be done from event handlers in the form.

I don't expect to ever really need to create an instance of a class from within code. Dragging one onto the form at design time is all I shall use.

I wrote a version of a game once—the commercial version was called "Concentration"—where you turn over cards and if you find a match the pair was removed, trying to remember where matching cards were. A bigger board size showed more "cards", and (this is in Xojo) I made as many instances of the card class as the user asked for. But gridviews show pictures in cells, so it is much easier to have one gridview with 6x10 rows and columns than 6x10 instances of a card class. So instantiating a new class derived from a TableView using code is just for academic interest only. Thanks again. Shall read up on components.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Making a Class Based on a TableView

Post by Cedron »

Hi Gerard,

I've been going at Gambas heavily for about four months now. My initial assessment is that Component development is unnecessarily cumbersome. Let's leave it at that for now.

If I need something only C can do, I have found writing a shared library to be much easier, and much more productive. If I need something on the forms, the controls that exist have been adequate for all my needs.

I recommend you load and run, then read, the programs I posted in "Programming is supposed to be fun"

https://forum.gambas.one/viewtopic.php?f=4&t=674

In the second example, I wrap a class around a form control, which in essence is the same as creating a new control. And yes, the instances (some) are created from code.

Ced

P.S. Quin does seem to have a knack for figuring out what you are actually asking.
.... and carry a big stick!
GerardBuzolic
Posts: 14
Joined: Tuesday 14th May 2019 8:00pm

Re: Making a Class Based on a TableView

Post by GerardBuzolic »

Just looked at your posts, Ced. Very clever. Four months is about how long I have been with Gambas too. Thanks.
Post Reply