次の方法で共有


TrackBar.Scroll イベント

マウス操作またはキー操作でスライダが移動すると発生します。

Public Event Scroll As EventHandler
[C#]
public event EventHandler Scroll;
[C++]
public: __event EventHandler* Scroll;

[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。

イベント データ

イベント ハンドラが EventArgs 型の引数を受け取りました。

解説

Scroll デリゲートを作成する場合は、イベントを処理するメソッドを識別してください。イベントをイベント ハンドラに関連付けるには、デリゲートのインスタンスをイベントに追加します。デリゲートを削除しない限り、そのイベントが発生すると常にイベント ハンドラが呼び出されます。

イベント ハンドラ デリゲートの詳細については、「 イベントとデリゲート 」を参照してください。

使用例

[Visual Basic, C#, C++] Scroll イベントを実装し、トラック バーのスライダのスクロールに応じて、フォーム上の情報ラベルを変更する方法を次の例に示します。この例は、 Form に 3 つのトラック バーと 3 つのラベルが配置されていることを前提にしています。

 
Private Sub showColorValueLabels()
    label1.Text = "Red value is : " & trackBar1.Value.ToString()
    label3.Text = "Green Value is : " & trackBar2.Value.ToString()
    label2.Text = "Blue Value is : " & trackBar3.Value.ToString()
End Sub

Private Sub trackBar_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles trackBar1.Scroll, trackBar2.Scroll, trackBar3.Scroll
    Dim myTB As System.Windows.Forms.TrackBar
    myTB = sender
    panel1.BackColor = Color.FromArgb(trackBar1.Value, trackBar2.Value, trackBar3.Value)
    myTB.Text = "Value is " & myTB.Value.ToString()
    showColorValueLabels()
End Sub

[C#] 
private void showColorValueLabels()
{
    label1.Text = "Red value is : " + trackBar1.Value.ToString();
    label3.Text = "Green Value is : " + trackBar2.Value.ToString();
    label2.Text = "Blue Value is : " + trackBar3.Value.ToString();
}
private void trackBar_Scroll(object sender, System.EventArgs e)
{
    System.Windows.Forms.TrackBar myTB;
    myTB = (System.Windows.Forms.TrackBar) sender ;
    panel1.BackColor = Color.FromArgb(trackBar1.Value,trackBar2.Value,trackBar3.Value);
    myTB.Text = "Value is " + myTB.Value.ToString();
    showColorValueLabels();
}

[C++] 
private:
    void showColorValueLabels()
    {
        label1->Text = String::Format( S"Red value is : {0}", __box(trackBar1->Value));
        label3->Text = String::Format( S"Green Value is : {0}", __box(trackBar2->Value));
        label2->Text = String::Format( S"Blue Value is : {0}", __box(trackBar3->Value));
    }

    void trackBar_Scroll(Object* sender, System::EventArgs* /*e*/)
    {
        System::Windows::Forms::TrackBar* myTB;
        myTB = dynamic_cast<System::Windows::Forms::TrackBar*> (sender );
        panel1->BackColor = Color::FromArgb(trackBar1->Value,trackBar2->Value,trackBar3->Value);
        myTB->Text = String::Format( S"Value is {0}", __box(myTB->Value));
        showColorValueLabels();
    }

[Visual Basic, C#, C++] TrackBar コントロールと TextBox コントロールが配置されたフォームを表示する例を次に示します。この例では、 MaximumTickFrequencyLargeChangeSmallChange の各プロパティを設定して、 Scroll イベントを処理しています。 TextBox の内容は、 Scroll イベントが発生したときに、 Value プロパティの値に更新されます。

 
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents trackBar1 As System.Windows.Forms.TrackBar
    Private textBox1 As System.Windows.Forms.TextBox

    <System.STAThread()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New Form1)
    End Sub 'Main

    Public Sub New()
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.trackBar1 = New System.Windows.Forms.TrackBar

        ' TextBox for TrackBar.Value update.
        Me.textBox1.Location = New System.Drawing.Point(240, 16)
        Me.textBox1.Size = New System.Drawing.Size(48, 20)

        ' Set up how the form should be displayed and add the controls to the form.
        Me.ClientSize = New System.Drawing.Size(296, 62)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.textBox1, Me.trackBar1})
        Me.Text = "TrackBar Example"

        ' Set up the TrackBar.
        Me.trackBar1.Location = New System.Drawing.Point(8, 8)
        Me.trackBar1.Size = New System.Drawing.Size(224, 45)

        ' The Maximum property sets the value of the track bar when
        ' the slider is all the way to the right.
        trackBar1.Maximum = 30

        ' The TickFrequency property establishes how many positions
        ' are between each tick-mark.
        trackBar1.TickFrequency = 5

        ' The LargeChange property sets how many positions to move
        ' if the bar is clicked on either side of the slider.
        trackBar1.LargeChange = 3

        ' The SmallChange property sets how many positions to move
        ' if the keyboard arrows are used to move the slider.
        trackBar1.SmallChange = 2
    End Sub 'New

    Private Sub trackBar1_Scroll(ByVal sender As Object, _
                    ByVal e As System.EventArgs) Handles trackBar1.Scroll

        ' Display the trackbar value in the text box.
        textBox1.Text = trackBar1.Value
    End Sub 

End Class 'Form1

[C#] 
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.TrackBar trackBar1;
    private System.Windows.Forms.TextBox textBox1;

    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.trackBar1 = new System.Windows.Forms.TrackBar();

        // TextBox for TrackBar.Value update.
        this.textBox1.Location = new System.Drawing.Point(240, 16);
        this.textBox1.Size = new System.Drawing.Size(48, 20);

        // Set up how the form should be displayed and add the controls to the form.
        this.ClientSize = new System.Drawing.Size(296, 62);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox1,this.trackBar1});
        this.Text = "TrackBar Example";

        // Set up the TrackBar.
        this.trackBar1.Location = new System.Drawing.Point(8, 8);
        this.trackBar1.Size = new System.Drawing.Size(224, 45);
        this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);

        // The Maximum property sets the value of the track bar when
        // the slider is all the way to the right.
        trackBar1.Maximum = 30;
        
        // The TickFrequency property establishes how many positions
        // are between each tick-mark.
        trackBar1.TickFrequency = 5;

        // The LargeChange property sets how many positions to move
        // if the bar is clicked on either side of the slider.
        trackBar1.LargeChange = 3;

        // The SmallChange property sets how many positions to move
        // if the keyboard arrows are used to move the slider.
        trackBar1.SmallChange = 2;
    }


    private void trackBar1_Scroll(object sender, System.EventArgs e)
    {
        // Display the trackbar value in the text box.
        textBox1.Text = "" + trackBar1.Value;
    }
}

[C++] 

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class Form1 : public System::Windows::Forms::Form {
private:
    System::Windows::Forms::TrackBar*  trackBar1;
    System::Windows::Forms::TextBox*  textBox1;

public:
    Form1() {
        this->textBox1 = new System::Windows::Forms::TextBox();
        this->trackBar1 = new System::Windows::Forms::TrackBar();

        // TextBox for TrackBar::Value update.
        this->textBox1->Location = System::Drawing::Point(240, 16);
        this->textBox1->Size = System::Drawing::Size(48, 20);

        // Set up how the form should be displayed and add the controls to the form.
        this->ClientSize = System::Drawing::Size(296, 62);
        System::Windows::Forms::Control* formControls[] = {
            this->textBox1, this->trackBar1};
        this->Controls->AddRange(formControls);
        this->Text = S"TrackBar Example";

        // Set up the TrackBar.
        this->trackBar1->Location = System::Drawing::Point(8, 8);
        this->trackBar1->Size = System::Drawing::Size(224, 45);
        this->trackBar1->Scroll += new 
            System::EventHandler(this, &Form1::trackBar1_Scroll);

        // The Maximum property sets the value of the track bar when
        // the slider is all the way to the right.
        trackBar1->Maximum = 30;

        // The TickFrequency property establishes how many positions
        // are between each tick-mark.
        trackBar1->TickFrequency = 5;

        // The LargeChange property sets how many positions to move
        // if the bar is clicked on either side of the slider.
        trackBar1->LargeChange = 3;

        // The SmallChange property sets how many positions to move
        // if the keyboard arrows are used to move the slider.
        trackBar1->SmallChange = 2;
    }

private:
    void trackBar1_Scroll(Object* /*sender*/, System::EventArgs* /*e*/) {
        // Display the trackbar value in the text box.
        textBox1->Text = String::Concat(S"", __box(trackBar1->Value));
    }
};

[STAThread]
int main() {
    Application::Run(new Form1());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TrackBar クラス | TrackBar メンバ | System.Windows.Forms 名前空間