0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[vb.net] テンキーがなくても...画面上操作

Posted at

#はじめに
テンキーがなくても画面上で操作出来る物を作ってみました。
スクリーンショット 2023-07-22 191519.png

#目次

  1. ソースコード
  2. "&="の意味について
  3. 動作動画

1.ソースコード

VB.net
Public Class Form1

   '「Label」と「Button」に表示する文字の指示を行う。
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Label1.Text = ""
        Button1.Text = "0"
        Button2.Text = "1"
        Button3.Text = "2"
        Button4.Text = "3"
        Button5.Text = "4"
        Button6.Text = "5"
        Button7.Text = "6"
        Button8.Text = "7"
        Button9.Text = "8"
        Button10.Text = "9"
        Button11.Text = "クリア"

    End Sub

   'Label1に「0」と表示する
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text &= "0"
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text &= "1"
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Label1.Text &= "2"
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Label1.Text &= "3"
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Label1.Text &= "4"
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Label1.Text &= "5"
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Label1.Text &= "6"
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Label1.Text &= "7"
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        Label1.Text &= "8"
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        Label1.Text &= "9"
    End Sub

   'クリアボタンを押された時に「Label1」の文字列を空白にする。
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Label1.Text = ""
    End Sub

End Class

2.「 &= の意味について」

VB.NETの"&="演算子は、文字列の連結と代入を同時に行います。

VB.net
Label1.Text &= "0"

このように書き換えることができます

VB.net
Label1.Text = Label1.Text & "0"

"&"演算子は二つの文字列を連結します。そして、"="演算子は左辺の変数に右辺の値を代入します。
したがって、"&="演算子は、右側の値を現在の値に追加(連結)し、結果を左側の変数に代入するという操作を行います。

・分かりやすいように、イメージ図が以下のようになっています。
※「Label1.Text = Label1.Text & "0"」と書いてありますが、「Label1.Text &= "0"」と同じになります。
テンキー.jpg

結論として、
"&="がないとLabel1に一文字しか入力されない。

3.動作動画

テンキーー.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?