C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 55 を表示中)
■30902 / )  Re[1]: ユーザーコントロールについて
□投稿者/ おてやわらか (1回)-(2009/01/08(Thu) 18:46:56)
ユーザーコントロール側に、Label1が配置してあるとして

ユーザーコントロール側で、

    '----------------------------------------------
    'ラベル
    '----------------------------------------------
    Property ClockEnable() As Boolean
        Get
            ClockEnable = Label1.Enabled
        End Get
        '
        Set(ByVal value As Boolean)
            Label1.Enabled = value
        End Set

    End Property

といったような、プロパティは用意してありますか?
用意していない場合は、作成したユーザーコントロールには、
ユーザーコントロールとしての、プロパティしかないので、
Label1単独のプロパティの変更は、出来ないことになります。
以下は、Labelに時計を表示するサンプルです。
たぶんMSのサイトの切り貼りですが、出典が残っていません。
サンプルですので、実際上は変更改造が必要になるかもしれません。
また、ベストな書き方であるとは、思っていません。
使用する側のプロパティ設定:ClockEnable=True/Falseで動作が変わります。
ユーザーコントロールのタイマーイベントも実装してあります。
lblTime:ラベル名
Timer1:タイマー名、インターバルは、1000で設定。

Public Class ctlClock
    '
    Private colFColor As Color
    Private colBColor As Color
    '----------------------------------------------
    '背景色
    ' Declares the name and type of the property.
    '----------------------------------------------
    Property ClockBackColor() As Color
        ' Retrieves the value of the private variable colBColor.
        Get
            Return colBColor
        End Get
        ' Stores the selected value in the private variable colBColor, and 
        ' updates the background color of the label control lblDisplay.
        Set(ByVal value As Color)
            colBColor = value
            lblTime.BackColor = colBColor
        End Set

    End Property
    '----------------------------------------------
    '前景色
    ' Provides a similar set of instructions for the foreground color.
    '----------------------------------------------
    Property ClockForeColor() As Color
        Get
            Return colFColor
        End Get
        Set(ByVal value As Color)
            colFColor = value
            lblTime.ForeColor = colFColor
        End Set
    End Property
    '----------------------------------------------
    'タイマインターバル
    '----------------------------------------------
    Property ClockInterval() As Long
        Get
            ClockInterval = Timer1.Interval
        End Get
        '
        Set(ByVal value As Long)
            Timer1.Interval = value
        End Set

    End Property
    '----------------------------------------------
    'タイマ
    '----------------------------------------------
    Property ClockEnable() As Boolean
        Get
            ClockEnable = Timer1.Enabled
        End Get
        '
        Set(ByVal value As Boolean)
            Timer1.Enabled = value
        End Set

    End Property
    '
    '----------------------------------------------
    'Timer Event 定義
    '----------------------------------------------
    Public Event ctlClock_Timer_tick(ByVal sender As Object, ByVal e As System.EventArgs)
    '
    Protected Overridable Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' Causes the label to display the current time.  コメントしてある部分の動作の違いに注意。
        'lblTime.Text = Format(Now, "yyyy/MM/dd (ddd) HH:mm:ss")
        lblTime.Text = Now.ToString("yyyy年MM月dd日 (ddd) HH:mm:ss", System.Globalization.CultureInfo.CreateSpecificCulture("ja-JP"))
        'lblTime.Text = Now.ToString("(ddd) d/MMMM/yyyy hh:mm:ss", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))

        '----------------------------------------------
        'Timer Event 発生
        '----------------------------------------------
        RaiseEvent ctlClock_Timer_tick(sender, e)
    End Sub

End Class

返信 編集キー/


管理者用

- Child Tree -