vb.net计算器

 

转载.

View Code
  1 Here is a simple calculator to do all the arithmetic operation.
  2 
  3 Public Class Form1
  4 
  5     Inherits System.Windows.Forms.Form
  6 
  7     Dim clearDisplay As Boolean
  8 
  9     Dim Operand1 As Double
 10 
 11     Dim Operand2 As Double
 12 
 13     Dim [Operator] As String
 14 
 15 #Region " Windows Form Designer generated code "
 16 
 17  
 18 
 19     Public Sub New()
 20 
 21         MyBase.New()
 22 
 23  
 24 
 25         'This call is required by the Windows Form Designer.
 26 
 27         InitializeComponent()
 28 
 29  
 30 
 31         'Add any initialization after the InitializeComponent() call
 32 
 33  
 34 
 35     End Sub
 36 
 37  
 38 
 39     'Form overrides dispose to clean up the component list.
 40 
 41     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 42 
 43         If disposing Then
 44 
 45             If Not (components Is Nothing) Then
 46 
 47                 components.Dispose()
 48 
 49             End If
 50 
 51         End If
 52 
 53         MyBase.Dispose(disposing)
 54 
 55     End Sub
 56 
 57  
 58 
 59     'Required by the Windows Form Designer
 60 
 61     Private components As System.ComponentModel.IContainer
 62 
 63  
 64 
 65     'NOTE: The following procedure is required by the Windows Form Designer
 66 
 67     'It can be modified using the Windows Form Designer. 
 68 
 69     'Do not modify it using the code editor.
 70 
 71     Friend WithEvents Button1 As System.Windows.Forms.Button
 72 
 73     Friend WithEvents Button2 As System.Windows.Forms.Button
 74 
 75     Friend WithEvents Button3 As System.Windows.Forms.Button
 76 
 77     Friend WithEvents Button4 As System.Windows.Forms.Button
 78 
 79     Friend WithEvents Button5 As System.Windows.Forms.Button
 80 
 81     Friend WithEvents Button6 As System.Windows.Forms.Button
 82 
 83     Friend WithEvents Button7 As System.Windows.Forms.Button
 84 
 85     Friend WithEvents Button8 As System.Windows.Forms.Button
 86 
 87     Friend WithEvents Button9 As System.Windows.Forms.Button
 88 
 89     Friend WithEvents Button10 As System.Windows.Forms.Button
 90 
 91     Friend WithEvents Button11 As System.Windows.Forms.Button
 92 
 93     Friend WithEvents Button12 As System.Windows.Forms.Button
 94 
 95     Friend WithEvents Button13 As System.Windows.Forms.Button
 96 
 97     Friend WithEvents Button14 As System.Windows.Forms.Button
 98 
 99     Friend WithEvents Button15 As System.Windows.Forms.Button
100 
101     Friend WithEvents Button16 As System.Windows.Forms.Button
102 
103     Friend WithEvents Button17 As System.Windows.Forms.Button
104 
105     Friend WithEvents Button18 As System.Windows.Forms.Button
106 
107     Friend WithEvents Button19 As System.Windows.Forms.Button
108 
109     Friend WithEvents Label1 As System.Windows.Forms.Label
110 
111     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
112 
113         Me.Button1 = New System.Windows.Forms.Button
114 
115         Me.Button2 = New System.Windows.Forms.Button
116 
117         Me.Button3 = New System.Windows.Forms.Button
118 
119         Me.Button4 = New System.Windows.Forms.Button
120 
121         Me.Button5 = New System.Windows.Forms.Button
122 
123         Me.Button6 = New System.Windows.Forms.Button
124 
125         Me.Button7 = New System.Windows.Forms.Button
126 
127         Me.Button8 = New System.Windows.Forms.Button
128 
129         Me.Button9 = New System.Windows.Forms.Button
130 
131         Me.Button10 = New System.Windows.Forms.Button
132 
133         Me.Button11 = New System.Windows.Forms.Button
134 
135         Me.Button12 = New System.Windows.Forms.Button
136 
137         Me.Button13 = New System.Windows.Forms.Button
138 
139         Me.Button14 = New System.Windows.Forms.Button
140 
141         Me.Button15 = New System.Windows.Forms.Button
142 
143         Me.Button16 = New System.Windows.Forms.Button
144 
145         Me.Button17 = New System.Windows.Forms.Button
146 
147         Me.Button18 = New System.Windows.Forms.Button
148 
149         Me.Button19 = New System.Windows.Forms.Button
150 
151         Me.Label1 = New System.Windows.Forms.Label
152 
153         Me.SuspendLayout()
154 
155         '
156 
157         'Button1
158 
159         '
160 
161         Me.Button1.BackColor = System.Drawing.Color.Silver
162 
163         Me.Button1.Location = New System.Drawing.Point(8, 64)
164 
165         Me.Button1.Name = "Button1"
166 
167         Me.Button1.Size = New System.Drawing.Size(32, 23)
168 
169         Me.Button1.TabIndex = 0
170 
171         Me.Button1.Text = "1"
172 
173         Me.Button1.UseVisualStyleBackColor = False
174 
175         '
176 
177         'Button2
178 
179         '
180 
181         Me.Button2.BackColor = System.Drawing.Color.Silver
182 
183         Me.Button2.Location = New System.Drawing.Point(48, 64)
184 
185         Me.Button2.Name = "Button2"
186 
187         Me.Button2.Size = New System.Drawing.Size(32, 23)
188 
189         Me.Button2.TabIndex = 1
190 
191         Me.Button2.Text = "2"
192 
193         Me.Button2.UseVisualStyleBackColor = False
194 
195         '
196 
197         'Button3
198 
199         '
200 
201         Me.Button3.BackColor = System.Drawing.Color.Silver
202 
203         Me.Button3.Location = New System.Drawing.Point(88, 64)
204 
205         Me.Button3.Name = "Button3"
206 
207         Me.Button3.Size = New System.Drawing.Size(32, 23)
208 
209         Me.Button3.TabIndex = 2
210 
211         Me.Button3.Text = "3"
212 
213         Me.Button3.UseVisualStyleBackColor = False
214 
215         '
216 
217         'Button4
218 
219         '
220 
221         Me.Button4.BackColor = System.Drawing.Color.Silver
222 
223         Me.Button4.Location = New System.Drawing.Point(8, 104)
224 
225         Me.Button4.Name = "Button4"
226 
227         Me.Button4.Size = New System.Drawing.Size(32, 23)
228 
229         Me.Button4.TabIndex = 3
230 
231         Me.Button4.Text = "4"
232 
233         Me.Button4.UseVisualStyleBackColor = False
234 
235         '
236 
237         'Button5
238 
239         '
240 
241         Me.Button5.BackColor = System.Drawing.Color.Silver
242 
243         Me.Button5.Location = New System.Drawing.Point(48, 104)
244 
245         Me.Button5.Name = "Button5"
246 
247         Me.Button5.Size = New System.Drawing.Size(32, 23)
248 
249         Me.Button5.TabIndex = 4
250 
251         Me.Button5.Text = "5"
252 
253         Me.Button5.UseVisualStyleBackColor = False
254 
255         '
256 
257         'Button6
258 
259         '
260 
261         Me.Button6.BackColor = System.Drawing.Color.Silver
262 
263         Me.Button6.Location = New System.Drawing.Point(88, 104)
264 
265         Me.Button6.Name = "Button6"
266 
267         Me.Button6.Size = New System.Drawing.Size(32, 23)
268 
269         Me.Button6.TabIndex = 5
270 
271         Me.Button6.Text = "6"
272 
273         Me.Button6.UseVisualStyleBackColor = False
274 
275         '
276 
277         'Button7
278 
279         '
280 
281         Me.Button7.BackColor = System.Drawing.Color.Silver
282 
283         Me.Button7.Location = New System.Drawing.Point(8, 144)
284 
285         Me.Button7.Name = "Button7"
286 
287         Me.Button7.Size = New System.Drawing.Size(32, 23)
288 
289         Me.Button7.TabIndex = 6
290 
291         Me.Button7.Text = "7"
292 
293         Me.Button7.UseVisualStyleBackColor = False
294 
295         '
296 
297         'Button8
298 
299         '
300 
301         Me.Button8.BackColor = System.Drawing.Color.Silver
302 
303         Me.Button8.Location = New System.Drawing.Point(48, 144)
304 
305         Me.Button8.Name = "Button8"
306 
307         Me.Button8.Size = New System.Drawing.Size(32, 23)
308 
309         Me.Button8.TabIndex = 7
310 
311         Me.Button8.Text = "8"
312 
313         Me.Button8.UseVisualStyleBackColor = False
314 
315         '
316 
317         'Button9
318 
319         '
320 
321         Me.Button9.BackColor = System.Drawing.Color.Silver
322 
323         Me.Button9.Location = New System.Drawing.Point(88, 144)
324 
325         Me.Button9.Name = "Button9"
326 
327         Me.Button9.Size = New System.Drawing.Size(32, 23)
328 
329         Me.Button9.TabIndex = 8
330 
331         Me.Button9.Text = "9"
332 
333         Me.Button9.UseVisualStyleBackColor = False
334 
335         '
336 
337         'Button10
338 
339         '
340 
341         Me.Button10.BackColor = System.Drawing.Color.Silver
342 
343         Me.Button10.Location = New System.Drawing.Point(8, 184)
344 
345         Me.Button10.Name = "Button10"
346 
347         Me.Button10.Size = New System.Drawing.Size(32, 23)
348 
349         Me.Button10.TabIndex = 9
350 
351         Me.Button10.Text = "."
352 
353         Me.Button10.UseVisualStyleBackColor = False
354 
355         '
356 
357         'Button11
358 
359         '
360 
361         Me.Button11.BackColor = System.Drawing.Color.Silver
362 
363         Me.Button11.Location = New System.Drawing.Point(48, 184)
364 
365         Me.Button11.Name = "Button11"
366 
367         Me.Button11.Size = New System.Drawing.Size(32, 23)
368 
369         Me.Button11.TabIndex = 10
370 
371         Me.Button11.Text = "0"
372 
373         Me.Button11.UseVisualStyleBackColor = False
374 
375         '
376 
377         'Button12
378 
379         '
380 
381         Me.Button12.BackColor = System.Drawing.Color.Silver
382 
383         Me.Button12.Location = New System.Drawing.Point(88, 184)
384 
385         Me.Button12.Name = "Button12"
386 
387         Me.Button12.Size = New System.Drawing.Size(32, 23)
388 
389         Me.Button12.TabIndex = 11
390 
391         Me.Button12.Text = "C"
392 
393         Me.Button12.UseVisualStyleBackColor = False
394 
395         '
396 
397         'Button13
398 
399         '
400 
401         Me.Button13.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
402 
403         Me.Button13.Location = New System.Drawing.Point(168, 64)
404 
405         Me.Button13.Name = "Button13"
406 
407         Me.Button13.Size = New System.Drawing.Size(32, 23)
408 
409         Me.Button13.TabIndex = 12
410 
411         Me.Button13.Text = "+"
412 
413         Me.Button13.UseVisualStyleBackColor = False
414 
415         '
416 
417         'Button14
418 
419         '
420 
421         Me.Button14.BackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(128, Byte), Integer))
422 
423         Me.Button14.Location = New System.Drawing.Point(224, 64)
424 
425         Me.Button14.Name = "Button14"
426 
427         Me.Button14.Size = New System.Drawing.Size(32, 23)
428 
429         Me.Button14.TabIndex = 13
430 
431         Me.Button14.Text = "-"
432 
433         Me.Button14.UseVisualStyleBackColor = False
434 
435         '
436 
437         'Button15
438 
439         '
440 
441         Me.Button15.BackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(192, Byte), Integer), CType(CType(255, Byte), Integer))
442 
443         Me.Button15.Location = New System.Drawing.Point(168, 104)
444 
445         Me.Button15.Name = "Button15"
446 
447         Me.Button15.Size = New System.Drawing.Size(32, 23)
448 
449         Me.Button15.TabIndex = 14
450 
451         Me.Button15.Text = "*"
452 
453         Me.Button15.UseVisualStyleBackColor = False
454 
455         '
456 
457         'Button16
458 
459         '
460 
461         Me.Button16.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer), CType(CType(255, Byte), Integer))
462 
463         Me.Button16.Location = New System.Drawing.Point(224, 104)
464 
465         Me.Button16.Name = "Button16"
466 
467         Me.Button16.Size = New System.Drawing.Size(32, 23)
468 
469         Me.Button16.TabIndex = 15
470 
471         Me.Button16.Text = "/"
472 
473         Me.Button16.UseVisualStyleBackColor = False
474 
475         '
476 
477         'Button17
478 
479         '
480 
481         Me.Button17.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer), CType(CType(192, Byte), Integer))
482 
483         Me.Button17.Location = New System.Drawing.Point(168, 144)
484 
485         Me.Button17.Name = "Button17"
486 
487         Me.Button17.Size = New System.Drawing.Size(32, 23)
488 
489         Me.Button17.TabIndex = 16
490 
491         Me.Button17.Text = "1/x"
492 
493         Me.Button17.UseVisualStyleBackColor = False
494 
495         '
496 
497         'Button18
498 
499         '
500 
501         Me.Button18.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(224, Byte), Integer), CType(CType(192, Byte), Integer))
502 
503         Me.Button18.Location = New System.Drawing.Point(224, 144)
504 
505         Me.Button18.Name = "Button18"
506 
507         Me.Button18.Size = New System.Drawing.Size(32, 23)
508 
509         Me.Button18.TabIndex = 17
510 
511         Me.Button18.Text = "+-"
512 
513         Me.Button18.UseVisualStyleBackColor = False
514 
515         '
516 
517         'Button19
518 
519         '
520 
521         Me.Button19.BackColor = System.Drawing.Color.RosyBrown
522 
523         Me.Button19.Location = New System.Drawing.Point(184, 192)
524 
525         Me.Button19.Name = "Button19"
526 
527         Me.Button19.Size = New System.Drawing.Size(64, 23)
528 
529         Me.Button19.TabIndex = 18
530 
531         Me.Button19.Text = "="
532 
533         Me.Button19.UseVisualStyleBackColor = False
534 
535         '
536 
537         'Label1
538 
539         '
540 
541         Me.Label1.BackColor = System.Drawing.Color.White
542 
543         Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
544 
545         Me.Label1.Location = New System.Drawing.Point(12, 19)
546 
547         Me.Label1.Name = "Label1"
548 
549         Me.Label1.Size = New System.Drawing.Size(244, 23)
550 
551         Me.Label1.TabIndex = 19
552 
553         '
554 
555         'Form1
556 
557         '
558 
559         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
560 
561         Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer))
562 
563         Me.ClientSize = New System.Drawing.Size(292, 266)
564 
565         Me.Controls.Add(Me.Label1)
566 
567         Me.Controls.Add(Me.Button19)
568 
569         Me.Controls.Add(Me.Button18)
570 
571         Me.Controls.Add(Me.Button17)
572 
573         Me.Controls.Add(Me.Button16)
574 
575         Me.Controls.Add(Me.Button15)
576 
577         Me.Controls.Add(Me.Button14)
578 
579         Me.Controls.Add(Me.Button13)
580 
581         Me.Controls.Add(Me.Button12)
582 
583         Me.Controls.Add(Me.Button11)
584 
585         Me.Controls.Add(Me.Button10)
586 
587         Me.Controls.Add(Me.Button9)
588 
589         Me.Controls.Add(Me.Button8)
590 
591         Me.Controls.Add(Me.Button7)
592 
593         Me.Controls.Add(Me.Button6)
594 
595         Me.Controls.Add(Me.Button5)
596 
597         Me.Controls.Add(Me.Button4)
598 
599         Me.Controls.Add(Me.Button3)
600 
601         Me.Controls.Add(Me.Button2)
602 
603         Me.Controls.Add(Me.Button1)
604 
605         Me.Name = "Form1"
606 
607         Me.Text = "Form1"
608 
609         Me.ResumeLayout(False)
610 
611  
612 
613     End Sub
614 
615  
616 
617 #End Region
618 
619     Private Sub Digit_Click(ByVal sender As System.Object, _
620 
621     ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, _
622 
623     Button3.Click, Button4.Click, Button5.Click, Button6.Click, _
624 
625    Button7.Click, Button8.Click, Button9.Click, Button11.Click
626 
627         If clearDisplay Then
628 
629             Label1.Text = ""
630 
631             clearDisplay = False
632 
633         End If
634 
635         Label1.Text = Label1.Text + sender.text
636 
637     End Sub
638 
639  
640 
641     Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
642 
643         Label1.Text = ""
644 
645     End Sub
646 
647  
648 
649     Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
650 
651         Operand1 = Val(Label1.Text)
652 
653         [Operator] = "+"
654 
655         clearDisplay = True
656 
657     End Sub
658 
659  
660 
661     Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
662 
663         Dim result As Double
664 
665         Operand2 = Val(Label1.Text)
666 
667         Try
668 
669             Select Case [Operator]
670 
671                 Case "+"
672 
673                     result = Operand1 + Operand2
674 
675                 Case "-"
676 
677                     result = Operand1 - Operand2
678 
679                 Case "*"
680 
681                     result = Operand1 * Operand2
682 
683                 Case "/"
684 
685                     If Operand2 <> "0" Then result = Operand1 / Operand2
686 
687             End Select
688 
689             Label1.Text = result
690 
691         Catch exc As Exception
692 
693             MsgBox(exc.Message)
694 
695             Label1.Text = "ERROR"
696 
697         Finally
698 
699             clearDisplay = True
700 
701         End Try
702 
703     End Sub
704 
705  
706 
707     Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
708 
709         If Label1.Text.IndexOf(".") > 0 Then
710 
711             Exit Sub
712 
713         Else
714 
715             Label1.Text = Label1.Text & "."
716 
717         End If
718 
719     End Sub
720 
721  
722 
723     Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
724 
725         If Val(Label1.Text) <> 0 Then Label1.Text = 1 / Val(Label1.Text)
726 
727         clearDisplay = True
728 
729     End Sub
730 
731  
732 
733     Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
734 
735  
736 
737         Operand1 = Val(Label1.Text)
738 
739         [Operator] = "-"
740 
741         clearDisplay = True
742 
743     End Sub
744 
745  
746 
747     Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
748 
749         Label1.Text = -Val(Label1.Text)
750 
751         clearDisplay = True
752 
753     End Sub
754 
755  
756 
757     Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
758 
759         Operand1 = Val(Label1.Text)
760 
761         [Operator] = "*"
762 
763         clearDisplay = True
764 
765     End Sub
766 
767  
768 
769     Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
770 
771         Operand1 = Val(Label1.Text)
772 
773         [Operator] = "/"
774 
775         clearDisplay = True
776 
777     End Sub
778 
779 End Class

转载于:https://www.cnblogs.com/fantasynoff/archive/2012/05/07/2487092.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值