掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
マウスクリック時にTextBoxの文字列反転表示をするには? (ID:119299)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
>カスタムコントロールを作るのが一番良い方法みたいですね。 いや、そんな事は言ってませんが…。オブジェクト指向と[VB.NET] の構造にまだ十分な知識をお持ちでないからでしょうが、クラス という単位で考える必要があります。この場合、カスタムコント ロールも、フォームもクラスなのです。ユーザ定義のカスタム コントロールを作るのに何か問題がある場合、普通にクラス化 する…つまりユーザ定義のクラスを作るのにも問題がある可能性 が高い…というか同じ意味だと言ったのです。まぁ、それが ダメとか言われるとオブジェクト指向そのものがダメな感じも しますが。 クラス化はただクラスを作るだけなのでデザイン画面に影響する 場合はまずありませんが?…というか影響するように書かなければ 良いだけの話です。 案1:Tag にフラグ設定型 [VB.NET] Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter Debug.WriteLine("TextBox1_Enter") TextBox1.Tag = True End Sub Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown Debug.WriteLine("TextBox1_MouseDown") If CBool(TextBox1.Tag) Then TextBox1.SelectAll() TextBox1.Tag = False End If End Sub 案1(共通関数化) [VB.NET] Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter Call Me.SetTextBoxSelectAll(DirectCast(sender, TextBox)) End Sub Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown Call Me.CheckTextBoxSelectAll(DirectCast(sender, TextBox)) End Sub Public Sub SetTextBoxSelectAll(ByVal TextBoxX As TextBox) With TextBoxX .Tag = True .SelectAll() 'キーボード Enter 対応 End With End Sub Public Sub CheckTextBoxSelectAll(ByVal TextBoxX As TextBox) With TextBoxX If CBool(.Tag) Then .SelectAll() .Tag = False End If End With End Sub 案2:Timer 使用クラス呼び出し型 [VB.NET] Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form [省略]" Windows フォーム デザイナで生成されたコード " Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter Debug.WriteLine("TextBox1_Enter") Call Me.TextBoxSelectAll(DirectCast(sender, TextBox)) End Sub Private Sub TextBoxSelectAll(ByVal TextBoxX As TextBox) Dim wEventClass As New TextBoxEventClass(TextBoxX) TextBoxX.SelectAll() 'キーボード Enter 対応 Call wEventClass.SelectAllTimerRun() End Sub End Class Public Class TextBoxEventClass Private mTextBox As TextBox Private WithEvents mTimer As New Timer 'Public Event GotFocus(ByVal TextBoxX As TextBox) Public Sub New(ByVal TextBoxX As TextBox) Me.mTextBox = TextBoxX 'Call Me.SelectAllTimerRun() End Sub Public Sub SelectAllTimerRun() With Me.mTimer .Interval = 1 .Start() End With End Sub Private Sub mTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles mTimer.Tick Me.mTimer.Stop() Debug.WriteLine("mTimer_Tick") Me.mTextBox.SelectAll() End Sub End Class
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.