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.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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