|
i created user control in my project and have a button and a textbox.. i created property for the textbox in the user control
in my windows form i have 2 textboxes and a button... i place the user control in my form... then i accessed the user control textbox in the button event click.. and it works perfectly..
then i add a property of the 2 textboxes in the windows form,
then in the user control codes... i use the button event click . i instantiated the windows form and use the property of the textbox from the windows form. but when i click the button from the user control... it didn't get the inputted text from the windows form...
what is the problem? Please help... or what the code that i need to do..
thanks.
| |
|
|
Report Abuse | | |
| rs03 Saturday, November 15, 2008 6:49 AM |
Hi rs03,
From your description, I got to know you instantiated a new windows Form and use the property of the TextBox in the Windows form instance.
I guess you were inputting in the current form, but get the TextBox propertywith the new Windows Form instance which may not be shown. If you were using C# language, you can try to use this.TextBox.Property to get the input. Else if you were using VB.NET language, you can try to use Me.TextBox.Property to get the input.
If it is not as I said, would you please post some of your codes? And let me know how you did that.
Best regards,
Bruce Zhou
|
| Bruce.Zhou Monday, November 17, 2008 7:51 AM |
Hi rs03,
Thanks for your further information. Thecause is covered in my previous post.
Actually, in the UserControl, you instantiated a new frmName form. The textBox you are inputting is the current form. They are not the same instances. So you may not get the input from the TextBoxes.
To solve this problem.We can overload the construtor of the UserControl like the following.
Code Snippet
Class UserControl1
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Sub New(ByVal hostFormRef As Form1)
Me.New() ' invoke the default constructor to initialize component
' This call is required by the Windows Form Designer.
' Add any initialization after the InitializeComponent() call.
MessageBox.Show(hostFormRef.TextBox_Text)
End Sub
Class
In a button click event of the host form, we can pass the instance of the current form to the UserControl. As a result, what you input in the TextBox will be used in the UserControl.
Code Snippet
Class Form1
Public Property TextBox_Text() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal value As String)
Me.TextBox1.Text = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim uc1 As UserControl1 = New UserControl1(Me)
End Sub
Class
The code which is in bold sytle most needs your attention.
If you have further problems, please feel free to let me know.
Best regards,
Bruce Zhou
|
| Bruce.Zhou Saturday, November 22, 2008 4:38 AM |
Hi rs03,
From your description, I got to know you instantiated a new windows Form and use the property of the TextBox in the Windows form instance.
I guess you were inputting in the current form, but get the TextBox propertywith the new Windows Form instance which may not be shown. If you were using C# language, you can try to use this.TextBox.Property to get the input. Else if you were using VB.NET language, you can try to use Me.TextBox.Property to get the input.
If it is not as I said, would you please post some of your codes? And let me know how you did that.
Best regards,
Bruce Zhou
|
| Bruce.Zhou Monday, November 17, 2008 7:51 AM |
hi bruce,
in the windows form(frmName) or the host form i place the property of the textbox...
Code Snippet
Public Property Name() As String Get Return txtName.Text End Get Set(ByVal Value As String) txtName.Text = Value End Set End Property
in the usercontrol button i place the code...
Dim frmName As New frmName MessageBox.Show(frm.Name)
when i clicked the button from the usercontrol it displays nothing.... |
| rs03 Saturday, November 22, 2008 3:58 AM |
Hi rs03,
Thanks for your further information. Thecause is covered in my previous post.
Actually, in the UserControl, you instantiated a new frmName form. The textBox you are inputting is the current form. They are not the same instances. So you may not get the input from the TextBoxes.
To solve this problem.We can overload the construtor of the UserControl like the following.
Code Snippet
Class UserControl1
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Sub New(ByVal hostFormRef As Form1)
Me.New() ' invoke the default constructor to initialize component
' This call is required by the Windows Form Designer.
' Add any initialization after the InitializeComponent() call.
MessageBox.Show(hostFormRef.TextBox_Text)
End Sub
Class
In a button click event of the host form, we can pass the instance of the current form to the UserControl. As a result, what you input in the TextBox will be used in the UserControl.
Code Snippet
Class Form1
Public Property TextBox_Text() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal value As String)
Me.TextBox1.Text = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim uc1 As UserControl1 = New UserControl1(Me)
End Sub
Class
The code which is in bold sytle most needs your attention.
If you have further problems, please feel free to let me know.
Best regards,
Bruce Zhou
|
| Bruce.Zhou Saturday, November 22, 2008 4:38 AM |
Code Snippet
i used the code but it doesnt work... i dont know if i did something wrong..
i will post another code to clarify my problem..
Public Class Form1 Inherits System.Windows.Forms.Form
Windows Form Designer generated code
Public Property Name() As String
Get
Return txtName.Text
End Get
Set(ByVal Value As String)
txtName.Text = Value
End Set
End Property
End Class
Public Class UserControl1 Inherits System.Windows.Forms.UserControlWindows Form Designer generated code
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm as new Form1
MessageBox.Show(frm.Name)
End Sub End Class
i place the overload contructor in form1 just like what you say... but the button click event must be in the usercontrol.. so when i click the user control it will display the text in the textbox of the form1.
|
| rs03 Saturday, November 22, 2008 4:58 AM |
Hi rs03,
If you want the button in the UserContorl, it is also Ok. But the code is a little bit different.
Here I change the code a littleto fit for your case, and in the UserControl class I add a member to store the referrence of current Form1 instance.
Form1 Class Code:
Code Snippet
Class Form1
Sub New()
' This call is required by the Windows Form Designer.
Dim uc1 As UserControl1 = New UserControl1(Me)
Me.Controls.Add(uc1)
' Add any initialization after the InitializeComponent() call.
End Sub
Public Property TextBox_Text() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal value As String)
Me.TextBox1.Text = value
End Set
End Property
Class
UserControl Class Code:
Code Snippet
Class UserControl1
Dim form1Ref As Form1
Sub New()
' This call is required by the Windows Form Designer.
' Add any initialization after the InitializeComponent() call.
End Sub
Sub New(ByVal hostFormRef As Form1)
Me.New() ' invoke the default constructor to initialize component
' This call is required by the Windows Form Designer.
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Class
If the problem can’t be solved, please don’t hesitate to tell me.
Best regards,
Bruce Zhou
|
| Bruce.Zhou Saturday, November 22, 2008 5:42 AM |
there is an error in the messageBox " Object reference not set to an instance of an object."
what is the thing i forgot to do.....
thanks for the help
|
| rs03 Saturday, November 22, 2008 6:04 AM |
In the form class, did you add this �/span>Dim uc1 As UserControl1 = New UserControl1(Me)� If not, you will get the exception when you try to use �/span>MessageBox.Show(form1Ref.TextBox_Text)� Because you didn’t pass the reference of the current form instance to the usercontrol.
Best regards,
Bruce Zhou |
| Bruce.Zhou Saturday, November 22, 2008 7:28 AM |
yes I did, but it still have an error...
thanks...
|
| rs03 Saturday, November 22, 2008 7:46 AM |
Ok,would you please send me a repro? See my profile for my email address.
|
| Bruce.Zhou Saturday, November 22, 2008 7:55 AM |