VB.NET MDI Child set other child property - vb.net

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

Related

TabCard event when a user has the intent to change tab

Probably a simple one but a cannot figure out the correct event:
I have a vb.net WinForm with a TabControl. On every TabPage, the user can enter/modify some data and then (hopefully) save it.
To keep things clean, I want to check, if there is unsaved data, when a user changes tabs (and delete it, if not saved).
I am looking for the best event of the TabCard to do so. There is TabControl1.Selecting, .SelectedIndexChanged and .Selected which look promising but they all fire AFTER the Tab changed.
If the user wants to return to save the data, i need to figure out where he came from and show that TabPage again. Also the event would the fire again - Not practicable.
In Conlusion: I am looking for a TabControl Event, that fires after the user clicked another tabcard but before the card actually changes...
Or a better idea to solve this isse another way.
Use the Selecting event. If you don't want to change the tab page, you can cancel the event.
'Here's an example class with a tabControl
Public Class Form1
'this variable stores the currently selected tab
Private activeTab As TabPage
'this initializes the activeTab variable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
activeTab = TabControl1.SelectedTab
End Sub
'This checks to see if the tab should change or not
Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
If (MessageBox.Show(String.Format("Return to {0} tab?", activeTab.Name), "TabControl", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK) Then
e.Cancel = True
Else
activeTab = e.TabPage
End If
End Sub
End Class

Not fill the grid

Good day,
I have a problem in an MDI form, have the main form, and have 2 buttons, these buttons lead to children forms, one of the forms allows you to select a category from a database, and fills the grid within the, everything works until I I embed the form within the MDI using the button that event is as follows:
Private Sub ButtonCatego_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles ButtonCatego.ItemClick
Dim addCategory As New AddCategory
addCategory.MdiParent = Me.MdiParent
addCategory.Show()
End Sub
When i use the normal event works:
Private Sub ButtonCatego_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles ButtonCatego.ItemClick
AddCategory.Show()
End Sub
Help me please. Thank you
You are setting addCategory.MdiParent to your current form's MDI parent. If Me is your main form then it won't have an MDI parent, which is why you're not getting it to work.
Set addCategory.MdiParent to Me instead and it should resolve your issue:
Private Sub ButtonCatego_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles ButtonCatego.ItemClick
Dim addCategory As New AddCategory()
addCategory.MdiParent = Me
addCategory.Show()
End Sub

Call function outside user control

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

e.target in VB.net

When using flash, I was able to get to the focus of an event by accessing the event's "target" attribute.
so if I remember, it was something similar to.
button1.addEventListener(mouse_click, doSomething);
doSomething(e: Event){
e.target.size = 50000;
}
And I'm looking for the equivalent in VB.
If you can give me it's common name across all languages, I'd be doubly grateful. I don't quite know what to search for aside from "event.target VB.net equivalent, and that's not returning anything.
Thanks in advance.
edit: for those new to flash. By focus, I mean the physical object that was clicked on. So the example given would be accessing the clicked button's size.
In VB you can wire up event handlers declaratively using the WithEvents keyword or imperatively using AddHandler.
Private WithEvents myButton
' OR
Public Sub New
Dim newButton = New Button()
AddHandler newButton.Click, AddressOf MyClickHandler
End New
'To consume it you declare a method as follows:
' The Handles clause is used when declaring WithEvents
Private Sub MyClickHandler(sender As Object, e As EventArgs) Handles myButton.Click
' The sender has a handle on the object that raised the event (aka the button)
Dim btn = DirectCast(sender, Button)
btn.Size = New Size(500, 500)
End Sub
Got it!
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclick.aspx#Y0
Sub GreetingBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
'When the button is clicked,
'change the button text, and disable it.
Dim clickedButton As Button = sender
clickedButton.Text = "...button clicked..."
clickedButton.Enabled = False
End Sub
The first parameter (sender by default) references the focused object. You can access it as you would any other normal variable, but it's information won't appear in the auto complete list unless you set it "As" that specific data type.
So I ended up with this
Private Sub nw_btn_Click(ByVal sender As System.Windows.Forms.Button, ByVal e AsSystem.EventArgs) Handles nw_btn.Click
sender.Hide()
End Sub

how do I catch events on runtime objects

I'm creating a form with a few buttons and a combobox at runtime.
dim f as new form
(blah blah)
then the buttons acceptDescription and rejectDescription are set up...
then the combobox descriptionCombo is set up...
then...
AddHandler acceptDescription.Click, AddressOf handleAcceptedDescription
AddHandler rejectDescription.Click, AddressOf handleRejectedDescription
then I have these two methods to catch the click events... but can't figure out how to reference the other runtime generated controls. (combobox if accepted, form if rejected)
Private Sub handleAcceptedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
'stub
'will need to reference the chosen combobox value here
dim acceptedDescription as string = descriptionCombo.selectedValue .tostring
End Sub
Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
'I want to close the runtime created form here, but can't reference it
f.close()
'and return user to main form
Me.Focus()
End Sub
If the code for generating the form is in your main form, then declare the Form variable at the class level of the main form class so you can access it from the event handlers. Same goes for your combobox and text field- you need to make sure the variables are declared outside the scope of the handlers so you can reference them within the handlers.
Why can't you reference it? Just save it as a module/form-level variable and you're set.
In order to avoid global definitions, the best answer is
Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
'sender is a button in this case.
'get the button
dim b as new button
b = ctype(sender,button)
'now get the button's parent form
dim f as new form
f = ctype(b.parent, form)
'now close the form
f.close()
End Sub