How to use a cursor inside a Textbox to edit MSFlexgrid cell...

I am using an MSFlexgrid with a single textbox for editing purposes.  I already have coded the ability for a user to enter values into each cell.  What I need now is the ability to click on any given cell and have a blinking cursor appear at the exact position the user clicked.  Then, I would like to allow the user to edit their value by selecting certain characters only (i.e. if value is 123.456, the user can choose to change just the 2 and 3)  or the whole value.      
Anjinsan5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anjinsan5Author Commented:
Any suggestions?
0
jmundsackCommented:
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As Long
    Dim p As Single
    For i = 1 To Len(Text1.Text)
        p = Me.TextWidth(Left$(Text1.Text, i))
        If X < p Then
            Text1.Enabled = True
            Text1.SelStart = i - 1
            Text1.SetFocus
            Exit Sub
        End If
    Next i
End Sub
0
jmundsackCommented:
I used a picturebox and embedded the textbox in it at pos 0,0.

You will need to adapt X to be the mouse position relative to the left side of the selected cell.

Do you know how to get the screen coordinates of a given cell?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jmundsackCommented:
I used a picturebox and embedded the textbox in it at pos 0,0.

You will need to adapt X to be the mouse position relative to the left side of the selected cell.

Do you know how to get the screen coordinates of a given cell?
0
Anjinsan5Author Commented:
Jmundsack,

Thanks for the suggestion,  I apologize for the delay in getting back to you.  Not quite sure how to get the screen coordinates of the current cell.  Also, should the picture box be visible or not.  I am using a textbox to edit over an msflexgrid; do you see any conflict with putting a picture box over a textbox in my scenario?  
0
jmundsackCommented:
Disregard the business about the picturebox -- I was only using that because I didn't want to go through the trouble of creating a flexgrid.  Let me throw something together with a grid and then post it for you.
0
jmundsackCommented:
This shows you one method you can use to set the cursor position based on where the click occurred in the grid cell.  

1. Create a new VB6 project and set a reference to the MSFlexGrid component.
2. Add a new form to the project.
3. Add a MSFlexGrid (MSFlexGrid1) to the form.
4. Add a TextBox (Text1) to the form, and set the following properties on it:
        * .Appearance = 0-Flat
        * .BorderStyle = 0-None
        * .BackColor = &H80000018&
        * .Visible = False
5. Paste in the code from the below code snippet

Basically, here's how it works:

a) On the MouseUp event of the grid, record the X,Y coordinates of the click (in twips)
b) On the Click event of the grid, position and fill the text box and then display it...
c) Also on the Click event of the grid, set the cursor position in the text box based on where the click occurred

Option Explicit
 
Private mLastX As Single
Private mLastY As Single
 
Private Sub Form_Load()
    With MSFlexGrid1
        .Clear
        .FormatString = "|<Type 1 |<Type 2 |<Type 3 ;|Apples|Oranges|Bananas"
        .TextMatrix(1, 1) = "Pippin"
        .TextMatrix(1, 2) = "Jonathan"
        .TextMatrix(1, 3) = "Macintosh"
        .TextMatrix(2, 1) = "Valencia"
        .TextMatrix(2, 2) = "Navel"
        .TextMatrix(2, 3) = "Blood"
        .TextMatrix(3, 1) = "Valery"
        .TextMatrix(3, 2) = "Plantain"
        .TextMatrix(3, 3) = "Red Jamaica"
    End With
End Sub
 
Private Sub MSFlexGrid1_Click()
    Dim i As Long
    Dim p As Single
    Dim x As Single
    'position, fill and display text box
    With MSFlexGrid1
        If (.Row = 0) Or (.Col = 0) Then    'fixed rows/cols
            Text1.Visible = False
            Exit Sub
        Else
            Text1.Move .Left + .CellLeft + Me.TextWidth(" "), .Top + .CellTop + Screen.TwipsPerPixelX, .CellWidth - Me.TextWidth(" ") - Screen.TwipsPerPixelY, .CellHeight - Screen.TwipsPerPixelX
            Text1.Text = .Text
            Text1.Visible = True
            Text1.SetFocus
        End If
        x = mLastX - .CellLeft
    End With
    'set cursor position in textbox based on where the click occurred
    For i = 1 To Len(Text1.Text)
        p = Me.TextWidth(Left$(Text1.Text, i))
        If x < p Then
            Text1.Enabled = True
            Text1.SelStart = i - 1
            Text1.SetFocus
            Exit Sub
        End If
    Next i
    Text1.SelStart = Len(Text1.Text)
End Sub
 
Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    mLastX = x
    mLastY = y
End Sub

Open in new window

0
Anjinsan5Author Commented:
jmundsack:

Thanks again for the suggestion.  I am on the right path, however still a few issues remain.  
1.  Is it possible to position edit box directly over the cell being edited as opposed to randomly to the left of my form.
2.  When I click on a cell with a value to begin editing, the value becomes distorted (i.e. 123.456 becomes 123.4...)
3.  Also how do I implement cell navigation again as opposed to having to click on each cell to go through the grid
0
jmundsackCommented:
Well I must confess I'm no expert on this, but this person has a complete breakdown of all the issues involved with doing in-cell editing on a MSFlexGrid:

http://groups.google.com/group/microsoft.public.vb.general.discussion/msg/7da172d34a50a549

There are also a number of other posts that might help with the fine-tuning, if you go here:

http://groups.google.com/groups?as_q=msflexgrid+in-cell+editing&num=100&as_ugroup=*vb*&as_mind=1&as_minm=1&as_miny=1981&as_maxd=11&as_maxm=2&as_maxy=2008&safe=off

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anjinsan5Author Commented:
I will look at those articles.  Thanks alot for your assistance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.