Question : Rubberbanding with 2D rectangle

Greetings and salutations:

I'm testing a rubberbanding method using 2D rectangles instead of drawreversiblerectangle, since there are a lot more options with a rectangle. However, I don't know enough about .NET graphics to solve this little problem:

After I've drawn a rubberband line/rectangle, I leave it onscreen on purpose. When I click to start another line or rectangle, if I select inside the rectangle (either the invisible rectangle holding the line or the rubberband rectangle, nothing happens. The mouse click is ignored and I don't know why.

Apparently, the rubberband object has captured the mouse. I tried adding code to the click method for the rubberband to hide itself, but the extra clicking became annoying.

Is there anything else I could do to eliminate this behaviour?

I'm using VS2005 w/VB.NET. BTW, if anyone can use any part of my code (such as it is), you're welcome to it.

Thanks.

P.S. I tried to send the entire project via ZIP, but the File link doesn't work. If you would like this file, reply here or send me an email at [email protected] (or someone could tell me how to upload the file here.)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
'written by schale(experts-exchange or [email protected])

Imports System.Drawing.Drawing2D

Public Class frmRubberband

    Const RubberLine = 0
    Const RubberRect = 1
    Const Solid = 0
    Const Dashes = 1

    Dim StartPt As Point
    Dim DrawToPt As Point
    Dim LastPt As Point
    Dim dwg As Boolean = False
    Dim Rubberband As Boolean = RubberLine
    Dim Linestyle As Integer = Solid
    Dim MyPen As New Pen(Color.FromArgb(128, Color.Black), 1)

    Private Sub frmRubberband_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim MyBrush As New SolidBrush(Color.FromArgb(128, 0, 0, 0))

        With g
            'MyBrush.Color = Color.FromArgb(128, Color.Red)
            '.FillEllipse(MyBrush, 50, 50, 100, 50)
            'MyBrush.Color = Color.FromArgb(128, Color.Green)
            '.FillEllipse(MyBrush, 100, 75, 100, 50)
            'MyBrush.Color = Color.FromArgb(128, Color.Blue)
            '.FillEllipse(MyBrush, 150, 100, 100, 50)
            'MyBrush.Color = Color.FromArgb(128, Color.White)
            '.FillEllipse(MyBrush, 200, 120, 100, 50)
            .FillEllipse(Brushes.Red, 10, 10, 100, 50)
            .FillEllipse(Brushes.Green, 60, 60, 100, 50)
            .SmoothingMode = SmoothingMode.AntiAlias
            .FillEllipse(Brushes.Blue, 110, 110, 100, 50)
            .FillEllipse(Brushes.White, 160, 160, 100, 50)
        End With

    End Sub

    Private Sub frmRubberband_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

        StartPt.X = e.X
        StartPt.Y = e.Y
        DrawToPt = StartPt
        LastPt = StartPt
        pbxRubberband.Show()
        dwg = True

    End Sub

    Private Sub frmRubberband_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

        If dwg Then
            DrawToPt.X = e.X
            DrawToPt.Y = e.Y
            pbxRubberband.Show()
            RedrawRubberBand()
        End If

    End Sub

    '=======================================================================
    '= If the rubberband paints over any controls on the form, go back to  =
    '= design mode, right click on the rubberband and select SEND_TO_BACK. =
    '=======================================================================
    Private Sub RedrawRubberBand()
        Dim xStart, yStart, xEnd, yEnd As Integer

        With pbxRubberband

            If DrawToPt.X < StartPt.X Then
                .Left = DrawToPt.X
                .Width = StartPt.X - DrawToPt.X
                xStart = 0
                xEnd = .Width - 1
            Else
                .Left = StartPt.X
                .Width = DrawToPt.X - StartPt.X
                xStart = .Width - 1
                xEnd = 0
            End If

            If DrawToPt.Y < StartPt.Y Then
                .Top = DrawToPt.Y
                .Height = StartPt.Y - DrawToPt.Y
                yStart = 0
                yEnd = .Height - 1
            Else
                .Top = StartPt.Y
                .Height = DrawToPt.Y - StartPt.Y
                yStart = .Height - 1
                yEnd = 0
            End If

            .Refresh()
            If Rubberband = RubberLine Then
                .CreateGraphics.DrawLine(MyPen, xStart, yStart, xEnd, yEnd)
            Else                'Draw a RubberRectangle
                If chkbxShadedRectangle.Checked Then

                    .CreateGraphics.FillRectangle(MyPen.Brush, 0, 0, .Width - 1, .Height - 1)
                Else
                    .CreateGraphics.DrawRectangle(MyPen, 0, 0, .Width - 1, .Height - 1)
                End If
            End If
        End With

        LastPt = DrawToPt

    End Sub

    Private Sub frmRubberband_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

        DrawToPt.X = e.X
        DrawToPt.Y = e.Y
        dwg = False

    End Sub

    Private Sub btnLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine.Click

        Rubberband = RubberLine
        RedrawRubberBand()

    End Sub

    Private Sub btnRectangle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRectangle.Click

        Rubberband = RubberRect
        RedrawRubberBand()

    End Sub

    Private Sub btnSolidLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSolidLine.Click

        MyPen.DashStyle = DashStyle.Solid
        RedrawRubberBand()

    End Sub

    Private Sub btnDashedLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDashedLine.Click

        MyPen.DashStyle = DashStyle.Dash
        RedrawRubberBand()

    End Sub

    Private Sub updnLineWidthSelect_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updnLineWidthSelect.ValueChanged

        MyPen.Width = updnLineWidthSelect.Value
        RedrawRubberBand()

    End Sub

    Private Sub chkbxShadedRectangle_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbxShadedRectangle.CheckedChanged

        RedrawRubberBand()

    End Sub

    'Private Sub pbxRubberband_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbxRubberband.Click
    '    pbxRubberband.Hide()
    'End Sub
End Class

Answer : Rubberbanding with 2D rectangle

1) They're all different
2) 0
3) No
4) You need to maintain the table by going to every state's website (and many counties) regularly, and keeping the rates up to date.

That's why this is a specialty business.  I've been doing accounting integration for over 20 years, and I wouldn't think of doing this myself, there are too many liabilities.  Many states have special offices and workers whose full time job is to make sure taxes are correctly calculated, and you'll get fined if you don't do it right.
Random Solutions  
 
programming4us programming4us