2008年1月29日火曜日

ダイアログっぽくDataGridViewの編集を行う際の表示位置

前回ダイアログを表示するときにはちょっとロジックがいる、というのを書いた。

簡単に言うとこういったことをやる必要があるってとこで。

  • 編集するセルにあわせて表示
  • ただし表示するダイアログが親コンテナをはみ出す際にはずらして表示

言葉で書くと2行なんだけどねw
実際にはこんな感じで表示座標を算出しないといけないのよ


Dim editCell As SubEditorDialogCell = TryCast(Me._parentDataGridView.CurrentCell, SubEditorDialogCell)
If (editCell Is Nothing) OrElse (editCell.EditorInstanceType Is Nothing) Then Return

subEditorInstance = TryCast(System.Activator.CreateInstance(editCell.EditorInstanceType), SubEditorDialog)
If subEditorInstance Is Nothing Then Return

'位置の割り出し
Dim cellRectangle As System.Drawing.Rectangle = editCell.DataGridView.GetCellDisplayRectangle(editCell.ColumnIndex, editCell.RowIndex, True)
Dim cellPoint As System.Drawing.Point = Me._parentDataGridView.PointToScreen(New System.Drawing.Point(cellRectangle.Left, cellRectangle.Top))

'表示位置調整
Dim editorSize As System.Drawing.Size = subEditorInstance.Size
Dim owner As System.Windows.Forms.ContainerControl = DirectCast(Me._parentDataGridView.TopLevelControl, System.Windows.Forms.ContainerControl)
Dim ownerSize As System.Drawing.Size = owner.ClientRectangle.Size
If (owner.Left + ownerSize.Width) < (cellPoint.X + editorSize.Width) Then cellPoint.X = owner.Left + ownerSize.Width - editorSize.Width
If (owner.Top + ownerSize.Height) < (cellPoint.Y + editorSize.Height) Then cellPoint.Y = owner.Top + ownerSize.Height - editorSize.Height
Dim afterCellPoint As System.Drawing.Point = Me._parentDataGridView.TopLevelControl.PointToClient(cellPoint)


ポイントとなるのは「親コンテナのクライアント領域」。これをコンテナのSizeとかからやると、コンテナにスクロールバーがでたり一部未表示になったりするか注意が必要。



まあこんな感じで表示位置を求めてあげれば、あとはLocationプロパティとかにつっこむことによってちゃんと表示されるって寸法です。

0 件のコメント:

コメントを投稿