I'm still devoloping my Add-In for Microsoft word. Now I have again trouble with the datagrid.
I have a datagrid where the user can enter data. My aim is to recognize when the user presses Enter while entering his data. I found some examples but they only work when I run my application as a console application. When I run it as the Add-In nothing happens.
Any ideas, what may be wrong or how to make it better?
Here's my code (the second column is the column where the user can enter data)
(BTW: how can I enter VB-Code?)
PHP Code:Private Sub FormatGrid()
Dim objTablestyle As DataGridTableStyle
Dim objColumnTextColumn As DataGridEnableTextBoxColumn
Dim objTextCol As DataGridKeyTrapTextBoxColumn
Dim i As Integer
Try
'Tablestyle
objTablestyle = New DataGridTableStyle()
With objTablestyle
'some code
End With
'1. Spalte (Bezeichnung)
i = 0
objColumnTextColumn = New DataGridEnableTextBoxColumn(i)
With objColumnTextColumn
'some code
End With
objTablestyle.GridColumnStyles.Add(objColumnTextColumn)
'2. Spalte (Wert)
i = 1
'objColumnTextColumn = New DataGridEnableTextBoxColumn(i)
'With objColumnTextColumn
objTextCol = New DataGridKeyTrapTextBoxColumn()
With objTextCol
.HeaderText = Me.grdData.TableStyles(IBS_Vorschau.objDataSetVorschau.strTableName).GridColumnStyles(i).HeaderText
.MappingName = Me.grdData.TableStyles(IBS_Vorschau.objDataSetVorschau.strTableName).GridColumnStyles(i).MappingName
.NullText = Me.grdData.TableStyles(IBS_Vorschau.objDataSetVorschau.strTableName).GridColumnStyles(i).NullText
.ReadOnly = Me.grdData.TableStyles(IBS_Vorschau.objDataSetVorschau.strTableName).GridColumnStyles(i).ReadOnly
.Width = Me.grdData.TableStyles(IBS_Vorschau.objDataSetVorschau.strTableName).GridColumnStyles(i).Width
AddHandler .TextBox.MouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseDown)
AddHandler .TextBox.Enter, New EventHandler(AddressOf EnterTextBox)
AddHandler .TextBox.Leave, New EventHandler(AddressOf LeaveTextBox)
AddHandler .TextBox.TextChanged, New EventHandler(AddressOf TextChangedInTextBox)
AddHandler .KeyPressed, New DataGridKeyTrapTextBoxColumn.KeyPressHandler(AddressOf KeyPressed)
'AddHandler .TextBox.KeyDown, New KeyEventHandler(AddressOf Test)
End With
DataGridKeyTrapTextBoxColumn.mRowCount = mdseDataSetVorschau.Tables(IBS_Vorschau.objDataSetVorschau.strTableName).Rows.Count
'objTablestyle.GridColumnStyles.Add(objColumnTextColumn)
objTablestyle.GridColumnStyles.Add(objTextCol)
' make the dataGrid use our new tablestyle and bind it to our table
Me.grdData.TableStyles.Clear()
Me.grdData.TableStyles.Add(objTablestyle)
Me.grdData.DataSource = mdseDataSetVorschau.Tables.Item(IBS_Vorschau.objDataSetVorschau.strTableName)
Catch
MsgBox("frmIBS_Vorschau.FormatGrid: " & Err.Description)
Finally
objColumnTextColumn = Nothing
objTablestyle = Nothing
End Try
End Sub
Public Class DataGridKeyTrapTextBoxColumn
Inherits DataGridTextBoxColumn
Private mKeyTrapTextBox As KeyTrapTextBox = Nothing
Private mSource As System.Windows.Forms.CurrencyManager = Nothing
Private mRowNum As Integer
Private mIsEditing As Boolean = False
Public Shared mRowCount As Integer = 0
'Events
Public Event KeyPressed As KeyPressHandler
'Methods
Public Delegate Sub KeyPressHandler(ByVal sender As Object, ByVal e As DataGridKeyPressEventArgs)
Private Sub TextBoxEditStarted(ByVal sender As Object, ByVal e As KeyPressEventArgs)
MsgBox("In TextBoxEditStarted")
mIsEditing = True
End Sub
Private Sub LeaveKeyTrapTextBox(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("In LeaveKeyTrapTextBox")
If mIsEditing Then
SetColumnValueAtRow(mSource, mRowNum, mKeyTrapTextBox.Text)
mIsEditing = False
Invalidate()
End If
mKeyTrapTextBox.Hide()
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
If (mRowCount < source.Count) Then
mSource.RemoveAt(mSource.Count - 1)
mSource.AddNew()
End If
mRowCount = source.Count
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)
mRowNum = rowNum
mSource = source
mKeyTrapTextBox.Parent = Me.TextBox.Parent
mKeyTrapTextBox.Location = Me.TextBox.Location
mKeyTrapTextBox.Size = Me.TextBox.Size
mKeyTrapTextBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
mKeyTrapTextBox.Visible = True
mKeyTrapTextBox.BringToFront()
mKeyTrapTextBox.Focus()
End Sub
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
If (mIsEditing) Then
mIsEditing = False
SetColumnValueAtRow(dataSource, rowNum, mKeyTrapTextBox.Text)
End If
Return True
End Function
Private Sub ReturnKey(ByVal sender As Object, ByVal e As DataGridProcessKeyEventArgs)
Dim x As DataGridKeyPressEventArgs
Try
If e.RunValue Then
MsgBox("ReturnKey, enablevalue= true, KeyCode = " & e.MyKeyCode) 'hier komme ich an
x = New DataGridKeyPressEventArgs(e.MyKeyCode)
RaiseEvent KeyPressed(Me, x)
Else
MsgBox("ReturnKey, enablevalue= false")
End If
Catch
MsgBox("DataGridKeyTrapTextBoxColumn.ReturnKey: " & Err.Description)
Finally
x = Nothing
End Try
End Sub
Public Sub New()
mKeyTrapTextBox = New KeyTrapTextBox()
mKeyTrapTextBox.BorderStyle = BorderStyle.Fixed3D
mKeyTrapTextBox.BackColor = Color.LightGray
'benoetigte handler dazu
AddHandler mKeyTrapTextBox.Leave, New EventHandler(AddressOf LeaveKeyTrapTextBox)
AddHandler mKeyTrapTextBox.KeyPress, New KeyPressEventHandler(AddressOf TextBoxEditStarted)
AddHandler mKeyTrapTextBox.ProcessKey, New KeyTrapTextBox.ProcessKeyHandler(AddressOf ReturnKey)
End Sub
End Class
Public Class KeyTrapTextBox
Inherits TextBox
'Konstanten
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101
Const WM_CHAR As Integer = &H102
'Events
Public Event ProcessKey As ProcessKeyHandler
'Methods
Public Delegate Sub ProcessKeyHandler(ByVal sender As Object, ByVal e As DataGridProcessKeyEventArgs)
Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean
Dim keycode As Keys
Dim e As DataGridProcessKeyEventArgs
keycode = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)
'for a datagrid, we need to eat the tab key oe else its done twice
If msg.Msg = WM_KEYDOWN And keycode = Keys.Enter Then
MsgBox("KeyTrapTextBox: enter")
'Event, damit das bekannt wird
e = New DataGridProcessKeyEventArgs(True, keycode)
RaiseEvent ProcessKey(Me, e)
Return False
End If
Return MyBase.PreProcessMessage(msg)
End Function
End Class




Reply With Quote