问题描述
我正在使用VS社区2015 for VB 2015。
我有一个datagridview,每个包含10列在Windows窗体上作为CRC计算器的24行。还有2个文本框,tbxCRC& tbxIn,2个按钮,btnExit& btnClear。
在datagridview中,偶数列只是索引0到124,奇数列是值。最初,所有奇数列行都预先加载了空字符串""。 这些值从文本框传递,该文本框的焦点是用户输入十六进制
数据,这很好。当框中的字符串长度为2时,它将从c1,r 0传递到datagridview,行/列根据需要递增,并计算新的CRC值并在每个条目的tbxCRC中显示。
如果出现输入错误,则用户单击具有错误的datagridview单元格,该值将被删除并替换为空值字符串并且CRC将重新生成 - 计算到空单元格,如果通过文本框输入新值,并且这不是列表末尾的
,则重新计算完整列表的CRC,并再次计算所有值。
但是:如果用户点击一个空单元格,例如假设单元格0到30已填满且用户在单元格80中单击,则可以直接在其中输入一个值扰乱整个苹果车。
datagridview设置为只读,但仍然可以访问和写入单元格。是否有任何聪明的方法来阻止这一点,但仍然允许CellMouseClick,(或其他鼠标操作)事件删除单元格内容?
小心它有效并且做CRC-8就像我一样想要,但有很多可能的方法来创建一个错误,它不可能作为一个有用的程序发布。
任何帮助将不胜感激。
我的代码在这里:
Public Class Form1
Dim i,j,ei,cl ,rw As Byte
Dim rta As Byte = 25'行添加
Dim crc As Integer
Dim posc As Integer
Dim posr As Integer
Dim oldcrc,Newcrc As Integer
Private Sub Form1_Load(sender As Object,e As EventArgs)Handles MyBase.Load
SetupDGVI()
tbxIn.Focus()
End Sub
Private Sub SetupDGVI()
DGVI.EditMode = DataGridViewEditMode.EditProgrammatically
DGVI.ColumnCount = 10
DGVI
对于j = 0到9'ZERO INDEXED你MUPPET
.Columns(j).Width = 60
Next
.Columns(0).Name =" Addr"
.Columns(1).Name =" Hex"
.Columns(2).Name =" Addr"
.Columns(3).Name =" Hex"
.Columns(4).Name =" Addr"
.Columns(5).Name =" Hex"
.Columns(6).Name =" Addr"
.Columns(7).Name =" Hex"
.Columns(8).Name =" Addr"
.Columns(9).Name =" Hex"
对于j = 0到rta - 1
.Rows.Add(j,",",j + 25,",",j + 50,"",j + 75, "",j + 100,"")
Next
for j = 0 To 9
.Columns(j).ReadOnly = True
Next j
'.CurrentCell = DGVI(1,0)
'ei = 0
'.BeginEdit(1)
posc = 1
posr = 0
End With
结束子
私人函数Get_CRC(ByVal vtg As String,crc As Byte)As String
尝试
Dim fb As Byte
Dim vtc As Byte
vtc = Convert.ToByte(vtg,16)'Base 16(Hex)
i = 8
Do
fb =(crc Xor vtc) 1
vtc = vtc>> 1
crc = crc>> 1
如果fb那么
crc = crc Xor 140'0x8C
结束如果
i - = 1
循环直到i = 0
Catch ex As Exception
crc = 255
结束尝试
返回crc
结束函数
私有子按钮2_Click(发送者为对象,e为EventArgs)处理BtnClr.Click
对于i = 1到9步骤2
对于j = 0到rta - 1
DGVI(i,j).Value =""
下一个j
下一个i
posc = 1
posr = 0
oldcrc = 0
tbxIn.Text = Nothing
tbxIn.Focus( )
tbxCRC.Text = Nothing
End Sub
Public Function IsHex(ByVal str As String)As Boolean
Try
Dim num As Long = CLng (& H"& str)
返回True
Catch ex As Exception
返回False
结束尝试
结束函数
Private Sub btnExit_Click(sender As Object,e As EventArgs)处理btnExit.Click
Me.Close()
End Sub
Private Sub tbxIn_KeyUp(sender As Object,e As KeyEventArgs)处理tbxIn.KeyUp
Dim inval As String
inval = tbxIn.Text.ToUpper
if inval.Length = 2 Then
If IsHex(inval)Then
DGVI.CurrentCell = DGVI(posc,posr)
DGVI.CurrentCel l.Value = inval
doCRC(inval,oldcrc)
posr + = 1
如果posr> = rta那么
posc = posc + 2
如果posc = 11然后
MsgBox("数据集已满。请清除表格",vbOK"
结束如果
posr = 0
结束如果
DGVI.CurrentCell = DGVI(posc,posr)
DGVI.CurrentCell.Value = DGVI.CurrentCell
如果DGVI.CurrentCell.Value<> ""然后'如果下一个框不为空,则将整个crc重做为空字符串
redocrc()
结束如果
Else MsgBox(仅限Hex值,vbOK)
结束If
tbxIn.Text = Nothing
End if
End Sub
Private Sub doCRC(ins as String,oldv As Byte)
Newcrc = Get_CRC(ins ,oldv)
oldcrc = Newcrc'更新已保存的值
tbxCRC.Text = Newcrc.ToString(" X2")
End Sub
Private Sub DGVI_CellMouseClick(sender As Object,e As DataGridViewCellMouseEventArgs)处理DGVI.CellMouseClick
posc = DGVI.CurrentCellAddress.X'列
posr = DGVI.CurrentCellAddress.Y'row
clearcell()
'如果是posr> 0然后
'posr - = 1
'Else posr = 24
'如果posc> 1然后
'posc - = 1
'posc - = 1
'Else posc = 1
'结束如果
'结束如果
'DGVI.CurrentCell = DGVI(posc,posr)'激活前一个单元格
结束次级
私有子redocrc()
Dim cellval As String
Dim il, jl As Byte
oldcrc = 0
对于jl = 1到9步骤2'获取所有单元格值并重新计算crc
对于il = 0到rta - 1
DGVI.CurrentCell = DGVI(jl,il)
cellval = DGVI.CurrentCell.Value
如果cellval =""然后
'clearcell()
退出Sub
否则
doCRC(cellval,oldcrc)
结束如果
下一个il
下一个jl
End Sub
Public Sub clearcell()
DGVI.CurrentCell.Value =""
tbxIn.Text =""
tbxIn.Focus()
redocrc()
End Sub
End Class
$
Hi,
I am using VS community 2015 for VB 2015.
I have a datagridview which has 10 columns each of 24 rows as a CRC calculator on a windows form. There are also 2 textboxes, tbxCRC & tbxIn, 2 buttons, btnExit & btnClear.
In the datagridview, even columns are just indices 0 to 124, odd columns are values. Initially all of the odd column rows are pre-loaded with the null string "". The values are passed from a textbox which has focus that the user enters hex data into, and that is fine. When the string length in the box is 2, it is passed to the datagridview from c1, r 0, the row / column is incremented as required, and the new CRC value is calculated and displayed in tbxCRC on every entry.
If there is an entry error, the user clicks in the datagridview cell that has the error, the value is deleted and replaced by a null value string and the CRC is re-calculated to the empty cell, and if a new value is entered via the textbox, and this is not at the end of the list, the CRC is re-calculated for the complete list, and again that all works.
However: If the user clicks in an empty cell, for example suppose that cells 0 to 30 are filled and the user clicks in cell 80, he can enter a value in there directly and that upsets the whole apple cart.
The datagridview is set to read only, but still cells can be accessed and written to. Is there any clever way to stop this but still allow the CellMouseClick, (or other mouse action) event to delete the cell contents?
With care it works and does the CRC-8 as I want, but there are so many possible ways to create an error that it would be impossible to issue as a useful program.
Any help would be appreciated.
My code is here:
Public Class Form1 Dim i, j, ei, cl, rw As Byte Dim rta As Byte = 25 'rows to add Dim crc As Integer Dim posc As Integer Dim posr As Integer Dim oldcrc, Newcrc As Integer Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load SetupDGVI() tbxIn.Focus() End Sub Private Sub SetupDGVI() DGVI.EditMode = DataGridViewEditMode.EditProgrammatically DGVI.ColumnCount = 10 With DGVI For j = 0 To 9 'ZERO INDEXED YOU MUPPET .Columns(j).Width = 60 Next .Columns(0).Name = "Addr" .Columns(1).Name = "Hex" .Columns(2).Name = "Addr" .Columns(3).Name = "Hex" .Columns(4).Name = "Addr" .Columns(5).Name = "Hex" .Columns(6).Name = "Addr" .Columns(7).Name = "Hex" .Columns(8).Name = "Addr" .Columns(9).Name = "Hex" For j = 0 To rta - 1 .Rows.Add(j, "", j + 25, "", j + 50, "", j + 75, "", j + 100, " ") Next For j = 0 To 9 .Columns(j).ReadOnly = True Next j '.CurrentCell = DGVI(1, 0) 'ei = 0 '.BeginEdit(1) posc = 1 posr = 0 End With End Sub Private Function Get_CRC(ByVal vtg As String, crc As Byte) As String Try Dim fb As Byte Dim vtc As Byte vtc = Convert.ToByte(vtg, 16) 'Base 16 (Hex) i = 8 Do fb = (crc Xor vtc) And 1 vtc = vtc >> 1 crc = crc >> 1 If fb Then crc = crc Xor 140 '0x8C End If i -= 1 Loop Until i = 0 Catch ex As Exception crc = 255 End Try Return crc End Function Private Sub Button2_Click(sender As Object, e As EventArgs) Handles BtnClr.Click For i = 1 To 9 Step 2 For j = 0 To rta - 1 DGVI(i, j).Value = "" Next j Next i posc = 1 posr = 0 oldcrc = 0 tbxIn.Text = Nothing tbxIn.Focus() tbxCRC.Text = Nothing End Sub Public Function IsHex(ByVal str As String) As Boolean Try Dim num As Long = CLng("&H" & str) Return True Catch ex As Exception Return False End Try End Function Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub tbxIn_KeyUp(sender As Object, e As KeyEventArgs) Handles tbxIn.KeyUp Dim inval As String inval = tbxIn.Text.ToUpper If inval.Length = 2 Then If IsHex(inval) Then DGVI.CurrentCell = DGVI(posc, posr) DGVI.CurrentCell.Value = inval doCRC(inval, oldcrc) posr += 1 If posr >= rta Then posc = posc + 2 If posc = 11 Then MsgBox("Dataset is full. Please clear table", vbOK) End If posr = 0 End If DGVI.CurrentCell = DGVI(posc, posr) DGVI.CurrentCell.Value = DGVI.CurrentCell If DGVI.CurrentCell.Value <> "" Then 'If next box is not empty, redo whole crc to empty string redocrc() End If Else MsgBox("Hex Values only", vbOK) End If tbxIn.Text = Nothing End If End Sub Private Sub doCRC(ins As String, oldv As Byte) Newcrc = Get_CRC(ins, oldv) oldcrc = Newcrc 'updates saved value tbxCRC.Text = Newcrc.ToString("X2") End Sub Private Sub DGVI_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DGVI.CellMouseClick posc = DGVI.CurrentCellAddress.X 'Column posr = DGVI.CurrentCellAddress.Y 'row clearcell() ' If posr > 0 Then ' posr -= 1 ' Else posr = 24 'If posc > 1 Then 'posc -= 1 'posc -= 1 ' Else posc = 1 'End If 'End If ' DGVI.CurrentCell = DGVI(posc, posr) 'activates previous cell End Sub Private Sub redocrc() Dim cellval As String Dim il, jl As Byte oldcrc = 0 For jl = 1 To 9 Step 2 'get all cell values and recalculate crc For il = 0 To rta - 1 DGVI.CurrentCell = DGVI(jl, il) cellval = DGVI.CurrentCell.Value If cellval = "" Then 'clearcell() Exit Sub Else doCRC(cellval, oldcrc) End If Next il Next jl End Sub Public Sub clearcell() DGVI.CurrentCell.Value = "" tbxIn.Text = "" tbxIn.Focus() redocrc() End Sub End Class
这篇关于Datagridview编辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!