Click Event not always firing the first time after dynamically adding crystal report viewer on tab pages.

Posted on 2013-12-30
Last Modified: 2014-01-04
I apologize in advance if I'm making this more complicated than it needs to be, or if I am unclear.  I am writing a crystal report viewer program in I show the reports on a crystalreportviewer on tabs, which I add dynamically. I also am adding a button to remove the report when the user is done. The problem is that every so often the user
needs to click twice on the button, or once on any other control on the form, to get the button to work. This seems to apply to ANY control, whether it be on the current tabpage or not. I have played around with this for almost a day and the only thing I can say for sure is that it is the fault of the crystal report viewer control. That is, if I add no crystal viewer,, or just add the button, all is fine. It's when I add the crystalreportviewer control that the button (or other control) seems to be non-existent the first time. A breakpoint is not even executed.  And the really crazy thing is that sometimes the first click works, sometimes it doesn't. The code is below, with comments.

So how can I  consistently get it to work on the first click?

Open in new window

 Sub showReport(ByVal crReportDocument As ReportDocument, ByVal rptId As String, ByVal rptNum As String,  Optional ByVal rptType As String = "")
        'shows the selected reports on their respective tabs
        'for some reason, the dynamic crystal report control sometimes interferes with the other controls.
        'The new button apparently does not cause a problem.  I am showing the code for it anyway.
        'When the new tab page is displayed, it takes two clicks before ANY control works, whether belonging to that
        'particular tabpage or just drawn outside the tab control on the form itself. You can also make the control
        'work by clicking twice on the tab, or clicking on any other control once, or you can click on all the tabpages.
        'versions: 2005 Version 8.0.50727.762  (SP.050727-7600)
      'Microsoft .NET Framework Version 2.0.50727 SP2
      'crystal stuff is all 11.5.3300.0
        Dim cr As New CrystalDecisions.Windows.Forms.CrystalReportViewer
        Dim tabknt As Integer
        Dim tb As TabControl.TabPageCollection = tabReports.TabPages
        Dim tabWidth As Integer = tabReports.Width
        Dim tabHeight As Integer = tabReports.Height
        Dim btn As New Button
        With btn
            .Size = New Size(100, 40)
            .Location = New Point((tabWidth / 2) - 50, 10)
            .BackColor = Color.LightGray
            .ForeColor = Color.SlateGray
            .Name = "cmdRemoveButton" 'works without unique name
            .Text = "Done, Remove Report " & rptNum
            .Tag = rptId 'identify which button to click
        End With

        'add an event handler
        AddHandler btn.Click, AddressOf cmdRemoveButton_Click
        tabknt = tb.Count - 1
                'the name is the 'key' for the tabpage to identify it for later removal. Index does not work well
        'because it changes once the tabpage is removed.
        tb(tabknt).Name = rptId
        With cr
            .Name = "crview" & Trim(rptNum)
            .Size = New Size(tabWidth, tabHeight)
            .ReportSource = crReportDocument
            .Update() 'Report may not be the correct one without this..
            .Refresh() 'sometimes we get an error without this..
        End With
        'add the two new controls. If I comment out adding the crystal viewer,
        'everything apparently works fine all the time; the controls only need one click to work.
        'tb(tabknt).Controls.Add(cr) 'uncomment this to cause the error.
'none of this helps at all. I thought it would because clicking on the tab pages helps.
'For Each page As TabPage In tb
'            page.Update()
'            page.Refresh()
 '           page.Select()
'        Next
        tabReports.Refresh() ' this seems to help somewhat
    End Sub
Question by:OutOnALimbAlways
  • 4
LVL 100

Assisted Solution

mlmcc earned 200 total points
ID: 39747238
The viewer has buttons on it.  I suspect the viewer needs to have the focus before it will respond to clicks so the first click gives the viewer focus.

If the viewer has focus then the form may need to get focus back before its buttons will work.

Not sure if you can do that programmatically.
When they work the buttons do the right thing?

If you click on the viewer then do the buttons works on the first  click?  So long as the click doesn/t change the focus do other buttons on the viewer work on first click?


Author Comment

ID: 39747275
mlmcc, what you say makes a lot of sense. Yes, everything appears to work fine once the click event does fire. Yes, if I click first on ANYTHING--including even a blank area on the viewer-any button, on the tabpage or not, fires immediately.

I just tried to set focus on the viewer in the 'selected' event of the tab control. Amazingly, it did not work.... I'll keep playing around with it, perhaps setting focus on other controls?  maybe if I use the 'Selecting' event instead of the 'selected'?

The code I used to do the focus was this:

Open in new window

Private Sub tabReports_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles tabReports.Selected
        Beep() 'always fires because it always beeps!
        e.TabPage.Controls(1).Focus() 'crystal report viewer
    End Sub
LVL 12

Assisted Solution

ktaczala earned 300 total points
ID: 39747452
You can use the Control.Focus method  to set the focus on a control only after the Visible property of the form is set to True.
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.


Author Comment

ID: 39747516
According to the immediate window both the form and tabpage are visible upon the tabcontrol selected event. However- I have been trying to set the focus on any of the newly created controls, and failed.  

The crazy thing is that I can set the focus on a dummy button OUTSIDE of the tabpage, but that STILL doesn't mean that that dummy button will click the first time!

I'm still certain that the dynamically created crystalreportviewer is what is messing everything up....

Accepted Solution

OutOnALimbAlways earned 0 total points
ID: 39747537
Well, it looks like the problem is finally solved.   That's good because it was driving me crazy! The two tips from you guys set me on the right track, especially the one about visibility (visible = show()). I still can't understand why  a successful focus didn't immediately allow a click on my 'dummy' button...

The solution is below. The also DID allow me to set focus on the new controls. Now why you would need to 'show' the tab when it is already shown is beyond me:

Open in new window

Private Sub tabReports_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles tabReports.Selected
        If e.TabPageIndex = 0 Then Exit Sub
    End Sub

Author Closing Comment

ID: 39755838
As I said, the two tips definitely set me on the right track.  After that it was a matter of trying everything in the book to make sure that the tabpage was actually 'finished'.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now