-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCalculator.vb
202 lines (180 loc) · 6.86 KB
/
Calculator.vb
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Threading
Imports System.Windows.Forms
Imports Microsoft.Win32
Public Class Calculator
Dim ErrWarning As Thread,
ErrMutex As Boolean
Dim HexVar = "{*}"
Private Sub Src_TextChanged(sender As Object, e As EventArgs) Handles Src.TextChanged
For Each Rpl In "!@#$%^&*()_~QWERTYUIOPASDFGHJKLZXCVBNM "
Src.Text = Replace(Src.Text.ToUpper, Rpl, String.Empty)
Next
If Src.Focused Then
If Not Mask.Text.Contains(HexVar) Then
Mask.Text &= HexVar
End If
DoCalc_ToHex()
End If
End Sub
Private Sub DoCalc_ToHex()
Try
If Src.Text = String.Empty OrElse Src.Text = "-" Then
Out.Text = String.Empty
Exit Sub
End If
Out.Text = Mask.Text.Replace(HexVar,
IIf(UpperMode.Checked,
ToX16(Src.Text).ToUpper,
ToX16(Src.Text)))
Catch ex As Exception
VisualError()
Out.Text = String.Empty
End Try
End Sub
Function ToX16(Num As Integer)
Dim Result As String = Num.ToString("x16")
If ShortFormat.Checked Then
For Each MIN In Result '0x001 => 0x1 [&H..]
If MIN = "0" AndAlso Result.Length > 1 Then
Result = Result.Substring(1) ' 0x..1 => 0x1
Else
Exit For
End If
Next
End If
Return Result
End Function
Private Sub Out_TextChanged(sender As Object, e As EventArgs) Handles Out.TextChanged
If Out.Focused Then
Try
If Out.Text = String.Empty Then
Src.Text = String.Empty
Exit Sub
End If
Dim Calc = Out.Text
For Each Rpl In "!@#$%^&*()QWRTYUIOPSGHJKLZXVBNM\/?|[]{}~"
Calc = Replace(Calc.ToUpper, Rpl, "")
Next
Src.Text = Convert.ToInt32(Calc, 16)
Catch ex As Exception
VisualError()
Src.Text = String.Empty
End Try
End If
End Sub
Private Sub Mask_TextChanged(sender As Object, e As EventArgs) Handles Mask.TextChanged
DoCalc_ToHex()
_0x.Enabled = True
_AMPH.Enabled = True
_H.Enabled = True
Cls.Enabled = True
If Mask.Text = $"0x{HexVar}" Then
_0x.Enabled = False
ElseIf Mask.Text = $"&H{HexVar}" Then
_AMPH.Enabled = False
ElseIf Mask.Text = $"{HexVar}H" Then
_H.Enabled = False
ElseIf Mask.Text = HexVar Then
Cls.Enabled = False
End If
If Not Mask.Text.Contains(HexVar) Then
Mask.BackColor = Color.Red
Mask.ForeColor = Color.White
Else
Mask.BackColor = Color.White
Mask.ForeColor = Color.Black
End If
End Sub
Private Sub _0x_Click(sender As Object, e As EventArgs) Handles _0x.Click
Mask.Text = $"0x{HexVar}"
UpperMode.Checked = False
ShortFormat.Checked = True
Src.Focus()
End Sub
Private Sub _AMPH_Click(sender As Object, e As EventArgs) Handles _AMPH.Click
Mask.Text = $"&H{HexVar}"
UpperMode.Checked = True
ShortFormat.Checked = True
Src.Focus()
End Sub
Private Sub _H_Click(sender As Object, e As EventArgs) Handles _H.Click
Mask.Text = $"{HexVar}H"
UpperMode.Checked = True
ShortFormat.Checked = True
Src.Focus()
End Sub
Private Sub Cls_Click(sender As Object, e As EventArgs) Handles Cls.Click
Mask.Text = HexVar
ShortFormat.Checked = False
UpperMode.Checked = False
Src.Focus()
End Sub
Private Sub CheckedChanged_1(sender As Object, e As EventArgs) Handles UpperMode.CheckedChanged, ShortFormat.CheckedChanged
DoCalc_ToHex()
End Sub
Private Sub Src_KeyPress(sender As Object, e As Windows.Forms.KeyPressEventArgs) Handles Src.KeyPress
e.Handled = Not e.KeyChar Like $"[-,0-9,{ChrW(8)}]"
End Sub
Private Sub Mask_KeyPress(sender As Object, e As Windows.Forms.KeyPressEventArgs) Handles Mask.KeyPress
If "EADFC".Contains(CStr(e.KeyChar).ToUpper) Then
VisualError()
e.Handled = True
Else
e.Handled = False
End If
End Sub
Private Sub Out_KeyPress(sender As Object, e As Windows.Forms.KeyPressEventArgs) Handles Out.KeyPress
If Convert.ToInt32(e.KeyChar) = 22 Then
Out.Text = String.Empty
Exit Sub ' Ctrl V ?=> replace all text
ElseIf Convert.ToInt32(e.KeyChar) = 3 Then
If Not Out.Text = String.Empty Then Clipboard.SetText(Out.Text) ' Ctrl C ?=> copy all text
Exit Sub
End If
If Not $"!@#$%^&*()/\|~EADFC1234567890-_{ChrW(8)}".Contains(CStr(e.KeyChar).ToUpper) AndAlso
Not Mask.Text.Contains(e.KeyChar) Then ' If KEYCHAR does not contain a character from the mask
VisualError()
e.Handled = True
Else
e.Handled = False
End If
End Sub
Private Sub VisualError()
ErrLbl.Visible = True
If ErrMutex Then
Exit Sub
End If
ErrMutex = True
ErrWarning = New Thread(Sub()
For fErr = 0 To 2
ErrLbl.ForeColor = Color.White
ErrLbl.BackColor = Color.Red
Thread.Sleep(100)
ErrLbl.ForeColor = Color.Red
ErrLbl.BackColor = Color.White
Thread.Sleep(100)
Next
ErrLbl.Visible = False
ErrMutex = False
End Sub)
ErrWarning.Start()
End Sub
Private Sub Calculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Text &= $" (v{Application.ProductVersion})"
End Sub
Private Sub Src_KeyDown(sender As Object, e As KeyEventArgs) Handles Src.KeyDown
If e.Control AndAlso e.KeyCode = Keys.V Then
Src.Text = Clipboard.GetText
ElseIf e.Control AndAlso e.KeyCode = Keys.C Then
If Not Src.Text = String.Empty Then Clipboard.SetText(Src.Text)
End If
End Sub
Private Sub Calculator_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Process.GetCurrentProcess().Kill()
End Sub
End Class