IT Community - Software Programming, Web Development and Technical Support

User control problem

This is a discussion on User control problem within the C# Programming forums, part of the Software Development category; User control problem created user control in my project and have a button and a textbox.. i created property for ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 05-27-2009, 08:08 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default User control problem

User control problem

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...
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 05-27-2009, 08:10 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: User control problem

Hi,

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 property with 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?
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 05-27-2009, 08:12 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default Re: User control problem

Hi,

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
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 05-27-2009, 08:18 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: User control problem

Hi,

Thanks for your further information. The cause 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
Public 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

End 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
Public 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

End Class



The code which is in bold sytle most needs your attention.



If you have further problems,
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 05-27-2009, 08:55 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default Re: User control problem

Hi,

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..


Code Snippet

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.UserControl

Windows 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.
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 05-27-2009, 09:14 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: User control problem

Hi,

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 little to 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
Public Class Form1

Sub New()

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

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

End Class




UserControl Class Code:



Code Snippet
Public Class UserControl1

Dim form1Ref As Form1

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.

form1Ref = hostFormRef

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBox.Show(form1Ref.TextBox_Text)

End Sub

End Class



If the problem can’t be solved, please don’t hesitate to tell me.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 05-27-2009, 09:15 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default Re: User control problem

Hi,

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
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 05-27-2009, 09:15 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: User control problem

Hi,

In the form class, did you add this “Dim uc1 As UserControl1 = New UserControl1(Me)”, If not, you will get the exception when you try to use “MessageBox.Show(form1Ref.TextBox_Text)”. Because you didn’t pass the reference of the current form instance to the usercontrol.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
issue in asp.net user control for message box illianafrazer ASP and ASP.NET Programming 0 09-07-2009 05:54 AM
How to create User Control in asp.net? poornima ASP and ASP.NET Programming 7 04-18-2008 08:01 PM
Treeview control problem: Selected node is not retained when parent node is closed. bluesky ASP and ASP.NET Programming 1 01-23-2008 04:11 AM
use of Custom Control and User Control? a.deeban ASP and ASP.NET Programming 1 08-20-2007 07:25 AM
I have a TreeView control in one of my webpages and I am facing the following problem itbarota C# Programming 1 07-27-2007 04:33 AM


All times are GMT -7. The time now is 11:04 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0