Making a Class Based on a TableView

Post

Posted
Rating:
#1 (In Topic #268)
Trainee
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.

Code (gambas)

  1. Private Sub Me_KeyPress()
  2.  
  3.   'Message("Hello")
  4.   Select Case Key.Code
  5.     Case Key.Esc, Key.BackSpace, Key.Del
  6.       ss = ""
  7.       Me.UnSelectAll
  8.     Case Key.Enter, Key.Return
  9.       Raise EnterOnLine 'action on pressing Enter
  10.       ss = ""
  11.       Me.UnSelectAll
  12.     Case Key.Tab
  13.       SearchDown
  14.     Case Key.BackTab
  15.       SearchUp
  16.     Case Else
  17.       ss &= Key.Text
  18.       SearchDown
  19.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
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:

Code (gambas)

  1. Public Sub Form_KeyPress()
  2.  
  3. \{your code goes here}
  4.  

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
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

Code (gambas)

  1. Public Sub SearchBox1_Keypress()
  2.   CheckKey
  3.  
  4. Public Sub SearchBox2_KeyPress()
  5.   CheckKey

That clears things up. I'll give that a go. Thanks.
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
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

Code (gambas)

  1. 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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
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)

Code (gambas)

  1. Public Sub GridViewSetAlignment(InAlign As String, ByRef InGridView As Gridview)
  2.  
  3.   Dim TmpAry As String[]
  4.   Dim TmpInt As Integer
  5.  
  6.   If Trim(InAlign) <> "" Then
  7.     InAlign = UCase(InAlign)
  8.     TmpAry = Split(InAlign, ",")
  9.     For TmpInt = 0 To TmpAry.Max
  10.       If TmpAry[TmpInt] = "L" Then InGridView.Columns[TmpInt].Alignment = Align.Left
  11.       If TmpAry[TmpInt] = "R" Then InGridView.Columns[TmpInt].Alignment = Align.Right
  12.       If TmpAry[TmpInt] = "C" Then InGridView.Columns[TmpInt].Alignment = Align.Center
  13.       If TmpAry[TmpInt] = "N" Then InGridView.Columns[TmpInt].Alignment = Align.Normal
  14.     Next
  15.   End If
  16.  
  17.   Message(Error.Text)
  18.  

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
Cedron is in the usergroup ‘Regular’
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"

Gambas One - Gambas ONE

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!
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
Just looked at your posts, Ced. Very clever. Four months is about how long I have been with Gambas too. Thanks.
Online now: No Back to the top
1 guest and 0 members have just viewed this.