Results 1 to 27 of 27

Thread: Loading Into an MSFlexGrid

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Loading Into an MSFlexGrid

    Alright, well I have a MSFlexGrid on the form, and I have a function set up that saves everything in the FlexGrid to a file in a certain format. How would I be able to load the saved file back into the FlexGrid? This is this format it saves in:
    Code:
    First Section's Data, Second Section's Data ,
    Here is the code used to save:
    Code:
    Public Function SaveFlex(FileName As String)
    'First, open the file
    Open FileName For Output As #1
    iRows = MSFlexGrid1.Rows
    iCols = MSFlexGrid1.Cols
    For i = 0 To iRows - 1
      For j = 0 To iCols - 1
        If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then
          Print #1, CCur(MSFlexGrid1.TextMatrix(i, j)); ",";
        Else
          Print #1, MSFlexGrid1.TextMatrix(i, j); ",";
        End If
      Next j
      Print #1, 'Finish the current row.
    Next i
    Close #1 'Close the file
    End Function
    Thanks in advaned!
    JLTL
    Last edited by jltl; Aug 2nd, 2005 at 10:26 PM.
    ~!~Computer Nerd~!~

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    You would use the same format to read it. Post the code that writes the data, and we'll try to help you read it back in. If you can't post the code in a thread, post your zipped project and all support files it needs.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Code:
    Public Function SaveFlex(FileName As String)
    'First, open the file
    Open FileName For Output As #1
    iRows = MSFlexGrid1.Rows
    iCols = MSFlexGrid1.Cols
    For i = 0 To iRows - 1
      For j = 0 To iCols - 1
        If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then
          Print #1, CCur(MSFlexGrid1.TextMatrix(i, j)); ",";
        Else
          Print #1, MSFlexGrid1.TextMatrix(i, j); ",";
        End If
      Next j
      Print #1, 'Finish the current row.
    Next i
    Close #1 'Close the file
    End Function
    There ya go ^^
    ~!~Computer Nerd~!~

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Anybody :'(
    ~!~Computer Nerd~!~

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    Something like this should work:

    VB Code:
    1. Public Function ReadFlex(FileName As String)
    2. Dim str As String, rowI() As String, colI() As String
    3. Open FileName For Input As #1
    4.   str = Input(LOF(1), #1) ' Read file into string
    5. Close #1 'Close the file
    6. rowI() = Split(str, vbCrLf) ' Split into rows
    7. For i = 0 To UBound(rowI) ' Determine number of rows
    8.   colI = Split(rowI(i), ",") ' Split each row into columns
    9.   For j = 0 To UBound(colI) ' Determine number of cols
    10.     MSFlexGrid1.TextMatrix(i, j) = colI(j) ' Assign col to grid
    11.   Next j
    12. Next i
    13. End Function

    Post back if you have any problems.
    I'll be around for a while.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Hmm. I get this:
    Code:
    Runtime Error '381':
    Subscript out of range
    and it points to this line:
    Code:
        MSFlexGrid1.TextMatrix(i, j) = colI(j) ' Assign col to grid
    Thanks in advance
    ~!~Computer Nerd~!~

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    Oh, you have an extra comma in there. Use:

    VB Code:
    1. For j = 0 To UBound(colI) - 1   ' Determine number of cols

    and please, no more PM's. The box gets filled too quickly.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Thanks again ^^ And sorry about the PM's I'm a desperate little programmer xD

    Edit: Worked PERFECTLY! Thanks a ton!
    ~!~Computer Nerd~!~

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Gar.. I'm getting that stupid Subscript out of range error again... What should I do -_-.
    ~!~Computer Nerd~!~

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    Where is it occurring? If you are getting all of the rows, you could subtracting 1 to see if there is an extra line in there. Did it ever work?

  11. #11
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Loading Into an MSFlexGrid

    Does your flexgrid have enough rows? If not use .additem to add rows. You could amend dglienna's code if you want to:

    Quote Originally Posted by dglienna
    VB Code:
    1. Public Function ReadFlex(FileName As String)
    2. Dim str As String, rowI() As String, colI() As String
    3. Open FileName For Input As #1
    4.   str = Input(LOF(1), #1) ' Read file into string
    5.  [COLOR=DarkRed][I] str = replace(str,",",vbTab) ' replaces comma with a tab character, so .additem works properly[/I][/COLOR]
    6. Close #1 'Close the file
    7. rowI() = Split(str, vbCrLf) ' Split into rows
    8. [COLOR=DarkRed][I]for z= MSFlexgrid1.rows to 1 step -1
    9.   MSFlexgrid1.removeitem 1
    10. next z[/I][/COLOR]
    11. For i = 0 To UBound(rowI) ' Determine number of rows
    12.   [COLOR=DarkRed][I]MSFlexgrid1.additem rowI(i)[/I][/COLOR]  
    13.  Next i
    14. End Function
    .additem will add a row, and uses the tab character to split the input row into columns. My approach deletes all the rows in the grid first, then populates it one row at a time

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Hmm... Now I get the error:
    "Can not remove last non-fixed row"
    >< FlexGrids are complicated :P
    ~!~Computer Nerd~!~

  13. #13
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Loading Into an MSFlexGrid

    Oh yeah! I remember that one. You either deal with the first line separately or (a simpler workaround, though I'm sure the real programmers would disapprove) set the rowheight of row 1 to 0 (hide it) and alter
    VB Code:
    1. for z= MSFlexgrid1.rows to 1 step -1
    2.   MSFlexgrid1.removeitem 1
    3. next z
    to

    VB Code:
    1. for z= MSFlexgrid1.rows to 2 step -1
    2.   MSFlexgrid1.removeitem 1
    3. next z

  14. #14
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Loading Into an MSFlexGrid

    Please post the code you are using. There are 100 reasons that you code could give you that error. We need to see what you are doing to be able to help you. If your code is too big post it as an attachment. Thanks.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  15. #15
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Loading Into an MSFlexGrid

    Oh yeah! I remember that one. You either deal with the first line separately or (a simpler workaround, though I'm sure the real programmers would disapprove) set the rowheight of row 1 to 0 (hide it) and alter
    VB Code:
    1. for z= MSFlexgrid1.rows to 1 step -1
    2.   MSFlexgrid1.removeitem 1
    3. next z
    to

    VB Code:
    1. for z= MSFlexgrid1.rows to [COLOR=DarkRed]2[/COLOR] step -1
    2.   MSFlexgrid1.removeitem [COLOR=DarkRed]2[/COLOR]
    3. next z

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    How would I set the Row Height of the first row to 0 though? Thank in advanced.
    ~!~Computer Nerd~!~

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    I did the exact opposite of the code that wrote the file. You had an extra comma that we took care of, now you might have an extra row.
    Stick with one way. We can't tell you how to fix it if you have two differnt routines. Post what you have.

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Here is both the Save Flex and the Load Flex:
    VB Code:
    1. Public Function LoadFlex(FileName As String)
    2. Dim str As String, rowI() As String, colI() As String
    3. Open FileName For Input As #1
    4.   str = Input(LOF(1), #1) ' Read file into string
    5.   str = Replace(str, ",", vbTab) ' replaces comma with a tab character, so .additem works properly
    6. Close #1 'Close the file
    7. rowI() = Split(str, vbCrLf) ' Split into rows
    8. For z = MSFlexGrid1.Rows To 2 Step -1
    9.   MSFlexGrid1.RemoveItem 1
    10. Next z
    11. For i = 0 To UBound(rowI) ' Determine number of rows
    12.   MSFlexGrid1.AddItem rowI(i)
    13.   Next i
    14. End Function
    15.  
    16. Public Function SaveFlex(FileName As String)
    17. 'First, open the file
    18. Open FileName For Output As #1
    19. iRows = MSFlexGrid1.Rows
    20. iCols = MSFlexGrid1.Cols
    21. For i = 0 To iRows - 1
    22.   For j = 0 To iCols - 1
    23.     If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then
    24.       Print #1, CCur(MSFlexGrid1.TextMatrix(i, j)); ",";
    25.     Else
    26.       Print #1, MSFlexGrid1.TextMatrix(i, j); ",";
    27.     End If
    28.   Next j
    29.   Print #1, 'Finish the current row.
    30. Next i
    31. Close #1 'Close the file
    32. End Function
    Last edited by jltl; Aug 4th, 2005 at 03:55 PM.
    ~!~Computer Nerd~!~

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    I don't like that method. You don't have to clear out the flexgrid when writing to it. If you are filling in all cells, then it will erase what was in there.

    What line is the error occurring on?

  20. #20

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Then I'll use your method, which worked right off the bat, but then stopped working. It pointed to the same line as before.

    VB Code:
    1. MSFlexGrid1.TextMatrix(i, j) = colI(j)
    That line
    ~!~Computer Nerd~!~

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Loading Into an MSFlexGrid

    How many rows are in the grid? How many columns?
    Hover the mouse over I, and J to see what the values are. There may be an extra line of code, so you would just -1 from .rows in the FOR statement, just as we did with the J For statement for the .cols

  22. #22

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    In VB it says there are 2 rows and 2 collumns. But when I open it and it loads the information from the Access Database, it changes to however many values there is in the database.
    ~!~Computer Nerd~!~

  23. #23
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Loading Into an MSFlexGrid

    You can use .cols to find the max number of cols and .rows to find the max number of rows.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  24. #24

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Quote Originally Posted by eyeRmonkey
    You can use .cols to find the max number of cols and .rows to find the max number of rows.
    In newbie terms? o.o

    Edit: nvm, figured it out =P

    Edit Again: Ok, VERY wierd. With some lists, it works, but with others it doesn't?
    Last edited by jltl; Aug 4th, 2005 at 04:24 PM.
    ~!~Computer Nerd~!~

  25. #25
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Loading Into an MSFlexGrid

    Quote Originally Posted by jltl
    Edit Again: Ok, VERY wierd. With some lists, it works, but with others it doesn't?
    Once again, if you want help, we need a better idea of whats going wrong. Does an error occur? If so what does it say and what line does it highlight? If not then tell us how it "doesn't work." Thanks.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  26. #26

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Loading Into an MSFlexGrid

    Quote Originally Posted by eyeRmonkey
    Once again, if you want help, we need a better idea of whats going wrong. Does an error occur? If so what does it say and what line does it highlight? If not then tell us how it "doesn't work." Thanks.
    I already posted the error and the line a few posts up.

    Edit: I'm not going to use the MSFlexGrid. For what I'm doing, there is way to much going on. I'll just use a simpler listbox :P Thanks for all your help though!
    ~!~Computer Nerd~!~

  27. #27
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Loading Into an MSFlexGrid

    Quote Originally Posted by jltl
    I already posted the error and the line a few posts up.
    Ok, well you said you figured it out, then said it wasn't working again so I assumed it was a new error.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width