VB.NETの場合はビット演算が簡単に使えます。
先ずSiftですが左Siftは<<、右Siftは>>です。
>10進7=2進0111これを左シフトすると2進1110=10進14は
Dim i As Integer = (7 << 1) 又は
Dim i As Integer = 7
i = i << 1 又は
i <<= 1
となります、2つシフトしたければ i<<2とします。
ある数のあるビットが立っているかいないか調べることをビットテストと言います。
ビットテストにはAnd演算子を使います。
これはテストしたいビットだけが立った数値とテストしたい数値のAndで実現します。
1100100 (10進 100)
And 0000100 (10進 4)
-------------------------
0000100
ANDの結果が0ならビットが立っていません。
コードは
Dim i As Integer = 100
Dim j As Integer = 2 '番目のビット
Dim k = i And 2 ^ j
If (i And 2 ^ j) > 0 Then
MessageBox.Show((j + 1).ToString + "番目のビットが立っています")
Else
MessageBox.Show((j + 1).ToString + "番目のビットが立っていません")
End If
i = 100
i = i And 123
MessageBox.Show(i.ToString())
強制的に1にするにはOrを使います
3ビット目を強制的に1にしています
1100000 (10進 96)
Or 0000100 (10進 4)
---------------------------
1100100 (10進 100)
i = 32
i = i Or 4
MessageBox.Show(i.ToString())
以上です
デリゲート:
大体そんな感じですね。
テキストボックス3つとぼたん3つを置いて、ボタンを押すと
ボタンと同じ番号のTextBoxに書き込むコードを示します。
これを応用してみてください。
Delegate Sub degaddText(ByVal textBoxNo As Integer, ByVal textBoxText As String)
Private Sub addText(ByVal textBoxNo As Integer, ByVal textBoxText As String)
Select Case textBoxNo
Case 1
TextBox1.Text += textBoxText
Case 2
TextBox2.Text += textBoxText
Case 3
TextBox2.Text += textBoxText
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim textBoxNo As Integer = 1
Dim textBoxText = "Button1"
Me.Invoke(New degaddText(AddressOf addText), New Object() {textBoxNo, textBoxText})
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
Dim textBoxNo As Integer = 2
Dim textBoxText = "Button2"
Me.Invoke(New degaddText(AddressOf addText), New Object() {textBoxNo, textBoxText})
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button3.Click
Dim textBoxNo As Integer = 3
Dim textBoxText = "Button3"
Me.Invoke(New degaddText(AddressOf addText), New Object() {textBoxNo, textBoxText})
End Sub
...続き
インデントが無くて読みにくいため、半角スペース2つを全角スペースに置き換えてみました。
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged
Static strBstr As String '前の文字列を覚えている
Static intPos As Integer '前のキャレットの位置を覚えている
Static boolFlg '文字修正時の再突入を防ぐフラグ
If boolFlg Then
Return
End If
boolFlg = True
'TextBox1.Text(i)でi番目の一文字を取り出します。
'TextBox1.Text(i)は配列でなくインデクサというプロパティです。
For i As Integer = 0 To TextBox1.Text.Length - 1
If (TextBox1.Text(i) < "0"c Or TextBox1.Text(i) > "9"c) _
And (TextBox1.Text(i) < "a"c Or TextBox1.Text(i) > "z"c) _
And (TextBox1.Text(i) < "A"c Or TextBox1.Text(i) > "Z"c) Then
'入力文字が想定外
MessageBox.Show("入力文字が違います")
TextBox1.Text = strBstr '前の文字に戻す
TextBox1.SelectionStart = intPos '前の位置に戻す
boolFlg = False
Return
End If
Next
strBstr = TextBox1.Text '文字列を記憶
intPos = TextBox1.SelectionStart 'キャレットの位置を記憶
boolFlg = False
End Sub