Dirty Form with TabControl - vb.net

Hi I have a form with a TabControl, 3 tabpages and controls(textboxes and comboboxes) in each tabPage.
I have one event for all the changings of the controls in tabPages (controlValueChanged) and one event for the tabControl.SelectedIndexChanged.
So I want to my button (btnOK) enabled if the form is Dirty.
Private Sub controlValueChanged(sender As System.Object, e As System.EventArgs)
If bLoading=False 'bLoading is a boolean that is true after I create and populate the controls
Dirty = True 'Dirty is a boolean property
End If
End Sub
So when I change something in one of my controls the event is fired and btnOK is Enabled.
The problem is that the first time that I change (visiting) to a tabPage the controls is that TabPage fire the controlValueChanged event causing my button to Enabled even if I haven't changed anything in my controls.
If I am visiting a TabPage for second time the event is not fired.
I know that this is normal but how I can overcome this problem and have my Dirty =true only if the control is fired because of something is changing and not when the tabPage is changing?

What about creating a new boolean variable - DoNotMakeDirty, say, then you could, in the TabControl1_SelectedIndexChanged sub, set DoNotMakeDirty = True and then put an if statement in your controlValueChanged sub.

Related

Change tab order inside ToolStripMenu

When the ToolStrip control is focused the first ToolStripItem is focused too.
I need to set the focus to the second item, but there is no TabIndex property in ToolStripItems.
I've tried to select the item manually when the ToolStrip gets the focus, but there is no Focus method either.
Actually, we use a inherited control, so I can create custom properties/methods, if it's needed.
Any idea on how to achieve this?
The ToolStrip is doing something when it fires the Enter event, so just setting the focus on a ToolStripItem won't work unless you focus after the enter event code has completed. The BeginInvoke method is a way to run code after the event has finished:
Private Sub ToolStrip1_Enter(sender As Object, e As EventArgs) Handles ToolStrip1.Enter
Me.BeginInvoke(New Action(Sub()
ToolStrip1.Items(1).Select()
End Sub))
End Sub

Need To Close A Panel When Clicked Outside (Lost Focus)

I have an issue that I cannot seem to overcome.
In my application, I have a custom class that loads a form into a panel upon startup. Then when I click a button on my main form I show the panel as visible revealing the form to the user.
My problem is that I want to be able to hide the panel when a user clicks outside of it (back onto the main form).
so far I have tried Form_Deactivate, Form_Leave, Form_LostFocus, Panel_Leave and Panel_LostFocus events but nothing will seem to trigger an event consistently to hide the panel. The only thing that works is if the user clicks inside the form (on a listview control) once the form is visible and then clicks outside of the form.
Is there anyway I can ensure this event gets called everytime whether the user clicks the form or not?
So far my code looks something like:
Public Class cls_UserObjects
Private frm As frmUsers
Public pnl As Panel
Public Sub ShowUserPanel()
Try
frm = New frmUsers
frm.TopLevel = False
pnl.Controls.Add(frm)
frm.Show()
frm.Focus()
....
End Class
Then in my main form I call the code below to build the form into the panel:
class_Users.pnl = pnlUsers
class_Users.ShowUserPanel()
And pnlUsers.Visible = True to show it to the user
I just can't seem to close it. I understand that Panels don't support the LostFocus properly, however, I can't find away around this. Maybe it has something to do with how I am opening my form/panel but I was advised to use classes to open forms so I can have more control over the controls within my forms from outside calls.
Any help appreciated. Thanks
MouseLeave event works, the panel hides immediately once it leaves the panel.
Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Panel1.Visible = False
End Sub

All Controls Click event on form

Is there a simple way to Activate the form when any controls in the form is clicked like datagirdview, panel, menustrip, button, textbox, label, etc....
it happens that my project can show many different form and it's hard for me to activate one form when it's on the back of the active form. I need to clicked the border of the form to activate it.
most likely your problem arises because your form is MDI child.. you'll have to click the menu bar to activate the form.. but if you have a borderless form, clicking on controls wont activate the form.. again, this usually happens on MDI but it shouldnt happen on a regular winform.. unless you have other running events in the background that interferes with the process.
You question is not clear at all, and I don't know what you are trying to archieve, but for executing something with a click event you have to add the handler for every control.
If you are declaring it not dinamically just:
Private Sub ControlsClick(sender As Object, e As EventArgs) _
Handles Panel1.Click, Button1.Click, TextBox2.Click ' etc.
Me.Activate 'Or Whatever
End Sub
You have to add the handler for each control. The same if you do it dinamically:
Private Sub InitializeClickHandlers(sender As Control, Optional bChilds As Boolean = True)
For Each elem As Control In sender.Controls
AddHandler elem.Click, AddressOf ControlsClick(elem, New EventArgs)
If bChilds AndAlso elem.Controls.Count > 0 Then
Call InitializeClickHandlers(sender)
End If
Next
End Sub
Then, for every control in the form, you call it like: Call InitializeClickHandlers(Me)
Or, for every control inside a panel: Call InitializeClickHandlers(Panel1)

vb.net How to get working scrollbars when moving nested form controls inside a parent panel

I have a container (a panel) which can contain multiple Form controls.
(Form.TopLevel = False)
When the user moves the Forms around I would like to display scrollbars when a form is out of the panel bounds.
When I register the Form.Move event, I can set the AutoScrollPosition. This works unless the user uses the scrollbars.
The problem is that the form.move event is also fired when the scrollbars are used. The result is that the scrollbars don't work. (And I have currently no idea how to find out whether the form has been moved by the mouse or by the scrollbar)
So the question is: How can I make the scrollbars of the panel appear/work when a form (or multiple) forms of the panel exceed the boundaries? I think there must be a simpler way than to handle the move event..
Note:
The panel is placed inside a Infragistics DockableControlPane. (Managed by an UltraDockManager)
(So there are multiple panels which contain at least one form per panel)
The reason is that the "panels" should appear as tabs, can be moved around using the DockManager and display their "sub" forms (Which also can be moved around on their panel).
Any idea would be great
It looks like the LocationChanged event could be used. Example with only one form:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Dim f As New Form
f.TopLevel = False
AddHandler f.LocationChanged, AddressOf Form_LocationChanged
Panel1.Controls.Add(f)
f.Show()
Call Form_LocationChanged(f, EventArgs.Empty)
End Sub
Private Sub Form_LocationChanged(sender As Object, e As EventArgs)
With DirectCast(sender, Form)
Panel1.AutoScrollMinSize = New Size(.Bounds.Right, .Bounds.Bottom)
End With
End Sub
Using an MDI form seems to be more appropriate though for something like this.

VB.Net Hide Tabpage

Ive seen some discussion on here about how to hide tabs in a tabcontrol but they all seem to be in C or some variant. I havent seen one for vb.net (i cant do C)
What i want to do is hide or disable all some of the tabs till the user has logged in.
Ive sorted out the login and logout. All i need to do is add the code to enable/disable some tabs until the user has logged in.
Anyone know a good way to do this?
WinForms btw
You just add and remove TabPages from the TabControl through the TabPages collection:
TabControl1.TabPages.Add(myTabPage)
and to remove it:
TabControl1.TabPages.Remove(myTabPage)
Note: Removing a TabPage does not dispose it, it just removes it from the TabPage collection.
Currently, the following code block disables all the controls on that TabPage (Sets Control.Enabled = False). The tab itself is still visible and selectable from the TabControl, it is not hidden. The tab is selectable and all the elements appear disabled.
TabMyTab.Enabled = False
If you want to disable the tab similar to i.e. button.Enabled = False which does not allow the control to be used, you will need to do something different as disabling a TabPage as in the code above disables all controls in that tab. If this is what you want, keep reading. A lot of programmers suggest using the TabControl to disallow the tab from being selected by selecting a different or the previously selected tab. This is the most effective way I know. I would implement this as follows:
Private PreviousTab As New TabPage
Private CurrentTab As New TabPage
Private Sub TabControlName_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Deselected
PreviousTab = e.TabPage
End Sub
Private Sub TabControlName_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Selected
CurrentTab = e.TabPage
If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = UnselectableTab.Name) Then
MessageBox.Show("Tab disabled.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TabControlName.SelectedTab = PreviousTab
End If
End Sub
Substitute your own values for "UnselectableTab" and "TabControlName" for your project.
Just hide the whole TabControl by setting its Visible property