Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1556
  • Last Modified:

Tabcontrol - Cancel Tab Changed event

Hi,

Having real problems with this issue. Thanks for clicking

Have a TabControl. When on the first tab, want to check that a ComboBox has a value in before allowing the user to change the TabPage.

Best way to do this would be to cancel the event if ComboBox="" otherwise prgress as normal.

Problem is I'm using the Private Sub _Tabcontrol_TabPage0_Validating event, so that can e.Cancel =True if the ComboBox =""

Problem is this validating event is not firing when the tab is changed.

Have seen a couple of posts on google search saying the same thing.

Can anyone possibly enlighten me to a fix or "get around"???

Rob
0
RobKearney
Asked:
RobKearney
  • 2
2 Solutions
 
Javert93Commented:
"Validating" events are only good for user input controls, and only fire when then focus is move to a control that has it's CausesValidation property set to True. So in your case, this event won't ever fire, and even if it did, it would only fire when the selection changed from a tab (the control itself) to another control. The event you want is the TabIndexChanged event. Unfortunately, this only fires after the user has selected a tab, not before. However, this code will probably do what you want:

    Private CurrentTabPage As TabPage

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CurrentTabPage = TabControl1.SelectedTab
    End Sub

    Private Sub TabControl1_TabIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.TabIndexChanged
        Dim bIsValid As Boolean

        ' Validate the tab here and set the result to bIsValid

        If Not bIsValid Then
            ' The data is invalid, revert the selection
            TabControl1.SelectedTab = CurrentTabPage
        End If
        CurrentTabPage = TabControl1.SelectedTab
    End Sub
0
 
Javert93Commented:
1) Here is an MSDN magazine article that discusses how to create an MSCOMM control alternative in .NET: http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx

2) According the MSDN documentation, "A process uses the CreateFile function to open a handle to a communications resource. For example, specifying COM1 opens a handle to a serial port, and LPT1 opens a handle to a parallel port. If the specified resource is currently being used by another process, CreateFile fails. Any thread of the process can use the handle returned by CreateFile to identify the resource in any of the functions that access the resource." This seems to clearly indicate that serial ports are not sharable outside of a single process. There is a good chance that greg's example is either replacing the default driver, use WaitComm() to intercept serial events.
0
 
RobKearneyAuthor Commented:
Hi Javert93, gregasm.

Thanks to both of you for your posts

gregasm - thanks for pointing me in the direction of those two PAQ's. made me realise needed to attack from anohter angle

And that solution was to do what Javert93 mention. Basically upon SelectedIndexChanged event, check to see that the comboBox <> "", and if it did revert it back to original tab and give out a message.

You have to be careful though, because the event fires twice due to changing the tab twice. I removed and addedhandler back in to overcome this.

Javert93's answer is basically the solution I came up with as well. Which adds credability to both our answers.

Thank you both for taking the time to reply to the post.

Rob
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now