2017-11-07 6 views
0

私は約400のテキストボックスがあるプログラムに取り組んでおり、フォーカスがあることを示すようにエフェクトをプログラムする必要があります。 VBでテキストボックスにソフトな青いアウトラインを追加する方法が分かっていない限り、視覚的な部分を表示することはできますが、すべてのテキストボックスを一度に処理するGotFocusイベントとLostFocusイベントの作成には問題があります。私は試しましたフォーカスを視覚的に表示し、クリックイベントを追加するすべてのテキストボックスに

Dim txtBox = Me.Controls.OfType(Of TextBox) 
Private Sub TextBox_GotFocus(sender As Object, e As EventArgs) Handles txtBox.GotFocus 

しかし、私は "WithEvents変数を持っている必要があります"私はかなり修正する方法を理解していないエラーが表示されます。私は試しました

Public Sub txtBoxGotFocusHandler(ByVal sender As Object, 
ByVal e As System.EventArgs) 
     For Each txtBox As TextBox In Me.Controls 'References all text boxes in form 
      If txtBox.Focus = True Then 
       txtBox.BackColor = Color.Black 
      End If 
     Next 

私はインターネットの周りに見たいくつかの他のいくつかの関連するものを試してみましたが、役に立たない。どんな助けもありがたいです

+4

「約400個のテキストボックスがあります」ユーザーはあなたを愛する必要があります。そのイベントのコントロールをループする必要はありません。送信者の引数は、どれが – Plutonix

+1

@Plutonixかもしれないことを教えてくれるでしょうか? – Misaz

+0

400 TextBoxes =グリッドを使用 – LarsTech

答えて

0

実行時に任意のコントロールでアプリケーションを作成することができます。あなたは、SQLからあなたのアプリケーションのレイアウトを照会することができ、あなたのアプリケーションレイアウトの変更を簡単に変更できます。

Private FocusRectangle As System.Drawing.Graphics 
Private OldRectangle As System.Drawing.Graphics 
Private MyTextBoxes As New List(Of TextBox) 
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    MyTextBoxes.Clear() 
    For xcount = 0 To 399 
     MyTextBoxes.Add(New TextBox) 
     With MyTextBoxes.Item(xcount) 
      .Name = "MyTextBoxes" & (xcount + 1).ToString 
      .Text = "" 
      .Location = New Point(0, 0) 
      .Size = New Size(50, 13) 
      .Visible = True 
      AddHandler .GotFocus, AddressOf MyTextBoxes_GotFocus 
      AddHandler .LostFocus, AddressOf MyTextBoxes_LostFocus 


     End With 
     Me.Controls.Add(MyTextBoxes.Item(xcount)) 
     'add them to a panel.... 
     'Panel1.Controls.add(MyTextBoxes.Item(xcount)) 
    Next 
End Sub 
Sub MyTextBoxes_GotFocus(sender As Object, e As EventArgs) 
    Dim ThisTextBox As TextBox = DirectCast(sender, TextBox) 
    Dim xPen As New System.Drawing.Pen(Color.LightBlue) 
    FocusRectangle = Me.CreateGraphics() 
    FocusRectangle.DrawRectangle(xPen, ThisTextBox.Location.X - 1, ThisTextBox.Location.Y - 1, ThisTextBox.Size.Width + 1, ThisTextBox.Size.Height + 1) 
    OldRectangle = FocusRectangle 
End Sub 
Sub MyTextBoxes_LostFocus(sender As Object, e As EventArgs) 
    Dim ThisTextBox As TextBox = DirectCast(sender, TextBox) 
    OldRectangle.Dispose() 
End Sub 

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove 
    MyTextBoxes.Item(0).Focus() 
End Sub 
0

デザイナーでフォームを作成した場合、WithEventsが自動的に追加されます。

あなたがプライベートフィールドとして400個のテキストボックスを宣言している場合は、あなたがすることはできません、その後、プログラム的にテキストボックスを作成し、テキストボックスか何かのコレクションに追加している場合は、Private WitheEvents txtBox As TextBox

としてそれらを宣言しますWithEventsを実行します。

しかし、すべてのWithEventsでは、関数にHandeles TextBox.SomeEventを追加することができます。代わりにこれを行うことができます:

Dim txtBox As New TextBox 
    ... 
    AddHandler txtBox.GotFocus, AddressOf txtBoxGotFocusHandler 
    Me.Controls.Add(txtBox)