how can i unload a form inside a panel
my code is :
frmInvoiceOverview.Close()
frmInvoiceOverview.Dispose()
With frmInvoiceOverview
.TopLevel = False
MainPanel.Controls.Clear()
MainPanel.Controls.Add(frmInvoiceOverview)
.BringToFront()
.reloadTable()
.Show()
End With
If i "load" it again the Form Load event will not fired.
what do i wrong ?
You are invoking a class like Form doing that which a simply Control (eg. Panel) can do.
That means you have more heavy runtime objects (even not so significant).
However after calling .Dispose in a Form (or whatever else Object) needs to be initialized like
frmInvoiceOverview = New InvoiceOverview With {
.TopLevel = False,
.FormBorderStyle = FormBorderStyle.None}
before calling methods or setting its properties then lifetime cycle restart again
Related
How can I change the BACKGROUND color of the MDI FORM in C#?
I changed it using the background color property but the color is not changed.
What should I do to perform this task?
The actual BackGround colour of the MDI control is based on the colour in the Windows current Theme. You have to physically set the MdiClient control's background inside the WinForm.
// #1
foreach (Control control in this.Controls)
{
// #2
MdiClient client = control as MdiClient;
if (!(client == null))
{
// #3
client.BackColor = GetYourColour();
// 4#
break;
}
}
Edit - Added comments:
We need to loop through the controls in the MdiParent form to find the MdiClient control that gets added when you set the Form to be an MdiParent. Foreach is just a simple iteration of a type through a collection.
We need to find the MdiClient control within the form, so to do this we cast the current control within the loop using the 'as' keyword. Using the 'as' keyword means that if the cast is invalid then the variable being set will be null. Therefore we check to see if 'client' is null. If it is, the current control in the loop is not the MdiClient control. As soon as the variable 'client' is not null, then the control we've got hold of is the MdiClient and we can set its background colour.
Set the backcolour to anything you want. Just replace "GetYourColour()" with whatever colour you want, i.e. Color.White, Color.Blue, Colour.FromArgb(etc)...
As there is only ever 1 MdiClient, there's no point continuing the loop as it's just a waste of processing time. Therefore we call 'break' to exit the loop.
Let me know if you want anything else explaining.
Write this in your load method of your MDI form.
Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.Purple;
I have the following code to select a custom control. But it does not want to set the active control to 'uPnlEntryOptions'. I have tried using control.select as well, same result. I have also tried
ActiveControl = uPnlEntryOptions
Code
uPnlEntryOptions.Visible = True
uPnlEntryOptions.Refresh()
uPnlEntryOptions.Select()
uPnlEntryOptions.Focus()
Debug.Print(CStr(uPnlEntryOptions.Focused))
Debug.Print(CStr(uPnlEntryOptions.CanFocus))
Output
False
True
The Focused property is true only if the control has the focus, not a sub-control.
Calling Select (or Focus?) on a UserControl will select the first child control. Afterwards the UserControl.ContainsFocus = True since the focus is WITHIN the UserControl, but the UserControl.Focused = False, since the UserControl itself isn't the focused control.
I've created a form that has a tabbed control that gets users controls added to each tab dynamically and a StatusStrip at the bottom of the form. When the app starts, the user controls are loaded in the tabs based on security with at least 1 tab being loaded. On the StatusStrip, two ToolStripComboBoxes, 2 ToolStripButtons, 1 ToolStripLabel, and 1 ToolStripStatusLabel. Everything loads fine and works.
I've been taksed to have a MonthCalendar popup when the user presses one of the two buttons. Here's the code I use to do this:
If IsNothing(theCal) Then
theCal = New MonthCalendar
AddHandler theCal.DateSelected, AddressOf theCalDateSelected
AddHandler theCal.LostFocus, AddressOf theCalLostFocus
AddHandler theCal.GotFocus, AddressOf theCalLostFocus
theCal.Parent = Me
theCal.Top = StatusStripMain.Top - theCal.Height
theCal.Left = ComboBoxAvailableLegDay.Bounds.X
theCal.Anchor = AnchorStyles.Bottom + AnchorStyles.Left
theCal.Show()
theCal.BringToFront()
theCal.Focus()
Else
Me.Controls.Remove(theCal)
theCal = Nothing
End If
theCal is defined as Protected at the top of the form's class. So, pressing the button will create the MonthCalendar and position it correctly if it doesn't exists and if it does exists, then it is removed. This works with no problems.
My problem is that theCal never fires GotFocus or LostFocus. I've got the procedure theCalLostFocus defined as follows and it never thows the exception. I can put a breakpoint at the throw and the code never makes it to that point.
Private Sub theCalLostFocus(ByVal sender As Object, ByVal e As EventArgs)
Throw New NotImplementedException
End Sub
Clicking a date on theCal will call theCalDateSelected procedure, but clicking any other area of the form does not fire theCalLostFocus. Since the user may want to not select a date and I don't want to force them to have to press the button to remove theCal, I'd like to be able to remove theCal when it loses focus. Anyone have any idea why this is happening and anyone got a solution?
Thanks.
-NCGrimbo
i'm not that surprised that the focus event won't fire, because you add the handler before inserting it in the visual tree. try adding the handler after the call to show(). or maybe in the loaded event handler. Note that since you request the focus, your focus event handler will be called every time.
Rq : as it is written, your code has memory leak since you do not remove the event handler when you clear theCal, so since a reference is kept to theCal, neither theCal nor the event handler get cleared and this lead to memory leak (cyclical reference).
I am trying to bind a checkbox to a custom object boolean property as follows:
chkTableIsReadonly.DataBindings.Add(New Binding("Checked", objectBindingSource, "ApplyforVisa", True, DataSourceUpdateMode.OnPropertyChanged, False))
The custom class supports the INotifyPropertyChanged interface.
Everything works find when I initially bind the checkbox to a new object:
objectBindingSource.Datasource = new objectToBindTo
Here is the odd part:
If I check the box, the property Set gets called and the INotifyPropertyChanged event gets called and everyone is happy.
If I uncheck the same box, the property Set doesn't get called, the INotifyPropertyChanged event never gets called and (the worse part), I cannot navigate to another record.
I have tried capturing the CheckedChanged event to set the object.ApplyForVisa property manually, but no success. The property Set gets called and the INotifyPropertyChanged event gets called, but I am still locked on control and can't navigate.
I have tried calling bindingsource.endedit in the CheckedChanged event, no success.
It only matters if I uncheck the box. The checkbox is two-state - true or false.
All of my other bindings work just fine - text boxes, combo boxes, datagrid. Just not checkbox.
My only thought is that is seems to act like a binding source data error, but no error is thrown. If I add the data error event handler for the binding source, it never gets called.
Assuming the ApplyForVisa property is a Boolean, you can just fix this by setting formattingEnabled parameter for the Binding to False.
chkTableIsReadonly.DataBindings.Add( _
New Binding("Checked", objectBindingSource, "ApplyforVisa", _
False, DataSourceUpdateMode.OnPropertyChanged, False))
I would just like to know how to run code in vb.net when the program is closed with the red cross in the top right of the screen.
Use Form.FormClosing Event - Read the Remarks on this event documentation for better implementation of your functionality.
It occurs before the form is closed.
Check FormClosingEventArgs properties for further manipulations:
e.CloseReason
e.Close
However, canceling the event will set to true the Cancel property of
the FormClosingEventArgs class that is passed as a parameter to the
parent form. To force all MDI parent and child forms to close, set the
Cancel property to false in the MDI parent form.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
sr.WriteLine("8 - FormClosing");
}
Windows Forms Events Lifecycle
You need to handle the FormClosing event: use the CloseReason property of the FormClosingEventArgs to determine why the form is closing - you want to look for CloseReason.UserClosing - and set the Cancel property to true if you want to cancel the form close.