Go Premium for a chance to win a PS4. Enter to Win

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

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

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
        '*******************************************
        tb.Add(rptNum)
        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(btn)
        '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
0
OutOnALimbAlways
Asked:
OutOnALimbAlways
  • 4
3 Solutions
 
mlmccCommented:
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?

mlmcc
0
 
OutOnALimbAlwaysAuthor Commented:
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
0
 
ktaczalaCommented:
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.
0
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!

 
OutOnALimbAlwaysAuthor Commented:
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....
0
 
OutOnALimbAlwaysAuthor Commented:
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
        e.TabPage.Show()
        e.TabPage.Refresh()
    End Sub
0
 
OutOnALimbAlwaysAuthor Commented:
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'.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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