Child Forms and Parent forms mismatch in Vb.net - vb.net

I have a parent form called MdiForm1 within it I open frm1 which is a child form. SO everything is great at this point - now I need to open another form within frm1 - lets call that frmX and here's where my issue arises - I previously had declared it as mdichild form, and did mdichild.show - however the issue comes up because when this form opens (it covers about 1/3 of frm1 - which is already open) and user clicks outside of the frmX - it simply disapears. So I tried to .showDialog() however am unable to do that because it's not Top level and is a mdiChild therefore won't let me .showdialog(). here's my code...
Private Sub cmd1_Click(sender As Object, e As EventArgs) Handles cmd1.Click
Dim NewMDIChild As New Frmx()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = MDI1
'Display the new form
NewMDIChild.ShowDialog()
NewMDIChild.Top = 310
NewMDIChild.Left = 36
NewMDIChild.Width = 897
End Sub
I get this error on .showDialog() and here's what it says....
Form that is not a top-level form cannot be displayed as a modal
dialog box. Remove the form from any parent form before calling
showDialog.
I tried to declare the frmX like this....
dim frmX as New Form
frmX.showDialog
'specifying Top/Left/Width but that doesn't do anything, basically opens an empty form elsewhere on the screen.
EDIT:It's a little confusing :/
This is what I Did - getting the same error. This is in frm1 on button click which is suppose to OPEN frmX in modal so that users clicking on frm1 will not make frmX disappear. It opens in the right location however when I click elsewher on frm1 --- frmX disappears
Dim frmxName As New FrmX()
frmxName.MdiParent = Me.MdiParent
frmxName.ShowDialog()
frmxName.Top = 310
frmxName.Left = 36
frmxName.Width = 897
My goal is to have frmX open until they click close on it!

Set the new form's MDI parent to the controlling form's MDI parent
In the MDI parent I called Form1. This form has the property IsMdiContainer = True
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myFrmX As New frmX
myFrmX.MdiParent = Me
myFrmX.Show()
End Sub
End Class
And in frmX with a button on it
Public Class frmX
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form
f.MdiParent = Me.MdiParent
f.Text = "frmY"
f.Show()
End Sub
End Class
Clicking the button creates new Forms, which are shown to be MDI children of the main form below
Or if you just want a dialog window, forgo the MDI business, and just show dialog
Public Class frmX
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form
f.Text = "frmY"
f.ShowDialog()
End Sub
End Class
Now frmY has focus until it's closed.

Related

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

How Show a Form on top of other?

I have a first Form that is always displayed of way:
Maximized and as content a printscreen of atual screen
Always on top
without borders
and now I want show a second Form on top this first Form, but I haven't success until this moment, in another words, this second Form don't is displayed on top. So how I can do it? All suggestions here are welcome.
Here is how I'm making for show the first Form:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each s As Screen In Screen.AllScreens
Dim Locker As New Form2(s, 0.3)
Locker.Show()
Next
End Sub
End Class
=========================================================================
Public Class Form2
Public Sub New(ByVal scrn As Screen, ByVal FrmOpacity As Double)
InitializeComponent()
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.StartPosition = FormStartPosition.Manual
Me.Bounds = scrn.Bounds
Me.TopMost = True
Me.Opacity = FrmOpacity
Me.ShowInTaskbar = False
Me.BackgroundImageLayout = ImageLayout.None
CaptureScreen(scrn)
End Sub
Private Sub CaptureScreen(ByVal s As Screen)
Using ScreenImg As New Bitmap(s.Bounds.Width, s.Bounds.Height)
Using g As Graphics = Graphics.FromImage(ScreenImg)
g.CopyFromScreen(s.Bounds.Location, Point.Empty, ScreenImg.Size, CopyPixelOperation.SourceCopy)
End Using
Me.BackgroundImage = New Bitmap(ScreenImg)
End Using
End Sub
End Class
You could simply set the Owner of the form2 to be form1
Public Class Form1
.....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each s As Screen In Screen.AllScreens
Dim Locker As New Form2(s, 0.3)
Locker.Show(Me)
Next
End Sub
.....
End Class
Passing the instance of Form1 to the Show method of Form2 will set the passed instance of the Form1 as the Owner of all the Locker forms that your create in your loop. In this way the Form2 instance will be always above the Form1 instance. Of course, you could remove the setting of the TopMost=True property that could be assigned only to one form at a time (Only one form could be the TopMost)
From MSDN on Owner form property
When a form is owned by another form, it is closed or hidden with the
owner form. For example, consider a form named Form2 that is owned by
a form named Form1. If Form1 is closed or minimized, Form2 is also
closed or hidden. Owned forms are also never displayed behind their
owner form. You can use owned forms for windows such as find and
replace windows, which should not disappear when the owner form is
selected. To determine the forms that are owned by a parent form, use
the OwnedForms property.
What you want is a Modal Dialog. I don't know vb.Net, but in C# .Net if you say
NameOfFormThatShouldBeOnTop.Show();
The form will just show but not necessarily be on top. However, if you go
NameOfFormThatShouldBeOnTop.ShowDialog();
It will be forced on top. From the documentation, it looks like doing this in vb is pretty similar. I would guess it would be something like
NameOfFormThatShouldBeOnTop.ShowDialog()
If I got that syntax wrong, feel free to edit :)
You may try to load a Sub after the LOAD or SHOWN events, that will contain the below code:
private sub LeaveMeAtTop()
Me.Topmost = True
Me.TopLevel = true
Me.Activate()
Me.ResizeRedraw() = true
Me.ResumeLayout()
Me.Focus()
end sub
It will force the form to be displayed at top of all

Load Event of MDi child form not firing?

I have a simple form which calls an external class containing another form in a vb.NET application.
The 2 forms are set up as an MDi parent and child.
Does anyone know why when I call MDIChild.show() in the code of the parent, the load event does not fire in the child form??
Parent Code:
Dim ce As New Policies.Main
ce.MdiParent = Me
ce.Show()
Child Code
Public Sub Main_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
'Do some stuff in load event
End Sub
Right
Following on from the comments above. Open up visual studio and create a simple Winforms project. It will be created with a default instance of Form1.
In the solution explorer right click on the solution and select add and from the menu that appear select Windows form. A new windows form will be created with a default name of Form2.
We are going to treat form 1 as our parent class and form 2 as our child.
Go back to form one and drag a button onto it from the toolbox. Double click on the button once its on the form to open up its default button click handler.
Add the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.IsMdiContainer = True 'we need this so that Form1 can act as a container for other forms
Dim frm As New Form2
With frm
.MdiParent = Me
.Show()
End With
End Sub
Now return to form2. Doubleclick on it to bring up its default load event in the code editor. Add the following code.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Hi, I'm being shown from the load event of form2")
End Sub
With that done press f5 to run this very simple ( and crude) example. Form1 will load. when you click the button a new instance of Form2 is created. Prior to the form being shown its load event is fired and that triggers te message box to display it's message. You do not need to call the load method directly.

Label won't update from separate form

I have 2 forms in my program. Form1 has a tab control on it, and on one of these tabs there are a load of labels. Form2 has a few textboxes and dropdown lists on it. Form1 has a button on it that opens Form2 on top of it as a normal form, not as a dialog.
There is code in Form1 that on loading populates the labels on it from a MySQL database. This code is in a separate public sub in the form that is called when the form loads.
What I am trying to do is fill in some of the boxes on Form2, when this Form closes it updates the database with these values (Works fine) and then those values are displayed on Form1. I can get Form2 to run the code in Form1 that populates the labels, but the problem is that the labels on Form1 never actually change. I have tried multiple things, including calling refresh on the labels, trying to change the labels from within Form2 instead of Form1, but they just never update. The .text value of the labels is being updated to the new value, I have checked this via debugging and stopping at the correct points to see what the value is.
Any ideas anyone?? I think it might be to do with the fact that it's Form2 calling the code and for some reason it doesn't have access to change the labels on Form1.
Code:
Form1 (AECSurveyForm)
Private Sub AECSurvey_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadMeterData()
End Sub
Public Sub LoadMeterData()
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
'utility
Meter1UtilityLabel.Text = AECMeteringData(0)("Utility1")
End Sub
Private Sub MeterButton1_Click(sender As Object, e As EventArgs) Handles Meter1Button.Click
If Not Application.OpenForms().OfType(Of AECMeteringDataForm).Any Then
AECMeteringDataForm.GetData(AECGlobalValues.CurrentSurveyUniqueIdentifier, 1)
AECMeteringDataForm.Show()
End If
End Sub
Form2 (AECMeteringDataForm)
Private Sub AECMeteringDataForm_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Closing
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
AECMeteringData(0)("Utility1") = UtilityComboBox.SelectedItem.ToString
AECMeteringDataAdapter.Update(AECMeteringData)
AECSurveyForm.LoadMeterData()
End Sub

Launching a MDI menu option with Enter key fires KeyUp in MDI Child form

Using Visual Studio 2012
Added an MDI Form to project which creates a bunch of default menu items and then added the following code for the File/Open menu item:
Public Class MDIParent1
Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
Dim frm As New Form1
With frm
.MdiParent = Me
.Show()
End With
End Sub
End Class
In another form, Form1, set KeyPreview = True and put in this code:
Public Class Form1
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Enter Then
MsgBox("enter pressed")
End If
End Sub
End Class
Now, if you launch the MDI form and click the File menu (or ALT+F) then scroll down using arrow keys to Open and hit Enter on the keyboard, it launches Form1 but fires KeyUp(). In my actual application this ends up launching another child form to Form1 and I need to suppress the Enter key from the MDI menu launch but can't figure out how to do that.
Handle KeyPress or KeyDown instead of KeyUp.