Public Class Form1 Public Overloads Sub Show(value As String) TextBox1.Text = value Me.Show() TextBox1.Focus() '追加した部分です。 End Sub 'ボタンをクリックしたときにForm2を表示します。 追加した部分です。 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim f As New Form2 f.Owner = Me f.Show() End Sub End Class
Public Class Form2 Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ListBox1.Items.AddRange({"Apple", "Banana", "Cat", "Dog"}) End Sub Private Sub ListBox1_DoubleClick(sender As Object, e As System.EventArgs) Handles ListBox1.DoubleClick If ListBox1.SelectedItem Is Nothing Then '何も選択されていなければ何もしない。 Return End If '追加した部分です Dim selectst As String selectst = ListBox1.SelectedItem Dim f As New Form1 f = Me.Owner f.Show(selectst) Me.Close() End Sub Private Sub ListBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles ListBox1.KeyPress If e.KeyChar = Chr(Keys.Enter) Then ListBox1_DoubleClick(sender, e) End If Me.Close() End Sub End Class