Question Prevent Child Control Redraw when ALT or TAB Key is Pressed

ibby

Member
Joined
Jul 30, 2011
Messages
7
Programming Experience
3-5
Hello,
I am programming an application in VB.NET that makes use of a custom control (inherited from Windows.Forms.UserControl), which uses its Paint event (or rather MyBase.Paint) to graph mathematical equations that the user can enter. My problem is that, when the user presses the Tab or Alt button on the main form hosting the control that graphs the equations, the form redraws itself and fires the Paint event of the custom control, thereby forcing a redraw of the graphs. Strangely, this only happens the first time each of the buttons is pressed - each subsequent press of the Alt or Tab button does not invoke a redraw.
This wouldn't be such a big problem if the drawing event wouldn't take such a long time - sometimes (depending on what equations the user has entered), the paint event can take about 10 seconds to complete. I would like to avoid this unecessary wait.
I would be thankful for any sort of help and can of course provide more information if needed.
Thanks
Ibby
 

hipturtle

Well-known member
Joined
Dec 1, 2011
Messages
59
Programming Experience
10+
The code that catches the Alt or Tab and also the Redraw code would be helpful to determine what might be going wrong.

Regards
 

ibby

Member
Joined
Jul 30, 2011
Messages
7
Programming Experience
3-5
The code that catches the Alt or Tab and also the Redraw code would be helpful to determine what might be going wrong.

Regards
I am not catching Alt or Tab button presses at all - however, the KeyPreview Property of the form is set to true and the following code catches some key combinations and invokes appropriate actions:

    Private Sub MainForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.Modifiers = Keys.Control Then ' All key combinations require the Ctrl key to be pressed
            Select Case e.KeyCode
                Case Keys.Enter
                    ' Adds another equation input field.
                    AddToolStripButton.PerformClick()
                Case Keys.L
                    ' Locks MyGraphPaper (an instance of my GraphPaper class) so that it does not graph equations anymore until it is unlocked.
                    LockToolStripButton.PerformClick()
                Case Keys.P
                    ' Opens a form that can be used to change the window settings of MyGraphPaper.
                    EditWindowToolStripButton.PerformClick()
                Case Keys.Add
                    ' Zoom in.
                    MyGraphPaper.CustomZoom((MyGraphPaper.XMax + MyGraphPaper.XMin) / 2, (MyGraphPaper.YMax + MyGraphPaper.YMin) / 2, Definitions.ZoomFactor)
                Case Keys.Subtract
                    ' Zoom out.
                    MyGraphPaper.CustomZoom((MyGraphPaper.XMax + MyGraphPaper.XMin) / 2, (MyGraphPaper.YMax + MyGraphPaper.YMin) / 2, 1 / Definitions.ZoomFactor)
            End Select
        End If
    End Sub


That is all for key presses. The class GraphPaper contains a custom event called 'Graphing,' which is raised in the routine that handles its paint event (after the co-ordinate axes and the grid have been drawn). The main form listens to that event and uses it to pass all the equations that the user wishes to graph to the graph paper. Here is the GraphPaper Paint routine:

Private Sub Draw(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        If Not Me.DrawingLocked Then ' Only proceed if drawing is not locked.
            Dim PaperGraphics As System.Drawing.Graphics = e.Graphics
            PaperGraphics.Clear(Me.BackColor)
            ' The next three routines draw the border, grid lines and axes respectively.
            Border(PaperGraphics)
            Grid(PaperGraphics)
            Axes(PaperGraphics)
            ' This is the event that the main form listens to in order to graph equations.
            ' To do so, it passes the equations to GraphPaper's 'GraphEquation' Sub one after the other. That sub looks after the rest.
            RaiseEvent Graphing(Me, System.EventArgs.Empty)
            ' Just to repaint the axes in case they have been hidden by a large graph:
            Axes(PaperGraphics)
            ' Finally, label the axes.
            Labels(PaperGraphics)
        End If
    End Sub


I hope this helps to find the problem. I can really not see something that might be wrong. The GraphPaper doesn't have any code to watch out for key presses at all, so there can't be anything wrong with that. The GraphEquation Sub basically just evaluates the equations and connects the dots to produce final graphs (actually, it is quite complex so I didn't include it here).

Thanks for helping me and
Regards
Ibby
 

hipturtle

Well-known member
Joined
Dec 1, 2011
Messages
59
Programming Experience
10+
I am not catching Alt or Tab button presses at all - however, the KeyPreview Property of the form is set to true and the following code catches some key combinations and invokes appropriate actions:

    Private Sub MainForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.Modifiers = Keys.Control Then ' All key combinations require the Ctrl key to be pressed
            Select Case e.KeyCode
                Case Keys.Enter
                    ' Adds another equation input field.
                    AddToolStripButton.PerformClick()
                Case Keys.L
                    ' Locks MyGraphPaper (an instance of my GraphPaper class) so that it does not graph equations anymore until it is unlocked.
                    LockToolStripButton.PerformClick()
                Case Keys.P
                    ' Opens a form that can be used to change the window settings of MyGraphPaper.
                    EditWindowToolStripButton.PerformClick()
                Case Keys.Add
                    ' Zoom in.
                    MyGraphPaper.CustomZoom((MyGraphPaper.XMax + MyGraphPaper.XMin) / 2, (MyGraphPaper.YMax + MyGraphPaper.YMin) / 2, Definitions.ZoomFactor)
                Case Keys.Subtract
                    ' Zoom out.
                    MyGraphPaper.CustomZoom((MyGraphPaper.XMax + MyGraphPaper.XMin) / 2, (MyGraphPaper.YMax + MyGraphPaper.YMin) / 2, 1 / Definitions.ZoomFactor)
            End Select
        End If
    End Sub


That is all for key presses.
If the KeyDown event is not catching the Alt or Tab Press then how is the redraw fired at all. There must be something that is doing this, sorry if I am misunderstanding you in this.

Regards :)
 
Top Bottom