0

我正在制作一个程序,当您输入配置文件名称(在 TextBox1 中)并单击添加按钮时。我希望它将输入的任何内容 TextBox1 添加到在后台打开的另一个表单上的另一个文本框 (TextBox2) 中。另一种形式称为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

看到你的代码后编辑。

我注意到您正在创建名为 oForm 的添加蛇表单,当您加载 NewProfile 表单时它是可见的,即使您将对象设置为空,它仍然处于活动状态且可见。当你点击你的按钮时,你创建了你的addsnake表单的一个newsnake对象,它将掩盖你在Forms构造函数中创建的类级别的newsnake,你不会通过显示它来使其可见,因此你永远不会看到TextBox的变化。我不确定您的程序流程到底是什么,但看起来您希望程序在最后一个表单关闭后关闭,因为您在更新 newsnake 后关闭了启动表单。

我现在的问题是你想做oForm什么?是您希望看到 TextBox 更改的表单吗?如果是这样,您需要将其设置为 Module 范围变量而不是 newsnake 并使用它。如果不是像我展示的那样在 Forms 构造函数中更新 newsnake,但是您还在 Button1 click 事件处理程序中创建了一个本地 newsnake 变量,该变量为 null,您需要删除该行并在某处显示 newsnake Form。

尝试将您的按钮单击事件更改为此。

    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 回答