0

私は、プロファイル名を(TextBox1に)入力して追加ボタンをクリックするとプログラムを作成しています。バックグラウンドで開いている別のフォームの別のテキスト ボックス (TextBox2) に TextBox1 に入力されたものを追加したい。もう 1 つの形式は、addsnake と呼ばれます (質問しないでください)。ビルド エラーは発生しませんが、addsnake フォーム (TextBox2) のテキスト ボックスは変更されません。これは、ボタンを押したときの私のコードです:

        Dim newsnake As New addsnake
        TextBox1.Text = newsnake.TextBox2.Text

はい、両方のテキスト ボックスを [パブリック] に設定しています。

最初のフォーム ( newprofile )

Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlClientPermission
Public Class NewProfile
Inherits System.Windows.Forms.Form
Dim newsnake As addsnake

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        newsnake = New addsnake
        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
    Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
    Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
    Friend WithEvents Dataset11 As TestFeedingChart.Dataset1
    Public WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
        Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter
        Me.Dataset11 = New TestFeedingChart.Dataset1
        CType(Me.Dataset11, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(104, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Enter Profile Name:"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(136, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(208, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(248, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 6
        Me.Button1.Text = "Add"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(136, 32)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(96, 24)
        Me.Button2.TabIndex = 7
        Me.Button2.Text = "Cancel"
       '
       'OleDbSelectCommand1
       '
       Me.OleDbSelectCommand1.CommandText = "SELECT id, ProfileName FROM Profile"
       Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
       '
       'OleDbConnection1
       '
       Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
       "ocking Mode=1;Data Source=""C:\Data\Database.mdb"";Jet OLEDB:Engine Type=5;Provide" & _
       "r=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persi" & _
       "st security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encry" & _
       "pt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Lo" & _
       "cale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Adm" & _
       "in;Jet OLEDB:Global Bulk Transactions=1"
       '
       'OleDbInsertCommand1
       '
       Me.OleDbInsertCommand1.CommandText = "INSERT INTO Profile(ProfileName) VALUES (?)"
       Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
       Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("ProfileName", System.Data.OleDb.OleDbType.VarWChar, 50, "ProfileName"))
       '
       'OleDbUpdateCommand1
       '
       Me.OleDbUpdateCommand1.CommandText = "UPDATE Profile SET ProfileName = ? WHERE (id = ?) AND (ProfileName = ? OR ? IS NU" & _
       "LL AND ProfileName IS NULL)"
       Me.OleDbUpdateCommand1.Connection = Me.OleDbConnection1
       Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("ProfileName", System.Data.OleDb.OleDbType.VarWChar, 50, "ProfileName"))
       Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "id", System.Data.DataRowVersion.Original, Nothing))
       Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_ProfileName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ProfileName", System.Data.DataRowVersion.Original, Nothing))
       Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_ProfileName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ProfileName", System.Data.DataRowVersion.Original, Nothing))
       '
       'OleDbDeleteCommand1
       '
       Me.OleDbDeleteCommand1.CommandText = "DELETE FROM Profile WHERE (id = ?) AND (ProfileName = ? OR ? IS NULL AND ProfileN" & _
       "ame IS NULL)"
       Me.OleDbDeleteCommand1.Connection = Me.OleDbConnection1
       Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "id", System.Data.DataRowVersion.Original, Nothing))
       Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_ProfileName", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ProfileName", System.Data.DataRowVersion.Original, Nothing))
       Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_ProfileName1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ProfileName", System.Data.DataRowVersion.Original, Nothing))
       '
       'OleDbDataAdapter1
       '
       Me.OleDbDataAdapter1.DeleteCommand = Me.OleDbDeleteCommand1
       Me.OleDbDataAdapter1.InsertCommand = Me.OleDbInsertCommand1
       Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
       Me.OleDbDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Profile", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("id", "id"), New System.Data.Common.DataColumnMapping("ProfileName", "ProfileName")})})
       Me.OleDbDataAdapter1.UpdateCommand = Me.OleDbUpdateCommand1
       '
       'Dataset11
       '
       Me.Dataset11.DataSetName = "Dataset1"
       Me.Dataset11.Locale = New System.Globalization.CultureInfo("en-US")
       '
       'NewProfile
       '
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(354, 68)
       Me.ControlBox = False
       Me.Controls.Add(Me.Button2)
       Me.Controls.Add(Me.Button1)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.Label1)
       Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
       Me.MaximizeBox = False
       Me.Name = "NewProfile"
       Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
       Me.Text = "Add New Profile"
       CType(Me.Dataset11, System.ComponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)

   End Sub

#End Region

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub


Private Sub RefreshData()
    Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Data\Database.mdb ;Extended Properties=Paradox 5.x;")
    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If
    Dim da As New OleDb.OleDbDataAdapter("SELECT [id], " & _
                                         "ProfileName AS [Name]" & _
                                         "FROM Profile ORDER BY id", cnn)
    Dim dt As New DataTable
    da.Fill(dt)
    cnn.Close()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If (TextBox1.Text = "") Then
        MsgBox("Please enter a profile name.")
    Else

        Dim newsnake As addsnake
        Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Database.mdb;User Id=admin;Password=;")
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If
        Dim SQL As String

        '("SELECT ProfileName FROM Profile WHERE ProfileName = TextBox1")
        'TextBox2.Text = SQL
        cmd.Connection = cnn
        cmd.CommandText = "INSERT INTO Profile(ProfileName) " & _
                          "VALUES ('" & Me.TextBox1.Text & "')"
        cmd.ExecuteNonQuery()
        If Not IsNothing(newsnake) Then
            newsnake.UpdateText = TextBox1.Text
        End If

        cnn.Close()
        Me.Close()

    End If
End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
End Sub

Private Sub OleDbDataAdapter1_RowUpdated(ByVal sender As System.Object, ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs)

End Sub

Private Sub NewProfile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim oForm As addsnake
    oForm = New addsnake
    oForm.Show()
    oForm = Nothing
End Sub
End Class

セカンドフォーム(addsnake

Public Class addsnake
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Public WithEvents TextBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Label2 = New System.Windows.Forms.Label
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(112, 23)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Snake Name/Morph:"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(112, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(288, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(296, 64)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Add"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(8, 64)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(104, 23)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Cancel"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Profile:"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(112, 32)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.ReadOnly = True
        Me.TextBox2.Size = New System.Drawing.Size(288, 20)
        Me.TextBox2.TabIndex = 5
        Me.TextBox2.Text = "-No Profile Selected-"
        Me.TextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'addsnake
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(402, 92)
        Me.ControlBox = False
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "addsnake"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Add New Snake"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Property UpdateText() As String
        Get
            Return TextBox2.Text
        End Get
        Set(ByVal value As String)
            TextBox2.Text = value
        End Set
    End Property
    Private Sub RefreshData()
        Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Data\Database.mdb ;Extended Properties=Paradox 5.x;")
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If
        Dim da As New OleDb.OleDbDataAdapter("SELECT [id], " & _
                                         "SnakeName AS [Name]" & _
                                         "FROM Snake ORDER BY id", cnn)
        Dim dt As New DataTable
        da.Fill(dt)
        cnn.Close()

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (TextBox1.Text = "") Then
            MsgBox("Please enter a snake name.")
        Else
            Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Database.mdb;User Id=admin;Password=;")
            Dim cmd As New OleDb.OleDbCommand
            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If

            cmd.Connection = cnn
            cmd.CommandText = "INSERT INTO Snake(SnakeName) " & _
                          "VALUES ('" & Me.TextBox1.Text & "')"
            cmd.ExecuteNonQuery()

            cnn.Close()
            Me.Close()
            MsgBox("Snake Succefully Added! :D")
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub
End Class
4

1 に答える 1

0

コメントで言ったように、別のaddsnakeフォームを作成しているように見えますが、それを表示するために表示せずに、テキストを表示したいものではない可能性が最も高いです。Classスコープインスタンス変数を保持する必要がありますaddsnake以前に可視化したインスタンス。また、Viji が述べたように、newsnake.TextBox2 の値を TextBox1 に割り当てていると述べたように、他の方法ではありません。この例のようなものからアイデアが得られるはずです。

Public Class Form1
    Dim newsnake As addsnake

    Public Sub New()

        InitializeComponent()
        newsnake = New addsnake
        newsnake.Show()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not IsNothing(newsnake) Then
            newsnake.TextBox2.Text = TextBox1.Text
        End If
   End Sub

End Class

しかし、個人的には、フォームの内部を公開することはしません。プロパティを使用して、このような内部にアクセスすることを好みます。変更を表示しているだけです。

Public Class addsnake
    Public Property UpdateText As String
        Get
            Return TextBox2.Text
        End Get
        Set(value As String)
            TextBox2.Text = value
        End Set
    End Property
End Class

フォーム1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Not IsNothing(newsnake) Then
        newsnake.UpdateText = TextBox1.Text
    End If
End Sub

コードを見た後に編集します。

私が気づいたのは、オブジェクトを何も設定していなくても、オブジェクトがまだアクティブで表示されている場合でも、NewProfileフォームをロードすると表示されるoFormという名前のaddsnakeフォームを作成していることです。ボタンをクリックすると、addsnake Form の newsnake オブジェクトが作成されます。このオブジェクトは、Forms コンストラクターで作成したクラス レベルの newsnake をマスクします。これを表示しても表示されないため、TextBox の変更は表示されません。あなたのプログラムフローが正確にはわかりませんが、ニュースネイクを更新した後にスタートアップフォームを閉じているため、最後のフォームが閉じられた後にプログラムを閉じたいようです.

今の私の質問は、あなたは何をしたいoFormですか?それは、TextBox が変更されていることを確認したいフォームですか? その場合は、newsnake の代わりに Module スコープの変数にして、それを使用する必要があります。私が示したように Forms コンストラクターで newsnake を作成していないが、Button1 クリック イベントハンドラーで null のローカル newsnake 変数も作成している場合は、その行を削除して、newsnake フォームをどこかに表示する必要があります。

ボタンクリックイベントをこれに変更してみてください。

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If (TextBox1.Text = "") Then
        MsgBox("Please enter a profile name.")
    Else

        Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Database.mdb;User Id=admin;Password=;")
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If
        Dim SQL As String

        '("SELECT ProfileName FROM Profile WHERE ProfileName = TextBox1")
        'TextBox2.Text = SQL
        cmd.Connection = cnn
        cmd.CommandText = "INSERT INTO Profile(ProfileName) " & _
                          "VALUES ('" & Me.TextBox1.Text & "')"
        cmd.ExecuteNonQuery()
        If Not IsNothing(newsnake) Then
            newsnake.UpdateText = TextBox1.Text
            newsnake.show()                     `note change
        End If

        cnn.Close()
        Me.Close()

    End If
End Sub
于 2013-09-19T23:10:05.863 に答える