我正在创建一个程序,当您输入配置文件名称(在TextBox1中)并单击添加按钮时。我希望它将在TextBox1中输入的内容添加到另一个在后台打开的表单上的另一个文本框(TextBox2)。另一种形式叫做addnake(不要问)。我没有构建错误,但addnake表单(TextBox2)上的文本框没有变化。这是按下按钮时的代码:
Dim newsnake As New addsnake
TextBox1.Text = newsnake.TextBox2.Text
是的,我确实将两个文本框设置为Public。
第一个表单( 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
答案 0 :(得分:0)
正如我在评论中所说,看起来您正在创建一个单独的addsnake
表单,但没有显示它以使其可见,很可能它不是您想要查看文本的表单。您需要保持Class
Scoped实例变量以保存您之前可见的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
<强> Form1中强>
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的addnake Form,当你加载你的NewProfile表格时它是可见的,即使你把对象设置为空它仍然是活动的和可见的。当您单击按钮时,您将创建一个addnake Form的newsnake对象,该对象将掩盖您在Forms构造函数中创建的类级别newsnake,通过显示它您无法显示它因此您将永远不会看到TextBox更改。我不确定你的程序流程是什么,但看起来你想要在最后一个Form关闭后关闭程序,因为你在更新newsnake后关闭了你的启动表单。
我现在的问题是你想要oForm
做什么?你希望看到TextBox改变的形式是什么?如果是这样,你需要使它成为模块范围的变量而不是newsnake并使用它。如果没有,你正在像我展示的那样在Forms构造函数中创建newsnake,但是你也在Button1 click eventhandler中创建了一个本地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