• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1639
  • Last Modified:

name cells in msflexgrid using vb6

Hi
I use many msFlexgrids in different project,like this:
grid.textmatrix(2,4) = "runs"
how can i name the textmatrix(2,4) to a name and then refer to the name ?

using vb6
0
isnoend2001
Asked:
isnoend2001
  • 4
  • 3
2 Solutions
 
isnoend2001Author Commented:
89408
0
 
Martin LissRetired ProgrammerCommented:
The short answer is you can't. You could however give names to the two parts of the textmatrix's parameters. This could be done by defining constants with values of 2 and 4 etcetera. However I like to use Enums as follows.

Option Explicit
Private Enum MyEnum
    NameA ' Can be anyt name. The default value is 1.
    NameB ' Default value is 2, etc
    NameX ' 3
    NameY ' 4
End Enum

Open in new window


Then do this
grid.textmatrix(NameB, NameY) = "runs"

You would of course use more descriptive names.
0
 
aikimarkCommented:
Add a class module to your project and name it clsArrayPoint.  Give it two public variables to hold the coordinates of your bookmarks.
Example:
Option Explicit

Public TMrow As Long
Public TMcol As Long

Open in new window


This code will test the class and store the coordinates in a dictionary object and then retrieve the bookmarks.
    Dim clsBookmark As clsArrayPoint
    Dim dicBookmarks As Object
    Set dicBookmarks = CreateObject("scripting.dictionary")
    Set clsBookmark = New clsArrayPoint
    With clsBookmark
        .TMrow = 2
        .TMcol = 4
    End With
    dicBookmarks.Add 89408, clsBookmark
    
    Set clsBookmark = New clsArrayPoint
    With clsBookmark
        .TMrow = 54
        .TMcol = 9
    End With
    dicBookmarks.Add "BR549", clsBookmark
    
    
    Set clsBookmark = dicBookmarks(89408)
    Debug.Print "Bookmark 89408 coordinates (row,col):", clsBookmark.TMrow, clsBookmark.TMcol
    
    Set clsBookmark = dicBookmarks("BR549")
    Debug.Print "Bookmark BR549 coordinates (row,col):", clsBookmark.TMrow, clsBookmark.TMcol

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
isnoend2001Author Commented:
thought their may have been a simple solution
MartinLiss
 have to give your solution some thought.
Most have a description column and a data column
so that might work

aikimark
many apps are included in setups and i hate to include references eg;vbscript

It may be easier to just to use it like i have been
0
 
isnoend2001Author Commented:
got this from another forum:
 Option Explicit

        Private colRows As Collection
        Private colCols As Collection

        Private Sub AllocateName(strName As String, intRow As Integer, intCol As Integer, Optional boModify = False)
        '
        ' Inserts values in the 'look-up' table
        ' If the optional argument is specified as True, will modify an
        ' existing name's row and column
        '
        If Not boModify Then
            On Error Resume Next    ' we're going to check the error code
            colRows.Add intRow, strName
            If Err.Number = 0 Then
                On Error GoTo 0     ' back to normal error handling
                colCols.Add intCol, strName
            Else
                On Error GoTo 0     ' back to normal error handling
                MsgBox "Name " & strName & " already Allocated"
            End If
        Else
            On Error Resume Next    'Ignore the error if it's not there
            colRows.Remove strName
            colCols.Remove strName
            On Error GoTo 0         ' Back to normal error handling
            colRows.Add intRow, strName
            colCols.Add intCol, strName
        End If
        End Sub

        Private Sub SetValue(gr As MSFlexGrid, strName As String, strValue As String)
        Dim intRow As Integer
        Dim intCol As Integer
        intRow = colRows.Item(strName)
        intCol = colCols.Item(strName)
        gr.TextMatrix(intRow, intCol) = strValue
        End Sub

        Private Function GetValue(gr As MSFlexGrid, strName As String) As String
        Dim intRow As Integer
        Dim intCol As Integer
        intRow = colRows.Item(strName)
        intCol = colCols.Item(strName)
        GetValue = gr.TextMatrix(intRow, intCol)
        End Function

        Private Sub Form_Load()
        Set colRows = New Collection
        Set colCols = New Collection
        grid.Rows = 5
        grid.Cols = 5
        grid.FixedCols = 0
        grid.FixedRows = 1
        grid.TextMatrix(2, 0) = "Fred's second value"
        Call AllocateName("Fred", 1, 0)
        Call AllocateName("Bert", 1, 1)
        SetValue grid, "Fred", "Doogle"
        SetValue grid, "Bert", "Doogle2"
        Debug.Print GetValue(grid, "Fred")
        AllocateName "Fred", 2, 0, True
        Debug.Print GetValue(grid, "Fred")
        End Sub

     
thought there might be something simple
   Think i will stick with referring to cells like i have been
0
 
aikimarkCommented:
>>i hate to include references eg;vbscript

There are no references.  This is a dynamic object creation from a system resource.
There is no VB Script in this code or this object.

>>It may be easier to just to use it like i have been
* So two different collections are easier than a single dictionary object?!?
* The advantage of the dictionary object in this scenario is that you can use its .Exists() method to determine if the key/name has been defined.  Another advantage is that dictionary keys can be numeric values.
Here's a good article on the Dictionary class worth reading:
http:A_3391-Using-the-Dictionary-Class-in-VBA.html

Note: duplicated keys/names in the two collection configuration waste memory

Here is a version of your code that uses a dictionary and the class object mentioned in my prior comment.
Option Explicit
    Private dicBookmarks As Object

Private Sub AllocateName(strName As String, intRow As Integer, intCol As Integer, Optional boModify = False)
    '
    ' Inserts values in the 'look-up' table
    ' If the optional argument is specified as True, will modify an
    ' existing name's row and column
    '
    Dim clsBookmark As clsArrayPoint
    
    If boModify Then
        If dicBookmarks.Exists(strName) Then
            dicBookmarks(strName).TMrow = intRow
            dicBookmarks(strName).TMcol = intCol
        Else
            MsgBox "Name " & strName & " does not exist -- modify not possible."
            'shouldn't this really be an .Add action here instead of an error?
        End If
    Else
        If dicBookmarks.Exists(strName) Then
            MsgBox "Name " & strName & " already Allocated -- create not possible."
            'shouldn't this really be a replace/update action here instead of an error?
        Else
            Set clsBookmark = New clsArrayPoint
            With clsBookmark
                .TMrow = intRow
                .TMcol = intCol
            End With
            dicBookmarks.Add strName, clsBookmark
        End If
    End If
End Sub

Private Sub SetValue(gr As MSFlexGrid, strName As String, strValue As String)
    If dicBookmarks.Exists(strName) Then
        gr.TextMatrix(dicBookmarks(strName).TMrow, dicBookmarks(strName).TMcol) = strValue
    Else
        MsgBox "Name " & strName & " does not exist -- SetValue not possible."
    End If
End Sub

Private Function GetValue(gr As MSFlexGrid, strName As String) As String
    If dicBookmarks.Exists(strName) Then
        GetValue = gr.TextMatrix(dicBookmarks(strName).TMrow, dicBookmarks(strName).TMcol)
    Else
        MsgBox "Name " & strName & " does not exist -- GetValue not possible."
    End If
End Function

Private Sub Form_Load()
    Set dicBookmarks = CreateObject("scripting.dictionary")
    
    grid.Rows = 5
    grid.Cols = 5
    grid.FixedCols = 0
    grid.FixedRows = 1
    grid.TextMatrix(2, 0) = "Fred's second value"
    
    Call AllocateName("Fred", 1, 0)
    Call AllocateName("Bert", 1, 1)
    SetValue grid, "Fred", "Doogle"
    SetValue grid, "Bert", "Doogle2"
    Debug.Print GetValue(grid, "Fred")
    AllocateName "Fred", 2, 0, True
    Debug.Print GetValue(grid, "Fred")
End Sub

Open in new window

0
 
isnoend2001Author Commented:
Thanks for explaining. i thought the scripting.dictionary was a reference
0
 
aikimarkCommented:
If you don't use createobject(), then you will need a reference in your project for any non-intrinsic object.  

The dictionary object is closely related to the filesystemobject, which you probably use elsewhere in your application, since they both belong to the "scripting" library/namespace.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now