Hi boards,

I managed to read text file into MSHFlexgrid.
Now i want to search a word in textbox and higlight it in flexgrid.
Anyone know how to do this?

Thank you

Dani AI

Generated

As 's answer shows, iterating the grid and using the TextMatrix property to check each cell is the right idea for a simple search. The common refinements are: do case-insensitive or partial matches, skip fixed header rows/cols, highlight only the matched cells (and restore previous colors when needed), and scroll the grid so the match is visible. The MSHFlexGrid/TextMatrix model and data-binding behavior are documented in the Visual Basic library. (library.thedatadungeon.com)

Here is a compact, practical example you can drop into a command button (adjust control names as needed). It does a case-insensitive partial match, clears previous highlights, highlights matches, and scrolls the first match into view:

Private Sub cmdFind_Click()
    Dim r As Long, c As Long
    Dim needle As String

    needle = Trim$(Text1.Text)
    If Len(needle) = 0 Then Exit Sub

    ' clear previous highlights
    For r = MSHFlexGrid1.FixedRows To MSHFlexGrid1.Rows - 1
        For c = MSHFlexGrid1.FixedCols To MSHFlexGrid1.Cols - 1
            MSHFlexGrid1.Row = r
            MSHFlexGrid1.Col = c
            MSHFlexGrid1.CellBackColor = 0
        Next c
    Next r

    ' search and highlight
    For r = MSHFlexGrid1.FixedRows To MSHFlexGrid1.Rows - 1
        For c = MSHFlexGrid1.FixedCols To MSHFlexGrid1.Cols - 1
            If InStr(1, MSHFlexGrid1.TextMatrix(r, c), needle, vbTextCompare) > 0 Then
                MSHFlexGrid1.Row = r
                MSHFlexGrid1.Col = c
                MSHFlexGrid1.CellBackColor = vbYellow
                MSHFlexGrid1.TopRow = r
            End If
        Next c
    Next r
End Sub

Note: CellBackColor applies to the active cell or current selection; setting it to 0 restores the default coloring. If you want to style a whole selected range at once set FillStyle appropriately before changing CellBackColor. See the MSHFlexGrid cell-coloring rules for details. (techshelps.github.io)

Practical tips: loop from FixedRows/FixedCols to Rows-1/Cols-1 so headers stay untouched, and use TopRow to make a found row visible (or RowSel/ColSel to select a whole row). TextMatrix uses numeric row/col indices (zero-based when you set up rows/cols in code). (vb-helper.com)

Gotchas and performance: always set Row and Col before reading/writing cell properties, and if you need to restore non-default colors later capture CellBackColor values (or reset with 0). A simple nested-loop search is O(rows*cols) but performs well for typical grid sizes; for very large, data-bound grids consider searching the source data instead of the control. (hanatyan.sakura.ne.jp)

Recommended Answers

All 2 Replies

This should working :

Private Sub Command1_Click()
xSearch = Text1.Text
For i = 0 To Grid.Rows - 1
    If xSearch = Grid.TextMatrix(i, 1) Then
        Grid.Row = Grid.TextMatrix(i, 1)
        For j = 1 To Grid.Cols - 1
            Grid.Col = j
            Grid.CellBackColor = vbCyan
        Next j
    End If
Next i
End Sub
commented: Thanks!!! +0

Thank You sir.
It works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.