1) MSHFLEXGRID的編輯.
關(guān)于MSHFLEXGRID的編輯,很多書都有介紹.一般都是用一個(gè)TEXTBOX作為的輸入框,通過移動(dòng)TEXTBOX來達(dá)到類似于EXCEL的編輯功能.很多書介紹的方法是在MOUSEDOWN或CLICK事件中移動(dòng)TEXTBOX,然后,再在LeaveCell事件中寫入.
本文的方法與其有類似之處,但亦有小許不同,主要在寫入網(wǎng)格時(shí),在TEXTBOX的Change事件中寫入.
2)網(wǎng)格內(nèi)容的保存與加載
對(duì)于網(wǎng)格的保存,一般人喜歡使用.Clip屬性,將整個(gè)網(wǎng)格一次性地寫入一個(gè)文件中,當(dāng)然,在文件不大時(shí),這當(dāng)然是一個(gè)好辦法.但是,當(dāng)網(wǎng)格達(dá)到幾千行幾萬行時(shí),這個(gè)方法好象不是很好.(各位如果有興趣的話,可以試試下面的程序)
‘將網(wǎng)格設(shè)置成5000*12,然后用隨機(jī)數(shù)填充網(wǎng)格.然后,調(diào)用下面程序
Private Sub Command4_Click()
Dim msgStr As String
Dim FileID As Long
Dim T1 As Date
Dim T2 As Date
T1 = Timer()
With MSHFlexGrid1
.Row = 0
.Col = 0
.RowSel = .Rows - 1
.ColSel = .Cols - 1
FileID = FreeFile
msgStr = .Clip
Open "C:\LX.TXT" For Output As #FileID
Print #FileID, msgStr
Close #FileID
End With
T2 = Timer()
MsgBox T2 - T1
End Sub
反正我的感覺是:好象死機(jī)一般,要過一分多鐘后計(jì)算機(jī)才能反應(yīng)過來(實(shí)測(cè)是82.5秒左右,我的計(jì)算機(jī)是:AMD2500+,512M內(nèi)存).
為什么一次性的寫入會(huì)如此的慢呢?這大概是有的人想不到的地方.其實(shí),這跟VB處理字符串的機(jī)制有關(guān),如果處理5K的字符串要一秒的話,那么,處理30K的字符串絕不是處理5K的6倍,而是長(zhǎng)得多.這種關(guān)系幾乎是呈某種幾何級(jí)數(shù)的關(guān)系.
明白了VB原來處理大字符串的效率原來是這么底.那么,解決的辦法自然就有了.就是一個(gè)字:拆,將大拆小將會(huì)大大地加快處理字符串的速度.
所以,下面的網(wǎng)格的保存函數(shù)的主要思想就將網(wǎng)格中的數(shù)據(jù)分步保存,每一次保存一小部分.直到整個(gè)網(wǎng)格保存完成.當(dāng)然,其中還有一些細(xì)小的技巧,例如:保存時(shí)將先將網(wǎng)格中的行,列,固定行,固定列的總數(shù)保存,然后,保存各列的寬度,再然后正式保存數(shù)據(jù).這都是為了加載的方便與快捷作了一定的處理.(參考下面的程序) 問題點(diǎn)數(shù):20、回復(fù)次數(shù):17Top
Option Explicit
Dim m_Row As Long
Dim m_Col As Long
Private Sub Command3_Click()
'填充網(wǎng)格
Dim R As Long
Dim C As Long
For R = 0 To MSHFlexGrid1.Rows - 1
For C = 0 To MSHFlexGrid1.Cols - 1
MSHFlexGrid1.TextMatrix(R, C) = R & C
Next
Next
End Sub
Private Sub Form_Load()
With MSHFlexGrid1
Text1.Visible = False
.RowHeight(-1) = 285
'設(shè)定網(wǎng)格是5000行.12列.
.Rows = 5000: .Cols = 12
End With
End Sub
'保存文件
Private Sub Command1_Click()
Call SaveFile(MSHFlexGrid1, "c:\kk.grd")
End Sub
'加載文件
Private Sub Command2_Click()
Call LoadFile(MSHFlexGrid1, "c:\kk.grd")
End Sub
Private Sub MSHFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Text1.Visible = False
With MSHFlexGrid1
m_Row = .MouseRow
m_Col = .MouseCol
If m_Row < .FixedRows Then m_Row = .FixedRows
If m_Col < .FixedCols Then m_Col = .FixedCols
.Row = m_Row: .Col = m_Col
Text1.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth, .CellHeight
Text1.Text = .Text
Text1.Visible = True
Text1.SetFocus
End With
End Sub
Private Sub Text1_Change()
With MSHFlexGrid1
.TextMatrix(m_Row, m_Col) = Text1
End With
End Sub
'//**以下是相應(yīng)的功能函數(shù)
'
'加載一個(gè)文件到表格.
'函數(shù):LoadFileToGrid
'參數(shù):MsgObj Mshfelxgrid控件名,FileName 加載的文件名
'返回值:=True 成功.=True 失敗.
Public Function LoadFile(MsgObj As Control, FileName As String) As Long
Dim InputID As Long, FileID As Long
Dim EndRow As Long, DltAdd As Long
Dim AddFlag As Boolean
Dim KeyTab As String, KeyEnter As String
Dim FixedRows As Long, FixedCols As Long
Dim GridInput As String, AddSum As String, RowColMax() As String
Dim GridColMax As Long, GridRowMax As Long
Dim OleRow As Long, OleCol As Long
Dim SumFmtStr As String
Dim DltCol As Long
On Error Resume Next
With MsgObj
.Redraw = False
Err.Clear: SetAttr FileName, 0
If Err.Number <> 0 Then '如果文件不存在
Err.Clear
Call SaveFile(MsgObj, FileName)
.Redraw = True
Exit Function
End If
KeyTab = Chr$(vbKeyTab): KeyEnter = Chr$(13)
InputID = 0: AddSum = ""
AddFlag = False: DltAdd = 25: DltCol = 1
.Redraw = False: .FixedRows = 0: .FixedCols = 0
FileID = FreeFile
Open FileName For Input As #FileID
Do While Not EOF(FileID) ' 循環(huán)至文件尾。
Line Input #FileID, GridInput
If InputID <= 1 Then
'取出總行數(shù)和總列數(shù),以及各列的寬度.
If InputID = 0 Then
RowColMax = Split(GridInput, "|")
GridRowMax = CLng("0" & RowColMax(0)): GridColMax = CLng("0" & RowColMax(1))
If CLng("0" & RowColMax(0)) < 2 Then GridRowMax = 1
If CLng("0" & RowColMax(1)) < 2 Then GridColMax = 1
.Rows = GridRowMax: .Cols = GridColMax
Else
SumFmtStr = GridInput '格式字符串.
End If
Else
If AddFlag Then
AddSum = AddSum & KeyEnter & GridInput
Else
AddSum = GridInput: AddFlag = True
End If
If (InputID - DltCol) Mod DltAdd = 0 Then
.Row = InputID - DltAdd - DltCol: .Col = 0
.RowSel = InputID - 1 - DltCol: .ColSel = GridColMax - 1
.Clip = AddSum: AddSum = ""
EndRow = InputID - DltCol: AddFlag = False
End If
End If
InputID = InputID + 1
Loop
If (InputID - DltCol) - EndRow > 1 Then
.Row = EndRow: .Col = 0
.RowSel = GridRowMax - 1
.ColSel = GridColMax - 1
.Clip = AddSum
AddSum = ""
End If
Close #FileID
Call FormatGrid(MsgObj, SumFmtStr)
.FixedRows = CLng("0" & RowColMax(2)): .FixedCols = CLng("0" & RowColMax(3))
.Redraw = True
.Row = .FixedRows
.Col = .FixedCols
.RowSel = .FixedRows
.ColSel = .FixedCols
End With
End Function
'
'保存表格數(shù)據(jù)
'函數(shù):SaveFile
'參數(shù):MsgObj Mshfelxgrid控件名,FileName 加載的文件名
'返回值:=True 成功.=True 失敗.
Public Function SaveFile(MsgObj As Control, FileName As String) As Boolean
'/保存文件
Dim FileID As Long, ConTents As String
Dim A As Long, B As Long
Dim RowMax As Long, ColMax As Long
Dim FixRows As Long, FixCols As Long
Dim OleRow As Long, OleCol As Long
Dim SFmtStr As String
Dim strColWidth As String
On Error Resume Next
With MsgObj
.Redraw = False
Next
聯(lián)系客服