0

我有一个,我有一个与每一行关联DataGridView的编辑。Button

CellContentClick事件中,我正在动态删除和创建我的textbox控件Label的数量TableLayoutPanel

这种动态删除和创建控件需要一些时间也闪烁(这不是一个大问题)。

但问题是,如果有人在一段时间后不断地点击编辑buttons各个行,那么整个事情就TableLayoutPanel变得一团糟了。

据我所知,这正在发生,因为我的CellContentClick活动没有时间完成,在活动完成之前,单击button其他行的编辑。我无法处理这种情况

处理程序代码在这里:

Private Sub gdXMLDOc1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gdXMLDOc1.CellContentClick
    Try
        If lockThis = False Then
            lockThis = True

            If e.ColumnIndex = 0 Then

                intPreviousRowIndex = intSelectedRowIndex
                intSelectedRowIndex = e.RowIndex

                gdXMLDOc1.Rows(intSelectedRowIndex).DefaultCellStyle.BackColor = Color.LightSkyBlue

                If intPreviousRowIndex <> -1 And intPreviousRowIndex <> intSelectedRowIndex Then

                    arrQuestion(intPreviousRowIndex).questionText = Replace(txtQText_New.Text, """", "\""")
                    arrQuestion(intPreviousRowIndex).choice = Replace(txtOpt1_New.Text, """", "\""")

                    For i As Integer = 0 To arrQuestion(intPreviousRowIndex).cText_eng.Length - 1

                        arrQuestion(intPreviousRowIndex).cText(i).line = Replace(TableLayoutPanel1.GetControlFromPosition(2, i + 3).Text, """", "\""")

                    Next

                    For i = TableLayoutPanel1.RowCount - 1 To 3 Step -1
                        TableLayoutPanel1.RowCount = TableLayoutPanel1.RowCount - 1
                        TableLayoutPanel1.Controls.Remove(TableLayoutPanel1.GetControlFromPosition(0, i))
                        TableLayoutPanel1.Controls.Remove(TableLayoutPanel1.GetControlFromPosition(1, i))
                        TableLayoutPanel1.Controls.Remove(TableLayoutPanel1.GetControlFromPosition(2, i))
                    Next

                End If

                TableLayoutPanel1.RowCount = 4

                txtQText.Text = arrQuestion(intSelectedRowIndex).questionText_eng
                txtOpt1.Text = arrQuestion(intSelectedRowIndex).choice_eng
                txtQText_New.Text = arrQuestion(intSelectedRowIndex).questionText
                txtOpt1_New.Text = arrQuestion(intSelectedRowIndex).choice
                TableLayoutPanel1.RowCount = TableLayoutPanel1.RowCount - 1
                Dim intRowIndex As Integer = TableLayoutPanel1.RowCount

                For i As Integer = 0 To arrQuestion(intSelectedRowIndex).cText_eng.Length - 1

                    Dim lbl As Label = New Label()
                    lbl.AutoSize = True
                    lbl.Font = New System.Drawing.Font("Arial", 11.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                    lbl.Size = New System.Drawing.Size(73, 36)
                    lbl.TabIndex = 5
                    lbl.Text = "Line" + arrQuestion(intSelectedRowIndex).cText_eng(i).lineId.ToString

                    Dim TxtBox1 As dynamicTextBox = New dynamicTextBox()
                    TxtBox1.Text = arrQuestion(intSelectedRowIndex).cText_eng(i).line

                    Dim TxtBox2 As TextBox = New TextBox()
                    TxtBox2.Font = New System.Drawing.Font("Mangal", 13.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                    TxtBox2.MaxLength = 50000
                    TxtBox2.Size = New System.Drawing.Size(600, 37)
                    TxtBox2.TabIndex = 16
                    TxtBox2.Text = arrQuestion(intSelectedRowIndex).cText(i).line

                    TableLayoutPanel1.RowCount = TableLayoutPanel1.RowCount + 1

                    TableLayoutPanel1.Controls.Add(lbl, 0, intRowIndex)
                    TableLayoutPanel1.Controls.Add(TxtBox1, 1, intRowIndex)
                    TableLayoutPanel1.Controls.Add(TxtBox2, 2, intRowIndex)


                    intRowIndex = TableLayoutPanel1.RowCount
                Next

            End If
            lockThis = False
        End If
    Catch ex As Exception

    End Try

End Sub
4

3 回答 3

0

执行此操作的几种方法

在事件处理程序中的代码运行时禁用按钮

添加一个布尔属性,测试是否为真,将其设置为真,做你的东西将其设置为假。

看看您是否可以将“编辑请求”排队并在后台完成大部分工作。

第二个是我的选择,为了快速获胜。

和 ....

"之后继续点击各行的编辑按钮"

你所有的编辑按钮都有相同的处理程序吗?

鉴于你已经完成

if (!_inhibit)
{
  try
  {
    _inhibit = true;
    // mess with template
  }
  finally
  {
    _inhibit = false;
  }
}

只是不明白这怎么会出错。如果你放了一个 debug _inhibit = true,除非处理程序代码完成,否则它永远不会到达那里。鉴于这是真的,你认为错的不是。

于 2012-04-11T13:34:19.743 回答
0

像这样使用单元格点击事件

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) 处理 DataGridView1.CellClick

当用户单击表格中的文本时,单元格内容单击起作用,从而产生令人困惑的用户体验

于 2013-08-21T11:13:17.893 回答
0

查看 SuspendLayout() 和 ResumeLayout() 函数以帮助解决闪烁问题。另一方面,没有任何代码很难确定。一种可能性是,如果您在任何地方使用 Appliation.DoEvents()。这可能会导致一个线程在前一个实例完成之前调用您的方法的问题,这可能会产生您描述的那种结果。

于 2012-04-11T13:31:20.720 回答