How to Call usercontrol in Windows form - vb.net

I am working with windowsForm in vb.Net.
I have one main form(frmMaster) that has buttons on the left side and one panel(PanelDetail) control on the right side.
I have two user controls, let say uc1 and uc2.
When I clicked the button from main form, it opens the uc1 in the panel properly through below code.
PanelDetail.Controls.Add(uc1)
uc1.Show()
There is a button control on uc1 and i wants to open uc2, when user clicks on the button from uc1.
So i have to add the uc2 to the PanelDetail and hide or remove the uc1. To do this, i have created the public method in the frmMaster
Public Sub DisplayControl(ControlName As UserControl)
PanelDetail.Controls.Clear()
PanelDetail.Controls.Add(ControlName)
ControlName.Show()
End Sub
and call this method from the button click event of the uc1 to call uc2
frmMaster.DisplayControl(frmMaster.vuc2)
uc2 is already declared as shared in the main form as
Public Shared vuc2 As New CtrlLeavesList
So when i click the button from uc1, it clears the uc1 from panel, but it does not load the uc2.
can anyone suggest me the right way to do this.
Thanks.

As OneFineDay said, you can declare an event in uc1.
Public Event RemoveButtonClicked()
Then in the event handler for the actual button on the user control (uc1), you raise the event.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent RemoveButtonClicked()
End Sub
Then in the main form, you would declare an instance of the uc1, and register a handle for the event.
Public WithEvents uc1 As New CtrlLeavesList
Public Sub uc1_RemoveButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles uc1.RemoveButtonClicked
DisplayControl(uc2)
End Sub

I have fixed the issue by using below code.
You need to define the public event as below in uc1
Public Event RemoveButtonClicked As EventHandler
and then raise the event in button click of the uc1
Private Sub btnApplyLeave_Click(sender As System.Object, e As System.EventArgs) Handles btnApplyLeave.Click
RaiseEvent RemoveButtonClicked(sender, e)
End Sub
Then in the main form, you would declare an instance of the uc1, and register a handle for the event.
Public Shared uc1 As New CtrlLeavesList
Public Shared uc2 As New CtrlApplyforLeave
Then most important you need to add the handler on the form load of the main form
AddHandler vCtrlLeavesList.RemoveButtonClicked, AddressOf vCtrlLeavesList_RemoveButtonClicked
and then register the event
Public Sub vCtrlLeavesList_RemoveButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)
PanelDetail.Controls.Add(uc2)
uc2.Show()
End Sub
The above example is working perfectly for me.

Related

How to raise event from user control to another form vb.net

I am not familiar with user controls. I have user control with OK and Cancel buttons, I add this user control to an empty form during run time, I have another form which I called "Main Form", In the code I open the empty form and add the user control, after that I want to raise an event (on the OK button) from the user control (or from the empty form I don't know!) to the Main form!
I searched the net and found way to create an event and raise the event in the same form. I tried to do the same thing between different forms but I don't know how to do that.
create event
Public Event OKEvent As EventHandler
raise event
Protected Overridable Sub OnbtnOKClick(e As EventArgs)
AddHandler OKEvent , AddressOf btnOKClickHandler
RaiseEvent OKEvent(Me, e)
End Sub
event Sub
Sub btnOKClickHandler(sender As Object, e As EventArgs)
'Event Handling
'My event code
End Sub
handle my event on btnOK.click event
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
OnbtnOKClick(e)
End Sub
Okey, that all code works in the same form, maybe its messy but that what I found on the net, I want to make similar thing but on different forms, how can I organize my code?
You don't raise an event "to" anywhere. The whole point of events is that you don't have to care who is listening. It's up to the whoever wants to react to the event to handle it and whoever raised the event never has to know about them.
So, all you need to do in this case is have the main form handle the appropriate event(s) of the user control. The user control is a control like any other and the event is an event like any other so the main form handles the event of the user control like it would handle any other event of any other control. Where you create the user control, use an AddHandler statement to register a handler for the event you declared in the user control.
EDIT:
The OnbtnOKClick method that you have shown above should not look like this:
Protected Overridable Sub OnbtnOKClick(e As EventArgs)
AddHandler OKEvent , AddressOf btnOKClickHandler
RaiseEvent OKEvent(Me, e)
End Sub
but rather like this:
Protected Overridable Sub OnbtnOKClick(e As EventArgs)
RaiseEvent OKEvent(Me, e)
End Sub
Also, the btnOKClickHandler method should be in the main form, not the user control. As I said in my answer, it's the main form that handles the event. That means that the event handler is in the main form. As I also said, when you create the user control in the main form, that is where you register the event handler, e.g.
Dim uc As New SomeUserControl
AddHandler uc.OKEvent, AddressOf btnOKClickHandler
As I hope you have absorbed in your reading, if you use AddHandler to register an event handler then you need a corresponding RemoveHandler to unregister it when the object is finished with.
If I'm understanding correctly, you have...
FormA creates FormB.
FormB creates UserControlB.
UserControlB raises "OK" event.
FormB receives "OK" event.
...but you want an additional step of:
FormA receives "OK" event.
The problem here is that FormA has no reference to UserControlB because FormB was the one that created the UserControl. An additional problem is that FormB may have no idea who FormA is (depending on how you've got it setup).
Option 1 - Pass References
If you want both FormB and FormA to respond to a SINGLE "OK" event (generated by the UserControl), then you'd have to somehow have a reference to all three players in the same place so that the event can be properly wired up. The logical place to do this would be in FormB as that is where the UserControl is created. To facilitate that, however, you'd have to modify FormB so that a reference to FormA is somehow passed to it. Then you can wire up the "OK" event directly to the handler in FormA when FormB creates its instance of UserControlB:
Public Class FormA
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frmB As New FormB(Me) ' pass reference to FormA into FormB via "Me" and the Constructor of FormB
frmB.Show()
End Sub
Public Sub ucB_OKEvent()
' ... do something in here ...
Debug.Print("OK Event received in FormA")
End Sub
End Class
Public Class FormB
Private _frmA As FormA
Public Sub New(ByVal frmA As FormA)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_frmA = frmA ' store reference to FormA so we can use it later
End Sub
Private Sub FormB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ucB As New UserControlB
ucB.Location = New Point(10, 10)
AddHandler ucB.OKEvent, AddressOf _frmA.ucB_OKEvent ' wire up the "OK" event DIRECTLY to the handler in our stored reference of FormA
Me.Controls.Add(ucB)
End Sub
End Class
Public Class UserControlB
Public Event OKEvent()
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
RaiseEvent OKEvent()
End Sub
End Class
Option 2 - "Bubble" the Event
Another option is "bubble" the event from FormB up to FormA. In this scenario, FormB will have no idea who FormA is, so no reference will be passed. Instead, FormB will have its own "OK" event that is raised in response to receiving the original "OK" event from UserControlB. FormA will get the "OK" notification from the UserControl, just not directly:
Public Class FormA
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frmB As New FormB
AddHandler frmB.OKEvent, AddressOf frmB_OKEvent
frmB.Show()
End Sub
Private Sub frmB_OKEvent()
' ... do something in here ...
Debug.Print("OK Event received from FormB in FormA")
End Sub
End Class
Public Class FormB
Public Event OKEvent()
Private Sub FormB_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ucB As New UserControlB
ucB.Location = New Point(10, 10)
AddHandler ucB.OKEvent, AddressOf ucB_OKEvent
Me.Controls.Add(ucB)
End Sub
Private Sub ucB_OKEvent()
Debug.Print("OK Event received from UserControl in FormB")
RaiseEvent OKEvent()
End Sub
End Class
Public Class UserControlB
Public Event OKEvent()
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
RaiseEvent OKEvent()
End Sub
End Class
Design Decisions
You have to make a design decision here. Who is the source of the "OK" event? Should it be the UserControl or FormB? Will the UserControl ever be used in different forms (other than FormB)? Will FormB ever be used with Forms other then FormA? These answers may help you decide which approach is better...or may lead you to rethink how you've designed your current solution; you may need to change it altogether.

VB.NET User control Referencing Form

I have a form (frmwizard) which I am using to create a wizard like interface. The form contains a usercontrol and a button (for testing). There is also a function on the form called "NextPage"
The form loads a usercontrol (ucpage1) on load and the usercontrol has a button on it that when clicked attempts to call a function on the main form as per below:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frmWizard.Nextpage(me.name)
End Sub
Within my function called "NextPage" have the following for testing.
Public Sub NextPage(ByVal CurrentPage As String)
MessageBox.Show(UserControl1.Controls.Count)
End Sub
When I call the function from the form itself (via the button) I get the result of 1, when i call the function via the User Control, I get the result as 0
I'm sure there is something simple I need to do, but i'm unsure what i've overlooked.
I am trying to make the button on the user-control Save the data within the control and then to browse to the next wizard page. Hopefully this is enough information
Codependance is a bad idea, as it locks the two types together for the future. If your user control really needs to invoke the form, you should instead have it raise an event and handle the event in your form.
Public Class MyUserControl
Public Event OnNextPage(ByVal sender As Object, ByVal e As EventArgs)
Private Sub btnNextPage_Click(sender As Object, e As EventArgs) Handles btnNextPage.Click
RaiseEvent OnNextPage(Me, New EventArgs)
End Sub
End Class
Public Class Form1
Private Sub MyUserControl_OnNextPage(sender As Object, e As EventArgs) Handles MyUserControl1.OnNextPage ' , MyUserControl2.OnNextPage, etc...
MessageBox.Show(DirectCast(sender, MyUserControl).Controls.Count)
End Sub
End Class

How does one override the OnShown Event of a form when creating a form programmatically?

I am trying to get a sound to play when a form is first shown (rather like a standard message box does for want of a better example). If using a standard form added through the designer I would generally do this by overriding the standard onshown event and then go on to call MyBase.OnShown(e)
The problem I've hit now is that the form is being created programmatically (Dim myForm as new Form etc) and as such I seem not to be able to use AddHandler to override this event. I don't doubt that I'm doing this in entirely the wrong way, but I'd appreciate any advice that can be offered. I would prefer advice from the perspective of VB.net, but I can just about muddle through in C#.
Form.OnShown is not an event. Rather, it is a method of the Form class which raises the form's Shown event. Here's the MSDN article that explains the OnShown method:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onshown.aspx
When you are making a derived class by using the form designer, you can override the OnShown method, but when you are simply accessing a form through its public interface, you need to use the Shown event instead. You can add an event handler for that event like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As Form1 = New Form1()
AddHandler f.Shown, AddressOf f_Shown
f.Show()
End Sub
Private Sub f_Shown(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Since the form doesn't exist in code, you would actually have to call the event then.
Try writing out the showing code first:
Public Sub form_Showing(ByVal sender As Object, e As EventArgs)
// play sound
End Sub
then when you create your form, you add the handler of the event:
Dim f As New Form
AddHandler f.Shown, AddressOf form_Showing
f.Show()

VB.NET MDI Child set other child property

I hope you can help my trouble.
I have 1 form as parent MDI (frmParent.vb) and have 2 child form (frmChild01.vb & frmChild02.vb).
the code for parent form as per below.
Private Sub OpenChild01ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenChild01ToolStripMenuItem.Click
Dim child01 As frmChild01
child01 = New frmChild01()
child01.MdiParent = Me
child01.Show()
End Sub
Private Sub OpenChild02ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenChild02ToolStripMenuItem.Click
Dim child02 As frmChild02
child02 = New frmChild02()
child02.MdiParent = Me
child02.Show()
End Sub
frmChild01 have button1
frmChild02 have label1
My problem is how can I set label1.text when user click button1
Thanks in advance...
There are a lot of creative ways you can do this; but ultimately you need to provide a communication channel between Child1 and Child2.
The most direct way would be to Pass a Reference of frmChild02 to frmChild01. You'd need label1 to be public so that frmChidl02 can access it (or you can provide a public method to handle the setting.
That only works if you have a reference to frmChild02 when you create frmChild01. Since you seem to have individual buttons to launch those forms, it might be more complicated. One way to handle this would be to use Events to handle the communication. Have your Mdi Parent listen for/raise events from the child forms. So, when you click the button in frmChild01 have your Mdi parent listen for that event and raise a new event called 'ButtonClickInForm1' or something similar. Have frmChild02 subscribe to that event. If there is an instance of frmChild02 it will respond to the button click and update it's label.
You need to check if ChildForm02 is already loaded. If not you need to load it before you can set its label's text property. It might look something like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If MDIParent1.ChildForm2 Is Nothing OrElse MDIParent1.ChildForm2.Visible = False Then
MDIParent1.ChildForm2 = New Form2
MDIParent1.ChildForm2.MdiParent = MDIParent1
MDIParent1.ChildForm2.Text = "Window "
MDIParent1.ChildForm2.Show()
End If
MDIParent1.ChildForm2.Label1.Text = "your text here"
End Sub
You also need to declare the child forms as Public in MdiParent form so that you can access it anywhere within the solution.
Public ChildForm1 As Form1
Public ChildForm2 As Form2

Click a specific button on another form

Consider i am having two forms, form1 and form2
How can i click,mouse over(any events) a specific button on another form using coding in vb.net?
I'm assuming that Form1 launches Form2, since there's not a whole lot of information in the description.
When Form1 launches, there are two buttons: "button1" and "Launch Form 2" (forgot to change text on button1, sorry. :(
When you click "Launch Form 2", Form2 pops up:
Clicking the "button1" on Form1, a message box originating from Form1 pops up saying:
Clicking the "button1" on Form2, a message box ALSO originating from Form1 pops up saying:
Here's the code:
Form1
Public Class Form1
Private WithEvents frm2 As New Form2
Private Sub Form1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1Button.Click
RunSomeCode("Called from form 1!")
End Sub
Public Sub RunSomeCode(ByVal message As String)
MessageBox.Show(message)
End Sub
Private Sub Form1LaunchForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1LaunchForm2Button.Click
frm2.Activate()
frm2.Show()
End Sub
Private Sub frm2_SimulateForm1ButtonClick() Handles frm2.SimulateForm1ButtonClick
RunSomeCode("Called from form 2!")
End Sub
End Class
Form2
Public Class Form2
Public Event SimulateForm1ButtonClick()
Private Sub Form2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2Button.Click
RaiseEvent SimulateForm1ButtonClick()
End Sub
End Class
How it works
Form 2 has a public event called "SimulateForm1ButtonClick". That event can be raised whenever you want, from any code block. I just decided to raise it when I click the button on the form.
Form 1 has an instance of Form2 WithEvents. It's very important that you use the WithEvents keyword, or that public event in Form2 won't show up. :(
Form 1 has a sub that handles the "SimulateForm1ButtonClick" that is raised when Form2 clicks its button.
Now, here's another important detail: The code executed when button1 is clicked on Form1 is actually in a private sub called RunSomeCode(). This is important, because it makes the code accessible from any other part of Form1, namely the part that handles Form2's event.
I hope that helps you out a little bit. I'm not sure exactly what you were asking. :/
Code: http://darin.hoover.fm/code/dl/FormsSandbox.zip
If you are trying to fire the event, just use Form2.Button1.PerformClick() assuming that the button on form 2 is called 'button1'.