Call function outside user control - vb.net

I need help to call a function in a usercontrol that is shown in a panel within the form, so far this is i tried, but no luck, i can't still get the text inputted on the textbox
Public Class Form1
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Dim ustudent As New StudentAdd
ustudent.Dock = DockStyle.Fill
SplitContainer1.Panel2.Controls.Add(ustudent)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
ustudent.SaveData()
End If
End Sub
End Class
in user control have some textbox
textbox1 and textbox2
Public Class StudentAdd
Public Sub SaveData() As Boolean
'just testing whether it could work well
' getting textbox value
MsgBox(TextBox1.Text)
End Sub
End Class

But ustudent is a local var, try to declare it outside link_clicked event. Do you want to create multiple user controls in the win or just one?
For one you could add it at design time by dragging from the your project components panel
For more, you should implement some logic to identify the selected component and make it available for save data. If you want to save all just enumerate components in Panel2 (of type StudentAdd) and call the method

Related

Add combobox items from another usercontrol form using textbox

I have a usercontrol form named "ucSETTINGS", where there is a textbox and once the button was clicked, the text inside the textbox will be added to the combobox from another usercontrol form name "ucITEMS"
I tried this code but it's not working
(cboCategory is the name of the combobox from ucITEMS, txtNAME is the textbox from ucSETTINGS)
Private Sub btnSAVE_Click(sender As Object, e As EventArgs) Handles btnSAVE.Click
Dim category As New ucITEMS()
category.cboCATEGORY.Items.Add(txtNAME.Text)
End Sub
Can someone help me?
In this sort of situation, the user controls don't know about each other by default and it should stay that way. The source UC just exposes an interface and lets whomever is watching use that as it sees fit. That means raising an event when something happens and exposing required data via properties, e.g.
Public Class SourceControl
Public ReadOnly Property TextBox1Text As String
Get
Return TextBox1.Text
End Get
End Property
Public Event Button1Click As EventHandler
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OnButton1Click(EventArgs.Empty)
End Sub
Protected Overridable Sub OnButton1Click(e As EventArgs)
RaiseEvent Button1Click(Me, e)
End Sub
End Class
The Text of the TextBox is exposed via a property and, when the user clicks the Button, the UC raises an event.
The destination UC provides an interface for new items to be provided but it adds them to its own ComboBox, e.g.
Public Class DestinationControl
Public Sub AddItemToComboBox1(item As Object)
ComboBox1.Items.Add(item)
End Sub
End Class
The form then plays go-between, handling the event, getting the property and calling the method:
Private Sub SourceControl1_Button1Click(sender As Object, e As EventArgs) Handles SourceControl1.Button1Click
DestinationControl1.AddItemToComboBox1(SourceControl1.TextBox1Text)
End Sub
Obviously you would use something more specific and appropriate than my generic naming.

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

One button to control another button on many user controls

Hey and sorry for another strange question...
I have 25 UserControls with a Start_Button on each of them - this Start_Button can either be Visible or not depending on whether the UserControl is active. On my form1 I have a Start_All button.
I would like to simulate a click of all the UserControl's Start_Buttons which are visible only.
Instead of simulating click-events expose a method for the start-functionality and call this method from the Start_Button.Click-event. Then you can use this method from wherever you want. On this way your code remains readable and reusable.
You should also provide an Active property in your UserControl which you can simply link to your start-button's Visible-property.
Presuming that the user-controls are in a container-control like Panel:
Public Sub StartAll()
Dim allActiveUserControls =
From uc In controlPanel.Controls.OfType(Of MyUserControlType)()
Where uc.Active
For Each uc In allActiveUserControls
uc.Start()
Next
End Sub
Here is an example for the Active property:
Public Property Active As Boolean
Get
Return StartButton.Visible
End Get
Set(value As Boolean)
StartButton.Visible = value
End Set
End Property
and here are the Start method and the event-handlers:
Public Sub Start()
' Do Something ... '
End Sub
Private Sub StartButton_Click(sender As System.Object, e As System.EventArgs) Handles StartButton.Click
Start()
End Sub
Private Sub Start_All_Click(sender As System.Object, e As System.EventArgs) Handles Start_All.Click
StartAll()
End Sub

Passing variables between windows forms in VS 2010

I have two forms. Form 1 allows the user to pick an employee from a dropdown combo box. That employee is then passed onto Form 2 where the user enters additional information regarding that employee. That data will then be passed into a SQL table.
On form 1 I have:
Dim ChangeJobInfo As New Form2(Employee)
ChangeJobInfo.Show()
On Form 2 I have:
Public Sub New(ByVal Employee As String)
MsgBox(Employee)
End Sub
The variable passes just fine. The issue is that nothing shows up on the new form. When I setup Form2, I added a combobox, date picker, two text boxes, submit button, etc., but when the form loads it is completely blank. No errors, the MsgBox returns the right result, but none of my gui elements show up. If I change the code on form 1 to Form2.show() I see the form as laid out in the designer.
Any ideas on how to get those items to show up?
Change your code in Form2.vb for the New sub to this:
Public Sub New(ByVal Employee As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
MsgBox(Employee)
End Sub
If you don't call InitializeComponent(), your complete GUI is not going to render.
You don't even have to use the InitializeComponent or New functions.
I have made an example to show how easily this can be done.
Clicking "Show Form" results in the below:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
which is simply used to display the second form.
By clicking "Pass Data" results in the following code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Form2.Label1.Text = TextBox1.Text
End Sub
As shown above you can pass the data directly from control to control. The same idea can be used with variables too.
I am late but I think this answer can help.
For example, Form1 named "menu" opens and passes variable to Form2 named "ordine".
The variable to pass is "hotel"
In menu on button_click
Dim ordinef As New Ordine()
If ordinef Is Nothing Then
'control that form is not opened yet if open close before
ordinef = New Ordine()
Else
ordinef.Close()
ordinef = New Ordine()
End If
ordinef.hotel = hotel
ordinef.Show()
In Form2 (Ordine):
Private Sub Ordine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
public hotel as string
msgbox hotel
That's it!!

Multi parent form

I've a vb .net winform that is show by others forms. I've frmA.vb, frmB.vb, frmC.vb and frmD.vb.
This all forms can call frmItem.vb.
frmItem.vb allows the user to select an item from a Database, this item is sent by calling a Set Property on the parent.
i.e.
I open frmA, click on button (something like:)
fi = new frmItem(frmA) 'frmItem has 4 New() methods, frmA.. b... c and d
'i need to pass the correct parent.
fi.showModal()
So, when i add an item, it calls
fA.addItem(item_id)
It works OK, my doubt is about optimization, because i've duplicated frmItem; one copy managed frmA and frmB, and the other one, frmC and frmD.
i.e.
in frmItem1 when i've to sent the item, i use:
private fB as frmB
private fA as frmA
if parentFrmA is nothing then
'Is frmB
fB.addItem(item_id)
else
'Is frmA
fA.addItem(item_id)
end if
And, on frmItem2:
private fC as frmC
private fD as frmD
if parentFrmC is nothing then
'Is frmD
fD.addItem(item_id)
else
'Is frmC
fC.addItem(item_id)
end if
If i modify frmItem1, i've to modify frmItem2 and viceversa, because they should look and act like one.
All four forms, have the same Set Property, but like they're differents forms, i can't use a unique Form class in frmItem.
Is the posibility that one form, can manage multi parents in an easy way??
If you need more info, let me know. Thanks
I can't completely follow your example since, well, I think it's just hard to follow.
But in general, it sounds like these child forms should just be raising an event that the parent form is listening for. That way, you can separate your concerns a bit and not hardcode these dependencies.
You can try making your own EventArgs class to follow best practices:
Public Class ChildFormEventArgs
Inherits EventArgs
Private _ItemID As Integer
Public Sub New(ByVal itemID As Integer)
_ItemID = itemID
End Sub
ReadOnly Property ItemID() As Integer
Get
Return _ItemID
End Get
End Property
End Class
Your child forms would have a public event and you would raise it when ever this "added" thing happens:
Public Class Form2
Public Event ItemAdded(ByVal sender As Object, ByVal e As ChildFormEventArgs)
Private _ItemID as Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RaiseEvent ItemAdded(Me, New ChildFormEventArgs(_ItemID))
End Sub
End Sub
And then your parent form is the one listening and can act accordingly:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Using testForm As New Form2()
AddHandler testForm.ItemAdded, AddressOf ChildForm_ItemAdded
testForm.ShowDialog(Me)
RemoveHandler testForm.ItemAdded, AddressOf ChildForm_ItemAdded
End Using
End Sub
Private Sub ChildForm_ItemAdded(ByVal sender As Object, ByVal e As ChildFormEventArgs)
'// do something here.
'// sender is the child form that called it
'// e is the event arguments that contains the ItemID value
End Sub