Mdiparent ,child forms - vb.net

I have mdiparent and many child forms
im calling a child form as below
Private Sub tsmQuotation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmQuotation.Click
If Application.OpenForms().OfType(Of Quotation).Any Then
Quotation.WindowState = FormWindowState.Normal
Quotation.Focus()
Else
Quotation.MdiParent = Me
Quotation.Show()
End If
End Sub
my first doubt is: when i declare this Quotation.MdiParent = Me...it takes more time to open the form than without this line. How can i reduce the time to open form..or am I doing anything wrong ?
2nd doubt is: I have place a picturebox in mdiparent's center. and i have sent picturebox to back but then too when i open any child form I see the picturebox above the quotaion .I want to show picture box at back not above any child forms.
Thanks in advance!!!

Without Quotation.MdiParent = Me, the form displayed would not be an MdiChild. It would instead be displaying as a normal form by itself. Try dragging it around the screen and you'll see that it is not confined to the MdiParent form.
See if this loads it any faster, though:
Private Sub tsmQuotation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmQuotation.Click
If Application.OpenForms().OfType(Of Quotation).Any Then
Dim Q As Quotation = Application.OpenForms().OfType(Of Quotation).First
Q.WindowState = FormWindowState.Normal
Q.Activate()
Else
Dim Q As New Quotation
Q.MdiParent = Me
Q.Show()
End If
End Sub
For the second issue, select the MdiParent form and set the BackgroundImage() and BackgroundImageLayout() properties. The image will NOT display on the form at design-time, but it will be there when you run the application.

Related

How can I center form when opening it in MDI Parent form?

I've been trying to solve a problem that occured on my program. All the forms I open from the menu refuse to center based in MDIParent form. Since I'm using 2 panels to design a custom toolbar (containing the Close and Minimize buttons) and another panel regarding the menu.
Here is an image to explain my struggle on fixing my problem.
Image regarding my problem
Things I've tried:
Private Sub RegistarDevoluçãoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RegistarProjetoToolStripMenuItem.Click
Dim janela As New frmRegProjeto
janela.MdiParent = Me
janela.StartPosition = FormStartPosition.CenterParent
janela.Show()
End Sub
Answer by Hans Passant:
Since it would not work on the specific case of an MDI child window CenterScreen object should get the job done:
Private Sub RegistarDevoluçãoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RegistarProjetoToolStripMenuItem.Click
Dim janela As New frmRegProjeto
janela.MdiParent = Me
janela.StartPosition = FormStartPosition.CenterScreen
janela.Show()
End Sub

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

combobox not being populated

I have a windows form project with a main form. There is a textbox leave event that opens a new form. In that new forms load event i have a combobox item loop that populates the combobox items. It works perfectly fine if run on the main form but doesnt work on the second form. Why doesn't the comboboxes on the secondary form populate when that form is opened via a textbox_leave event from the main form?
this is the leave event
Private Sub tbChartTitle_Leave(sender As Object, e As System.EventArgs) Handles tbChartTitle.Leave
If Not tbChartTitle.Text = Nothing Then
frmTitleAttributes.Show()
End If
End Sub
This is the code that populates one of the comboboxes on the second form (it works if run on a combobox on the main form)
Private Sub frmTitleAttributes_Load(sender As Object, e As System.EventArgs) Handles Me.Load
InitializeComponent()
AddFonts()
End Sub
Private Sub AddFonts()
' Get the installed fonts collection.
Dim allFonts As New Drawing.Text.InstalledFontCollection
' Get an array of the system's font familiies.
Dim fontFamilies() As FontFamily = allFonts.Families
' Display the font families.
For i As Integer = 0 To fontFamilies.Length - 1
cbxTitleFonts.Items.Add(fontFamilies(i).Name)
Next
End Sub
make sure that the Load handler is hit after you show your form (use break point)
also you can try to call it in the Shown event
Private Sub frmTitleAttributes_Shown(sender as Object, e as EventArgs) _
Handles frmTitleAttributes.Shown
AddFonts()
End Sub

Can't set focus on a Windows Forms textbox

I can't seem to get input focus on a textbox when a tab page first comes up (I'm using Windows Forms, VB.NET 3.5).
I have a textbox on a panel on a tab page, and I want the focus to be on the textbox when the tab page comes up. I want the user to be able to start typing immediately in the focused textbox without having to click on the textbox. I have tab stops set in the order I want and the textbox is the first tab stop. The tab stops work except that when the tab page comes up the focus is not on the textbox, i.e. the one that's first in the tab order.
In the Enter event handler of the tab page I call the Focus method of the text box, but it returns False and does nothing, no error messages. I know I can access the text box because
at the same point in the code I can set the text of the text box.
If it matters, the layout of the tab page is a little complicated:
frmFoo/TabControl1/TabPageX/Panel1/Panel2/TextBox1
I want to set the focus on TextBox1.
What's the best way to get the focus on the desired textbox?
If setting focus is the best way, why is the textbox.Focus() method failing?
I would assume you are attempting to set focus in the form load event handler? If so, you need to do a Me.Show() to actually create the onscreen controls before focus can be set. Something along the lines of:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Show()
Application.DoEvents()
TextBox1.Focus()
End Sub
If you don't do the Me.Show(), the form is NOT displayed until the load event is complete.
For the tab control, handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
You will still want to set the initial focus in the load event as shown above if the first field selected is to be the textbox on the tab control.
Try either:
Me.ActiveControl = TextBox1
or
TextBox1.Select()
Do the control.Focus() in the OnShown event. You don't need any of the DoEvents logic which didn't work for me anyway.
You Should Use Selected Event of TabControl
Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
If e.TabPage.Name = "TabPage1" Then
TextBox1.Select()
End If
End Sub
As I have Checked in Both TabControl.Selected and TabPage.Enter Event can set Select TextBox. I think there is some other elements stealing focus. please varify
Any of the solutions I found online don't solve the problem when the control is on a tab page.
However, this works:
(1) set the TabIndex of the control to 0.
(2) In your code that handles the tabpage event, do the following:
SendKeys.Send("{TAB}")
If SendKeys doesn't seem to be a valid statment, make sure you have the following import at the top of your code file:
Imports System.Windows.Forms
I found that the TabControl gets the focus when the Selected event completes. To make this work I used the Paint event of the TabPage to set the focus of the desired object.
Private Sub TabChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tab1.Paint, Tab2.Paint, Tab3.Paint
Select Case sender.Name
Case "Tab1"
Textbox1.Focus()
Case "Tab2"
T3extbox2.Focus()
Case "Tab3"
Textbox3.Focus()
End Select
End Sub
Try the Activated event of the form like this:
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'SendKeys.Send("{TAB}") this line works too
TextBox1.Focus()
End Sub
That is guaranteed to work.
I once had the same problem but i solved it using the Me.activate() function.