Search code examples
vb.netwinformsspreadfarpoint-spreadfarpoint

Cancel EnterCell event of spread farpoint


My program use spread farpoint as a 3rd-party control, it has a TextBox control and a Spread control for showing data. When user change active cell in spread, I want to validate that the TextBox must not empty. If the TextBox is empty, EnterCell event must be cancel and the TextBox must got focus, also active cell of spread must not change. I'm stuck here.

Currently I performed validation in LeaveCell event but it doesn't work. EnterCell event fired and spread still changed the active cell :(

If TextBox1.Text = String.Empty Then
  MsgBox ("TextBox1 cannot blank")
  TextBox1.Select()
  'TextBox1.Focus() 'I have tried this function but still not working
End If

Please support me! Thanks all.


Solution

  • As far as my knowledge, there's nothing we can do to "cancel" EnterCell event. However, I found out that we could do a little trick to achieve it.

    Private Sub spread_LeaveCell(sender as Object, e as LeaveCellEventArgs) Handles sprSheet.LeaveCell
      If TextBox1.Text = String.Empty Then
        MsgBox ("TextBox1 cannot blank")
        'This is the trick
        e.NewColumn = e.Column
        e.NewRow = e.Row
        Exit Sub
      End If
      'Other leave cell proceses..
    End Sub
    
    ...
    
    Private Sub spread_EnterCell(sender as Object, e as EnterCellEventArgs) Handles sprSheet.EnterCell
      If TextBox1.Text = String.Empty Then
        TextBox1.Select()
      End If
    Exit Sub