vb.net Visual Studio 更改另一个表单上的文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18905776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Change Text Box On Another Form
提问by James Conroy
I am making a program where when you enter a Profile Name (in TextBox1) and click the add button. I want it to add whatever was entered TextBox1 to another text box (TextBox2) on another form open in the background. The other form is called addsnake (don't ask). I get no build errors but the text box on the addsnake form (TextBox2) doesn't change. This is my code in the button press:
我正在制作一个程序,当您输入配置文件名称(在 TextBox1 中)并单击添加按钮时。我希望它将输入的任何内容 TextBox1 添加到另一个在后台打开的表单上的另一个文本框 (TextBox2)。另一种形式称为addsnake(不要问)。我没有收到构建错误,但 addnake 表单 (TextBox2) 上的文本框没有改变。这是我在按钮按下时的代码:
Dim newsnake As New addsnake
TextBox1.Text = newsnake.TextBox2.Text
And yes I do have both text boxes set to Public.
是的,我确实将两个文本框都设置为公共。
First Form (newprofile)
第一表格 ( 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
Second Form (addsnake)
第二种形式(addnake)
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
回答by Mark Hall
As I said in my comment, it looks like you are creating a separate addsnakeForm, without showing it to make it visible most likely it is not the one that you want to see your text in. You need to keep a ClassScoped instance variable to hold the instance of addsnakethat you made visible earlier. Also as Viji stated you are assigning the value of newsnake.TextBox2 to TextBox1 not the other way as you stated that you wanted. Something like this example should give you an idea.
正如我在评论中所说,您似乎正在创建一个单独的addsnake表单,但没有显示它以使其可见,很可能它不是您想要在其中查看文本的表单。您需要保留一个ClassScoped 实例变量来保存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
But personally I would not make any of my form's internals public, I would prefer to use properties to access the internals something like this, I am just showing the changes.
但就我个人而言,我不会公开任何表单的内部结构,我更喜欢使用属性来访问内部结构,我只是展示了更改。
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
表格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
Edit after seeing your code.
看到你的代码后编辑。
What I noticed is you are creating the addsnake Form called oForm that is visible when you load your NewProfile Form, even though you set the object to nothing it is still active and visible. When you click your button you create a newsnake object of your addsnake Form which will mask the class level newsnake you created in the Forms constructor which you do not make visible by showing it therefore you will never see the TextBox change. I am uncertain exactly what your program flow is but it looks like you are wanting the program to close after the last Form is close because you are closing your startup Form after you update newsnake.
我注意到您正在创建名为 oForm 的 addnake 表单,该表单在您加载 NewProfile 表单时可见,即使您将对象设置为空,它仍然处于活动状态且可见。当您单击按钮时,您会创建一个 addsnake 表单的 newsnake 对象,它将掩盖您在 Forms 构造函数中创建的类级别 newsnake,您不会通过显示它来使其可见,因此您永远不会看到 TextBox 更改。我不确定您的程序流程到底是什么,但看起来您希望程序在最后一个表单关闭后关闭,因为您在更新 newsnake 后关闭了启动表单。
My question right now is what do you want oFormto do? is that the form you wish to see the TextBox changed in? if so you need to make it a Module scoped variable instead of newsnake and use that. If not you are newing up newsnake in the Forms constructor like I showed but you are also creating a local newsnake variable in your Button1 click eventhandler which is null, you need to get rid of that line and show the newsnake Form somewhere.
我现在的问题是你想做oForm什么?这是您希望看到 TextBox 更改的表单吗?如果是这样,您需要使其成为模块范围的变量而不是 newsnake 并使用它。如果不是,您像我展示的那样在 Forms 构造函数中新建 newsnake,但您还在 Button1 单击事件处理程序中创建了一个本地 newsnake 变量,该变量为空,您需要摆脱该行并在某处显示 newsnake 表单。
Try changing your Button click event to this.
尝试将您的按钮单击事件更改为此。
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

