How do I use the User-Controls as a form within a Form?

This is my answer to my problem located on this thread

As seen in the original thread's solution, I've used solution #2. Instead of switching forms, I've used switching panels and the controls are contained in those panels. This way, there will be no fade transition between forms. I found this solution a bad practice and becomes unmanageable due to the number of controls in the Design window.

As I've read on CodeDoctor's post, I have found another way of switching Forms within a Form and actually be manageable in terms of OOP design.

Proposed Solution: Use User-Controls as the new panel, "A Form within A Form". In this method I can switch between forms without actually switching the form just the User-Control.

Does anyone have a simple tutorial for newbies using User Control?. Or anyone can just reply with a step by step instruction?

UPDATE:

Ok, I am finally getting it. But how do you edit values of controls within a User Control

This code does NOT work

UserControl1.Textbox1.Text = "New Text"

Open up the designer file for your user control. The head of the file will look like:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class UserControl1
    Inherits System.Windows.Forms.UserControl

Paste the contents here. It should let you do what you are describing. I created a user control, added a panel, and put a button and a textBox inside of the control and this works:

Dim uc As New UserControl1()
		uc.Button1.Text = "abc"
		uc.TextBox1.Text = "abc"

[edit]
Oh -- You are using the class name of the user control, ie UserControl1 . You need to use the instance name where you declared an instance of that class.
[/edit]

Edited 7 Years Ago by sknake: n/a

How do I use the User-Controls as a form within a Form?

This is my answer to my problem located on this thread

As seen in the original thread's solution, I've used solution #2. Instead of switching forms, I've used switching panels and the controls are contained in those panels. This way, there will be no fade transition between forms. I found this solution a bad practice and becomes unmanageable due to the number of controls in the Design window.

As I've read on CodeDoctor's post , I have found another way of switching Forms within a Form and actually be manageable in terms of OOP design.

Proposed Solution: Use User-Controls as the new panel, "A Form within A Form". In this method I can switch between forms without actually switching the form just the User-Control.

Does anyone have a simple tutorial for newbies using User Control?. Or anyone can just reply with a step by step instruction?

User Controls you will find are easy to create, and a very modular way of creating individual pieces of functionality.

Within your VS 2008 Solution, right click on the project, and choose Add / User Control.

The designer will show a blank design surface that is sizable. Drop any controls you want to on this area.

This example will create a Logon control.
Add two labels with text properties of "Logon:" and "Password:"
Add two text boxes named txtLogon and txtPassword.
Add two Buttons, one named btnLogon the other btnCancel.

Now, add the following code directly under the Public Class UserControl1 (Assuming you didn't rename your User Control.

Public Event Logon(ByVal sUserName As String, ByVal sPassword As String)
    Public Event Cancel()

Now, under the btnLogon_Click event for your button, add the following code.

Dim sLogon As String
        Dim sPassword As String
        sLogon = Me.txtLogon.Text
        sPassword = Me.txtPassword.Text
        RaiseEvent Logon(sLogon, sPassword)

Under the btnCancel_Click add the following:

RaiseEvent Cancel()

Now, drop your UserControl on a new form. When a user fills in the Logon and Password and clicks the Logon Button, an event will be raised in your Form called Logon, passing both the Logon value and the Password value.

You will find that User Controls are your friend, because you can re-use them everywhere in a Windows form application.

Hope this helps.

@Sknake-I understand thank you

@CodeDoctor - Thank you for replying.

From the post of CodeDoctor, what is the difference of these two codes(Code1 and Code2)?

Sample Code1:

Public Event Show(ByVal myMessage As String)

    Private Sub Message(ByVal myMessage As String) Handles Me.Show
        MsgBox(myMessage)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent Show("Hello")
    End Sub

Sample Code2:

Private Sub Message(ByVal myMessage As String)
        MsgBox(myMessage)
    End Sub

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

@Sknake-I understand thank you

@CodeDoctor - Thank you for replying.

From the post of CodeDoctor, what is the difference of these two codes(Code1 and Code2)?

Sample Code1:

Public Event Show(ByVal myMessage As String)

    Private Sub Message(ByVal myMessage As String) Handles Me.Show
        MsgBox(myMessage)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent Show("Hello")
    End Sub

Sample Code2:

Private Sub Message(ByVal myMessage As String)
        MsgBox(myMessage)
    End Sub

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

If you are creating a control, and place that control in a place to implement its functionality, this is where you will use Events.

When using code behind in a form, there is no real need to declare an Event for that, you will have access to all of the private/friend/public methods and functions contained within that form.

Always keep in mind that a control is a seperate unit of work, and should be designed as such. If your control need to communicate back to where it is being used, do this through events.

Hope this helps.

If you are creating a control, and place that control in a place to implement its functionality, this is where you will use Events.

When using code behind in a form, there is no real need to declare an Event for that, you will have access to all of the private/friend/public methods and functions contained within that form.

Always keep in mind that a control is a seperate unit of work, and should be designed as such. If your control need to communicate back to where it is being used, do this through events.

Hope this helps.

What if I am not using the User-Control to communicate back?
What if I am using the User-Control as the form itself and using the actual form as a container for the User-Control to be placed?

Do I still need events or will I just use functions and subs?

What if I am not using the User-Control to communicate back?
What if I am using the User-Control as the form itself and using the actual form as a container for the User-Control to be placed?

Do I still need events or will I just use functions and subs?

Well, the answer is NO, you don't need events if you are only creating code procedures in your main form. You can simply call those procedures.

I was trying to show the concept of encapsulation of a User Control. Encapsulating what is needed to be done seperately from your main form. Have your control contain all of that logic, and simply communicate back to your Implementing form through events of the actions your control is taking.

This is an Object Oriented approach to your problem, not a code based approach.


Hope this Helps.

Thank you for all your replies, I've learned so much from this.

This question has already been answered. Start a new discussion instead.