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
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 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.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


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

Technology Partners: 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 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