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

Posted on 2013-12-30
Medium Priority
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 vb.net. 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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 101

Assisted Solution

mlmcc earned 600 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 13

Assisted Solution

ktaczala earned 900 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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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 tab.show() 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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