Hallo!
Für solche Zwecke verwendet man ein Datagridview-Control.
Hier der erste Teil des Codes.
Wenn Du Controls im Designer hinzufügen willst, mußt Du
die entsprechende Dim-Anweisung aus dem Code entfernen
und das Control im Designer einrichten.
Beachte, dass es sich hier um eine FESTE Feldgröße handelt.
Zellenzahl und Zellengröße müssen zu den Abmessungen passen.
RESIZE, SCROLLBARS etc. sind ungeeignet.
Public Class frmHex2
Const gridlines As Integer = 20
Const rowcolumns As Integer = 16
Dim inputlength As Integer = gridlines * rowcolumns
Dim filelength, topposition As Long
Const ShowHex As Boolean = True
Dim fsin As IO.FileStream, br As IO.BinaryReader, bw As IO.BinaryWriter
Dim WithEvents dgv As New DataGridView With {.Parent = Me, .Dock = _
DockStyle.Fill}
Private Sub frmHex_FormClosing(sender As Object, _
e As FormClosingEventArgs) Handles Me.FormClosing
br.Close() : bw.Close() : fsin.Close()
End Sub
Private Sub frmHex_Load(sender As System.Object, e As EventArgs) Handles _
MyBase.Load
'Formular einrichten
Me.Width = 740 : Me.Height = 470
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
'Datei laden
Dim infile As String = "F:\Daten\bytes.bin"
fsin = New IO.FileStream(infile, IO.FileMode.Open, _
IO.FileAccess.ReadWrite)
filelength = fsin.Length
br = New IO.BinaryReader(fsin)
bw = New IO.BinaryWriter(fsin)
'Datagridview einrichten
With dgv
For i As Integer = 0 To rowcolumns - 1
.Columns.Add("SP" & CStr(i), "SP" & CStr(i))
.Columns(i).Width = 30
.Columns(i).DefaultCellStyle.Alignment = _
DataGridViewContentAlignment.BottomRight
If i = rowcolumns \ 2 Then
.Columns(i).Width = 70
Else
.Columns(i).Width = 40
End If
Next i
.Font = New Font("Arial", 10)
.Rows.Add(gridlines)
.ColumnHeadersVisible = False
.AllowUserToAddRows = False : .AllowUserToDeleteRows = False
.AllowUserToResizeColumns = False : .AllowUserToOrderColumns = False
.AllowUserToResizeRows = False : .RowHeadersVisible = False
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.CellBorderStyle = DataGridViewCellBorderStyle.None
End With
'Anzeige aktualisieren
topposition = 0 : Showbytes(0)
dgv.Focus()
End Sub
Private Sub Showbytes(ByVal RowSelected As Integer)
fsin.Position = topposition
Dim byt As Byte() = br.ReadBytes(inputlength)
Dim r, c As Integer
dgv.SuspendLayout()
r = 0 : c = -1
For i As Integer = 0 To byt.Length - 1
c += 1 : If c = rowcolumns Then r += 1 : c = 0
If Not ShowHex Then
dgv.Rows(r).Cells(c).Value = CStr(byt(i))
Else
dgv.Rows(r).Cells(c).Value = Hex(byt(i))
End If
Next i
For i As Integer = byt.Length To inputlength - 1
c += 1 : If c = rowcolumns Then r += 1 : c = 0
dgv.Rows(r).Cells(c).Value = String.Empty
Next i
dgv.ResumeLayout()
dgv.Rows(RowSelected).Selected = True
End Sub
Private Sub dgv_CellEndEdit(sender As Object, _
e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles dgv.CellEndEdit
Dim c As DataGridViewCell = dgv.CurrentCell
Try
Dim byt As Byte
If ShowHex Then
byt = CByte(Convert.ToInt32(c.Value.ToString, 16))
Else
byt = CByte(c.Value)
End If
Dim fileposition As Long = topposition + e.RowIndex * rowcolumns + _
e.ColumnIndex
If fileposition < filelength Then
fsin.Position = fileposition : bw.Write(byt)
Else
dgv.CurrentCell.Value = String.Empty
End If
Catch
Finally
Dim ex As New KeyEventArgs(Keys.Down)
Showbytes(e.RowIndex)
dgv_KeyDown(dgv, ex)
End Try
End Sub |