2016-09-21 3 views
1

私は2つのDatagridviewコントロールを同じ形式で持っています。各Datagridにはユーザーが長いテキストを書き込む列がいくつかあるので、ユーザーがこれらの列をダブルクリックしてテキスト入力を拡大するときに開くRichTextBoxでフォームを設計しました。コードは機能しますが、両方のDatagridに同じフォームを使用したいので、何らかの形でアクティブなDataGridviewセルにテキストを返す必要があります。ここで(Datagridview1用)私のコードは次のとおりです。Custom Textbox Zoom - テキストをアクティブなDataGridviewセルに戻しますか?

TextZoomForm:Form1の中

Public Class TextZoomForm 

    Public OpenedForm1 As New Form1 
    Private Sub RichTextBox1_DoubleClick(sender As Object, e As EventArgs) Handles RichTextBox1.DoubleClick 

     OpenedForm1.DataGridView1.CurrentCell.Value = RichTextBox1.Text 
     OpenedForm1.Label24.Focus() 
     Me.Close() 

    End Sub 

    Private Sub TextZoom_Load(sender As Object, e As EventArgs) Handles Me.Load 
       RichTextBox1.Text = OpenedForm1.DataGridView1.CurrentCell.Value 

    End Sub 

End Class 

DataGridView1_CellMouseDoubleClick:

Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick 

     If e.ColumnIndex = 1 Then 

      Dim cp = Cursor.Position 
      cp.Y += CInt(Cursor.Size.Height * (-0.5)) 
      cp.X += CInt(Cursor.Size.Width * 0.8) 

      Dim f As New TextZoomForm() 
      f.OpenedForm1 = Me 
      f.Show() 
      f.Location = New Point(cp) 

     End If 

    End Sub 

アクティブDataGridViewのセルにテキストを返す方法上の任意のアイデア?

答えて

1

ズームしたフォームを変更して、データの元の場所がわからないようにします。代わりに、それを使用するコントロールがデータを渡します。以下に、あなたのコードを変更し、フォームを呼び出すために

Public Class TextZoomForm 

    Public Property ZoomedText As String 
     Get 
      Return RichTextBox1.Text 
     End Get 
     Set(value As String) 
      RichTextBox1.Text = value 
     End Set 
    End Property 

    Private Sub RichTextBox1_DoubleClick(sender As Object, e As EventArgs) Handles RichTextBox1.DoubleClick 
     Me.Close() 
    End Sub 

End Class 

ShowDialogを使用して

Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick 

... 

      Dim f As New TextZoomForm() 
      f.ZoomedText = DataGridView1.CurrentCell.Value 
      f.ShowDialog() 
      'Great breakpoint location. 
      DataGridView1.CurrentCell.Value = f.ZoomedText 
      Label24.Focus() 
.... 

    End Sub 

はあなたの呼び出しを介して電流セル部の道をユーザが変更できないようにします。

あなたはそれモードレス必要があるなら、あなたはすべき:

  • ストアユーザーが
  • FormClosingイベントを処理選択したセル。
  • DialogResultをテストして、ユーザーがOKを押したことを確認してください。
  • データを保存したセルに書き戻します。
+0

ZoomTextをダブルクリックするにはどのようにプロパティを使用しますか?あなたのコードを貼り付けましたが、Datagridviewセルに何も返されません。 – LuckyLuke82

+0

私はダブルクリックでフォームを閉じるだけで、答えを更新しました。 'ShowDialog()'の後の行のセルにデータが戻されます。 – FloatingKiwi

+0

はい今は動作しますが、それでも問題は1つです。 Datagridにテキストを挿入した後、私は他のコントロールに焦点を当てて、入力されたテキストを即座に見るようにしました。私はLabel24を使っています。私の編集した質問(RichTextbox double_click)を見てください。今これはいずれも機能しません.... – LuckyLuke82

関連する問題