|
-
March 10th, 2012, 08:59 AM
#1
Remove Blanks from a Grid(DataWidget)
Hello, I have a grid in which I need to be able to manipulate. If there are any blanks, then these need to be moved to the bottom and all the numbers below it should be moved up.
I am using VB6

Current Grid (blanks are shown here as asterisk)
5 6 1 4 9 3
7 8 3 * 1 5
9 * 5 * 3 *
1 2 7 6 5 9
Need it to be:
5 6 1 4 9 3
7 8 3 6 1 5
9 2 5 * 3 9
1 * 7 * 5 *
I need to read the grid column by column. If a blank is found, I need to move everything up and place the blanks at the end of the column.
For example in the 2nd column I have:
6
8
*
2
I need this to become
6
8
2
*
There might be more than one blank and more than one blank in a row. For example in the 4th column I have:
4
*
*
6
I need this to become
4
6
*
*
As an example of how the grid is named. Here is a small code section that shows how the program will swap numbers. For example in column 1 if I change the 7 to a 1, the grid changes from:
5
7
9
1
To
5
1
9
7
Code:
For X = FrmMain.grid.Rows - 1 To 0 Step -1
FrmMain.grid.Row = X
If FrmMain.grid.Columns(UpdateCol).Value = newValue And FrmMain.grid.Row <> rRow Then
FrmMain.grid.Columns(UpdateCol).Value = oldValue
Exit For
End If
Next
I am showing this code just so you have the naming convention for the grid and how I have been coding other areas.
Thank in advance for reading this post, and any help is greatly appreciated.
-
March 10th, 2012, 02:59 PM
#2
Re: Remove Blanks from a Grid(DataWidget)
Do it the easy way.
Create a NEW EMPTY TABLE, and fill it line-by-line until it's right.
Then, copy that table to the output table as the RESULT!
-
March 10th, 2012, 09:02 PM
#3
Re: Remove Blanks from a Grid(DataWidget)
thanks for the quick response and advice.
This is done in 'real time' so as you change a value that particular column is updated. It does not happen all at once. So is it still best to re-do the entire grid each time?
I am still learning and this is someone else's code..
Could you provide me a code sample?
I really appreciate your help.
thanks
Last edited by kpierce; March 10th, 2012 at 09:05 PM.
-
March 11th, 2012, 11:37 AM
#4
Re: Remove Blanks from a Grid(DataWidget)
Back to VB6? This reads a file, and lines up the TIME field.
Code:
Option Explicit
Private Sub Form_Load()
Dim str(247) As String * 10 ' Set Data Type & Size as ARRAY()
Dim strOut As String ' Set Output file
Dim x As Integer, dt As Date ' Set Variables
dt = #8:00:00 AM# ' Date Literal
For x = 0 To 47 ' Add spaces to 48 fields
str(x) = Space(8)
Next x
' Open "c:\temp\schedule.txt" For Output As #1
For x = 0 To 47
If x Mod 2 = 0 Then
RSet str(x) = Format(dt, "HH:MM AMPM")
Else
RSet str(x) = Format(dt, ":n") & " "
End If
strOut = str(x) ' & vbCrLf
' Print #1, strOut
Debug.Print strOut
dt = DateAdd("n", 30, dt)
Next x
' Close #1
Unload Me
End Sub
RSet() right-aligns a string, while LSet() left-aligns it
-
March 11th, 2012, 02:06 PM
#5
Re: Remove Blanks from a Grid(DataWidget)
Well, that doesn't look helpful at all. 
But it seems, the OP wasn't specific enough for "Literal" David. He simply asked for a code sample and that is what he got. 
First, we should know what kind of grid it is you are using.
What is it? Some kind of Sudoku solver?
-
March 11th, 2012, 03:56 PM
#6
Re: Remove Blanks from a Grid(DataWidget)
Yes, I guess I got what I asked for.
Thanks for the quick response.
This is a Sheridan DataWidget gird.
I will not be adding rows or columns, just when a value is 'blanked' out everything in the column is moved up and the blank is placed at the bottom of that column.
The user can type directly into the grid.
So when the user tabs(exists the field) and it is BLANK I simply want to move everything up. (kind of like one of those old-timey sliding puzzles)
so for example, the grid currently sits as:
1 4 5
2 4 3
9 5 7
4 5 6
Let's say the user is currently in Column 1 and Row 3 (value is 9) and they space/delete the 9.
The grid then is updated to the following:
1 4 5
2 4 3
4 5 7
* 5 6
The 9 was deleted so the 4 was moved up and the blank was placed at the bottom (blank is represented by an *).
A small section of code from the program so you know how the grid is named referenced is as follows:
Code:
For X = FrmMain.grid.Rows - 1 To 0 Step -1
FrmMain.grid.Row = X
If FrmMain.grid.Columns(UpdateCol).Value = newValue And FrmMain.grid.Row <> rRow Then
FrmMain.grid.Columns(UpdateCol).Value = oldValue
Exit For
End If
Next
I hope that makes sense.
thanks.
Last edited by kpierce; March 11th, 2012 at 04:06 PM.
-
March 12th, 2012, 06:59 AM
#7
Re: Remove Blanks from a Grid(DataWidget)
To achieve what you want we'd have to know
a) how an individual cell can be addressed
b) When a cell was left.
E.g.:
for b) in MSHFlexGrid control we have a LeaveCell() event which fires when the cursor leaves a certain cell. We can put the code to move up all consecutive cells in this column into this event handler.
for a) in MSHFlexGrid we'd have the .TextMatrix() property which allows to access all cells as a twodimensional text array like
Grid.TextMatrix(Row, Col) = "new contents"
Based on these assumptions a piece of code to move up all following cells in a column could look like this:
Code:
Private Sub Grid_LeaveCell()
Dim r%, c%, i%
r = Grid.Row
c = Grid.Col
For i = r+1 To Grid.Rows - 1
Grid.TextMatrix(i - 1, c) = Grid.TextMatrix(i, c)
Next
End Sub
You might have to look for the apropriate properties and events in you sheridan control which is not known to me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|