Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents Seiji Mori Presents

MSFlexGrid設定例

'フォームに、MSFlexGrid1、テキストボックスText_Editを配置する
 
'メニューエディタで
'キャプション:編集、名前:Edit
'(その下に)  キャプション:切り取り、名前:Edit_Cut
'       キャプション:コピー、名前:Edit_Copy
'       キャプション:全て選択、名前:Edit_Select_All
'       キャプション:貼り付け、名前:Edit_Paste
 
'テキストボックスのポップアップメニュー表示のためのAPI
Private Declare Function LockWindowUpdate Lib "user32" _
     (ByVal hwndLock As Long) As Long
 
Dim Nyuuryoku As Boolean
Dim GridTemp As String
 

Private Sub Form_Load()
  Dim i As Integer
  Dim j As Integer
  
  Me.Show
 
  With MSFlexGrid1
    .Rows = 11   '行数(固定行を含む)
    .Cols = 11   '列数(固定列を含む)
    .Row = 1    '最初のセルの行番号
    .Col = 1    '最初のセルの列番号
    .RowSel = 1   '最後のセルの行番号
    .ColSel = 1   '最後のセルの列番号
    .FixedCols = 1 '固定列の数
    .FixedRows = 1 '固定行の数
    .AllowUserResizing = 3 '行列幅を自由に変更できる
    .ColWidth(0) = (Int(Log(.Rows) / Log(10)) + 1) * 120 + 80 '列幅指定 (固定列)
    .RowHeight(-1) = 300 '行幅指定(-1で全部を変更)
    
  End With
  
  '固定行にナンバーをふる
  With MSFlexGrid1
    For i = 1 To .Rows - 1
      .TextMatrix(i, 0) = i
    Next i
    For i = 1 To .Cols - 1
      .TextMatrix(0, i) = i
    Next i
  End With
  
  MSFlexGrid1_EnterCell
  
End Sub
 
Private Sub MSFlexGrid1_EnterCell()
'セルがアクティブになったときの処理
'テキストボックスの位置とサイズを合わせ、セルの文字をテキストボックスに記入する
 
  With Text_Edit
    .Text = ""
    
    'テキストボックスの位置とサイズを合わせる
    .Top = MSFlexGrid1.Top + MSFlexGrid1.CellTop
    .Left = MSFlexGrid1.Left + MSFlexGrid1.CellLeft
    .Height = MSFlexGrid1.CellHeight
    .Width = MSFlexGrid1.CellWidth
    
    .Text = MSFlexGrid1.Text
    .Visible = True
    .Enabled = True
    .SetFocus
    
    If .Text <> "" Then
      Nyuuryoku = True
    End If
    
  End With
  
End Sub
 

Private Sub MSFlexGrid1_LeaveCell()
'カレントセルが移動したとき
 
  MSFlexGrid1.Text = ""
  MSFlexGrid1.Text = Text_Edit.Text
  
End Sub
 
Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
 
  'Deleteキーが押された場合
  If KeyCode = vbKeyDelete Then
    MSFlexGrid1.FillStyle = flexFillRepeat
    MSFlexGrid1.Text = ""
    Text_Edit.Text = ""
    MSFlexGrid1.FillStyle = flexFillSingle
  End If
  
End Sub
 
Private Sub MSFlexGrid1_Scroll()
  
  Text_Edit.Visible = False
  
End Sub
 

Private Sub Text_Edit_KeyDown(KeyCode As Integer, Shift As Integer)
 
  Dim dRow As Long
  Dim dCol As Long
  Dim Houkou As Integer
  Dim MyLast As Long
  
  If Shift = 3 Then 'Shift + Ctrl
      If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Or KeyCode = vbKeyRight _
        Or KeyCode = vbKeyLeft Then
        
        If KeyCode = vbKeyDown Then
          Houkou = 1
          MyLast = MSFlexGrid1.ColSel
        ElseIf KeyCode = vbKeyUp Then
          Houkou = 2
          MyLast = MSFlexGrid1.ColSel
        ElseIf KeyCode = vbKeyRight Then
          Houkou = 3
          MyLast = MSFlexGrid1.RowSel
        ElseIf KeyCode = vbKeyLeft Then
          Houkou = 4
          MyLast = MSFlexGrid1.RowSel
        End If
        
        ShiftCtrlHoukou Houkou, MyLast
        
      End If
  ElseIf Shift = 1 Then 'Shift
    If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Or KeyCode = vbKeyRight _
      Or KeyCode = vbKeyLeft Then
  
      Text_Edit.Visible = False
    End If
  ElseIf Shift = 2 Then 'Ctrl
    If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Or KeyCode = vbKeyRight _
      Or KeyCode = vbKeyLeft Then
      
      If KeyCode = vbKeyDown Then
        Houkou = 1
      ElseIf KeyCode = vbKeyUp Then
        Houkou = 2
      ElseIf KeyCode = vbKeyRight Then
        Houkou = 3
      ElseIf KeyCode = vbKeyLeft Then
        Houkou = 4
      End If
      
      ControlHoukou Houkou
      
    End If
 
  ElseIf KeyCode = vbKeyReturn Or KeyCode = vbKeyDown Then
    dRow = MSFlexGrid1.Row + 1
    If dRow < MSFlexGrid1.Rows Then
      MSFlexGrid1.Row = dRow
    End If
  ElseIf KeyCode = vbKeyUp Then
    dRow = MSFlexGrid1.Row - 1
    If dRow > 0 Then
      MSFlexGrid1.Row = dRow
    End If
  ElseIf KeyCode = vbKeyRight Then
    dCol = MSFlexGrid1.Col + 1
    If dCol < MSFlexGrid1.Cols Then
      MSFlexGrid1.Col = dCol
    End If
  ElseIf KeyCode = vbKeyLeft Then
    dCol = MSFlexGrid1.Col - 1
    If dCol > 0 Then
      MSFlexGrid1.Col = dCol
    End If
  ElseIf KeyCode = vbKeyDelete Then
    Text_Edit.Visible = False
    MSFlexGrid1.FillStyle = flexFillRepeat
    MSFlexGrid1.Text = ""
    Text_Edit.Text = ""
    MSFlexGrid1.FillStyle = flexFillSingle
  ElseIf Nyuuryoku = True Then
    Text_Edit.Text = ""
    Nyuuryoku = False
    
  End If
    
End Sub
 
Sub ControlHoukou(Houkou As Integer)
  
  Dim i As Long
  
  MSFlexGrid1.Text = ""
  MSFlexGrid1.Text = Text_Edit.Text
  
  With MSFlexGrid1
  
    Select Case Houkou
      Case 1 'Ctrl + ↓
        If .Row = .Rows - 1 Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row + 1, .Col) = "" Then
            For i = .Row + 1 To .Rows - 1
              If .TextMatrix(i, .Col) <> "" Then
                .Row = i
                Exit Sub
              ElseIf i = .Rows - 1 Then
                .Row = i
              End If
            Next i
          
          Else
            For i = .Row + 1 To .Rows - 1
              If .TextMatrix(i, .Col) = "" Then
                .Row = i - 1
                Exit Sub
              ElseIf i = .Rows - 1 Then
                .Row = i
              End If
            Next i
          End If
        ElseIf .Text = "" Then
          For i = .Row + 1 To .Rows - 1
            If .TextMatrix(i, .Col) <> "" Then
              .Row = i
              Exit Sub
            ElseIf i = .Rows - 1 Then
              .Row = i
            End If
          Next i
        End If
      Case 2 'Ctrl + ↑
        If .Row = .FixedRows Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row - 1, .Col) = "" Then
             For i = .Row - 1 To .FixedRows Step -1
              If .TextMatrix(i, .Col) <> "" Then
                .Row = i
                Exit Sub
              ElseIf i = .FixedRows Then
                .Row = i
              End If
            Next i
          Else
            For i = .Row - 1 To .FixedRows Step -1
              If .TextMatrix(i, .Col) = "" Then
                .Row = i + 1
                Exit Sub
              ElseIf i = .FixedRows Then
                .Row = i
              End If
            Next i
          End If
          
        ElseIf .Text = "" Then
          For i = .Row - 1 To .FixedRows Step -1
            If .TextMatrix(i, .Col) <> "" Then
              .Row = i
              Exit Sub
            ElseIf i = .FixedRows Then
              .Row = i
            End If
          Next i
        End If
      Case 3 'Ctrl + →
        If .Col = .Cols - 1 Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row, .Col + 1) = "" Then
            For i = .Col + 1 To .Cols - 1
              If .TextMatrix(.Row, i) <> "" Then
                .Col = i
                Exit Sub
              ElseIf i = .Cols - 1 Then
                .Col = i
              End If
            Next i
          Else
            For i = .Col + 1 To .Cols - 1
              If .TextMatrix(.Row, i) = "" Then
                .Col = i - 1
                Exit Sub
              ElseIf i = .Cols - 1 Then
                .Col = i
              End If
            Next i
          
          End If
          
        ElseIf .Text = "" Then
          For i = .Col + 1 To .Cols - 1
            If .TextMatrix(.Row, i) <> "" Then
              .Col = i
              Exit Sub
            ElseIf i = .Cols - 1 Then
              .Col = i
            End If
          Next i
        End If
      Case 4 'Ctrl + ←
        If .Col = .FixedCols Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row, .Col - 1) <> "" Then
            For i = .Col - 1 To .FixedCols Step -1
              If .TextMatrix(.Row, i) = "" Then
                .Col = i + 1
                Exit Sub
              ElseIf i = .FixedCols Then
                .Col = i
              End If
            Next i
          Else
            For i = .Col - 1 To .FixedCols Step -1
              If .TextMatrix(.Row, i) <> "" Then
                .Col = i
                Exit Sub
              ElseIf i = .FixedCols Then
                .Col = i
              End If
            Next i
          End If
        ElseIf .Text = "" Then
          For i = .Col - 1 To .FixedCols Step -1
            If .TextMatrix(.Row, i) <> "" Then
              .Col = i
              Exit Sub
            ElseIf i = .FixedCols Then
              .Col = i
            End If
          Next i
        End If
    End Select
    
  End With
          
            
End Sub
 
Sub ShiftCtrlHoukou(Houkou As Integer, MyLast As Long)
   
  Dim i As Long
  Dim FirstRow As Long
  Dim FirstCol As Long
  Dim LastRow As Long
  Dim LastCol As Long
  
  MSFlexGrid1.Text = ""
  MSFlexGrid1.Text = Text_Edit.Text
  
  With MSFlexGrid1
  
    Select Case Houkou
      Case 1 'Ctrl + ↓
        FirstRow = .Row
        .Row = .RowSel
        LastCol = MyLast
        If .Row = .Rows - 1 Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row + 1, .Col) = "" Then
            For i = .Row + 1 To .Rows - 1
              If .TextMatrix(i, .Col) <> "" Then
                .Row = FirstRow
                .RowSel = i
                .ColSel = LastCol
                Exit Sub
              ElseIf i = .Rows - 1 Then
                .Row = FirstRow
                .RowSel = i
                .ColSel = LastCol
              End If
            Next i
          
          Else
            For i = .Row + 1 To .Rows - 1
              If .TextMatrix(i, .Col) = "" Then
                .Row = FirstRow
                .RowSel = i - 1
                .ColSel = LastCol
                Exit Sub
              ElseIf i = .Rows - 1 Then
                .Row = FirstRow
                .RowSel = i
                .ColSel = LastCol
              End If
            Next i
          End If
        ElseIf .Text = "" Then
          For i = .Row + 1 To .Rows - 1
            If .TextMatrix(i, .Col) <> "" Then
              .Row = FirstRow
              .RowSel = i
              .ColSel = LastCol
              Exit Sub
            ElseIf i = .Rows - 1 Then
              .Row = FirstRow
              .RowSel = i
              .ColSel = LastCol
            End If
          Next i
        End If
      Case 2 'Ctrl + ↑
        FirstRow = .Row
        .Row = .RowSel
        LastCol = MyLast
        If .Row = .FixedRows Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row - 1, .Col) = "" Then
             For i = .Row - 1 To .FixedRows Step -1
              If .TextMatrix(i, .Col) <> "" Then
                .Row = FirstRow
                .RowSel = i
                .ColSel = LastCol
                Exit Sub
              ElseIf i = .FixedRows Then
                .Row = FirstRow
                .RowSel = i
                .ColSel = LastCol
              End If
            Next i
          Else
            For i = .Row - 1 To .FixedRows Step -1
              If .TextMatrix(i, .Col) = "" Then
                .Row = FirstRow
                .RowSel = i + 1
                .ColSel = LastCol
                Exit Sub
              ElseIf i = .FixedRows Then
                .Row = FirstRow
                .RowSel = i
                .ColSel = LastCol
              End If
            Next i
          End If
          
        ElseIf .Text = "" Then
          For i = .Row - 1 To .FixedRows Step -1
            If .TextMatrix(i, .Col) <> "" Then
              .Row = FirstRow
              .RowSel = i
              .ColSel = LastCol
              Exit Sub
            ElseIf i = .FixedRows Then
              .Row = FirstRow
              .RowSel = i
              .ColSel = LastCol
           End If
          Next i
        End If
      Case 3 'Ctrl + →
        FirstCol = .Col
        .Col = .ColSel
        LastRow = MyLast
        If .Col = .Cols - 1 Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row, .Col + 1) = "" Then
            For i = .Col + 1 To .Cols - 1
              If .TextMatrix(.Row, i) <> "" Then
                .Col = FirstCol
                .ColSel = i
                .RowSel = LastRow
                Exit Sub
              ElseIf i = .Cols - 1 Then
                .Col = FirstCol
                .ColSel = i
                .RowSel = LastRow
              End If
            Next i
          Else
            For i = .Col + 1 To .Cols - 1
              If .TextMatrix(.Row, i) = "" Then
                .Col = FirstCol
                .ColSel = i - 1
                .RowSel = LastRow
                Exit Sub
              ElseIf i = .Cols - 1 Then
                .Col = FirstCol
                .ColSel = i
                .RowSel = LastRow
              End If
            Next i
          
          End If
          
        ElseIf .Text = "" Then
          For i = .Col + 1 To .Cols - 1
            If .TextMatrix(.Row, i) <> "" Then
              .Col = FirstCol
              .ColSel = i
              .RowSel = LastRow
              Exit Sub
            ElseIf i = .Cols - 1 Then
              .Col = FirstCol
              .ColSel = i
              .RowSel = LastRow
            End If
          Next i
        End If
      Case 4 'Ctrl + ←
        FirstCol = .Col
        .Col = .ColSel
        LastRow = MyLast
        If .Col = .FixedCols Then
          Exit Sub
        ElseIf .Text <> "" Then
          If .TextMatrix(.Row, .Col - 1) <> "" Then
            For i = .Col - 1 To .FixedCols Step -1
              If .TextMatrix(.Row, i) = "" Then
                .Col = FirstCol
                .ColSel = i + 1
                .RowSel = LastRow
                Exit Sub
              ElseIf i = .FixedCols Then
                .Col = FirstCol
                .ColSel = i
                .RowSel = LastRow
              End If
            Next i
          Else
            For i = .Col - 1 To .FixedCols Step -1
              If .TextMatrix(.Row, i) <> "" Then
                .Col = FirstCol
                .ColSel = i
                .RowSel = LastRow
                Exit Sub
              ElseIf i = .FixedCols Then
                .Col = FirstCol
                .ColSel = i
                .RowSel = LastRow
              End If
            Next i
          End If
        ElseIf .Text = "" Then
          For i = .Col - 1 To .FixedCols Step -1
            If .TextMatrix(.Row, i) <> "" Then
              .Col = FirstCol
              .ColSel = i
              .RowSel = LastRow
              Exit Sub
            ElseIf i = .FixedCols Then
              .Col = FirstCol
              .ColSel = i
              .RowSel = LastRow
            End If
          Next i
        End If
    End Select
    
  End With
            
End Sub
 

Private Sub Text_Edit_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'ビット 0 がマウスの左ボタン、ビット 1 が右ボタン、ビット 2 が中央ボタンに対応
 
  Text_Edit.Visible = False
    
  'マウスの右クリックで、ポップアップメニュー表示
  If Button = vbRightButton Then
    LockWindowUpdate Text_Edit.hWnd
    Text_Edit.Enabled = False
    DoEvents
    PopupMenu Edit
    Text_Edit.Enabled = True
    LockWindowUpdate 0&
   End If
    
End Sub
 
Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Text_Edit.Visible = False Then
    Text_Edit.Visible = True
    Text_Edit.SetFocus
  End If
  
  If Button = vbRightButton Then
    With MSFlexGrid1
      If .Row = .RowSel And .Col = .ColSel Then
      'セルが複数選択してない場合
        If .MouseRow > .FixedRows - 1 And .MouseCol > .FixedCols - 1 Then
          .Row = .MouseRow
          .Col = .MouseCol
        Else  '固定行の場合
          Exit Sub
        End If
        
        PopupMenu Edit
      Else
      'セルが複数選択してある場合
        If .Row <= .MouseRow And .MouseRow <= .RowSel _
          And .Col <= .MouseCol And .MouseCol <= .ColSel Then
        'マウスが範囲内にある場合
        
          PopupMenu Edit
          
        Else
        'マウスが範囲外にある場合
          If .MouseRow > .FixedRows - 1 And .MouseCol > .FixedCols - 1 Then
            .Row = .MouseRow
            .Col = .MouseCol
          Else
            Exit Sub
          End If
          
          PopupMenu Edit
        End If
        
      End If
    End With
End If
  
End Sub
 
Private Sub Edit_Cut_Click()
  Dim TempText As String
  
  TempText = MSFlexGrid1.Clip
  Clipboard.Clear
  Clipboard.SetText TempText
  MSFlexGrid1.FillStyle = flexFillRepeat
  MSFlexGrid1.Text = ""
  Text_Edit.Text = ""
  MSFlexGrid1.FillStyle = flexFillSingle

  'MSFlexGridからコピーの場合、一時保存
  GridTemp = TempText
 
End Sub
 
Private Sub Edit_Copy_Click()
'コピー
 
  Dim TempText As String
  
  TempText = MSFlexGrid1.Clip
  Clipboard.Clear
  Clipboard.SetText TempText
  
  'MSFlexGridからコピーの場合、一時保存
  GridTemp = TempText
  
End Sub
 
Private Sub Edit_Select_All_Click()
  '全セルを選択
  With MSFlexGrid1
    .Row = .FixedRows
    .Col = .FixedCols
    .RowSel = .Rows - 1
    .ColSel = .Cols - 1
  End With
  
End Sub

 
Private Sub Edit_Paste_Click()
'貼り付け
 
  Dim TempText As String
  Dim IRow As Integer
  Dim ICol As Integer
  
  IRow = 1
  ICol = 1
 
  TempText = Clipboard.GetText
  
  'データの行数、列数を得る
  Search TempText, IRow, ICol
  
  '範囲を指定し、貼り付け
  With MSFlexGrid1
    If .Row + IRow <= .Rows And .Col + ICol <= .Cols Then
      .RowSel = .Row + IRow - 1
      .ColSel = .Col + ICol - 1
      .Clip = TempText
      Text_Edit.Text = .TextMatrix(.Row, .Col)
    Else
      MsgBox "貼り付け先が範囲外です", vbOKOnly, "警告"
    End If
  End With
  
End Sub

Private Sub Search(ByVal Temp As String, r As Integer, C As Integer)
'データの行数、列数を得る
 
  Dim TmpString As String
  Dim MyTemp As String
   
  '貼り付けデータの一時保存
  MyTemp = Temp
   
  '1行分の文字列を得る Chr(13)=リターン
  TmpString = Left(Temp, InStr(Temp, Chr(13)))
   
  '列数を得る  Chr(9)=タブ
  Do Until InStr(TmpString, Chr(9)) = 0
    TmpString = Mid(TmpString, InStr(TmpString, Chr(9)) + 1)
    C = C + 1
  Loop
   
  '行数を得る
  Do Until InStr(Temp, Chr(13)) = 0
    Temp = Mid(Temp, InStr(Temp, Chr(13)) + 1)
    r = r + 1
  Loop
   
  r = r - 1
   
  'MSFlexGridからのコピーの場合
  If MyTemp = GridTemp Then
    r = r + 1
    If r = 1 Then
      '列数を得る
      Do Until InStr(MyTemp, Chr(9)) = 0
        MyTemp = Mid(MyTemp, InStr(MyTemp, Chr(9)) + 1)
        C = C + 1
      Loop
    End If
 
  End If
   
End Sub