0

こんにちは、フォームにラベルを作成する次のループがあります。

  private Label newLabel = new Label();
    private int txtBoxStartPosition = 300;
    private int txtBoxStartPositionV = 25;

    private void button1_Click(object sender, EventArgs e)
    {
        int txt = Int32.Parse(textBox1.Text);
        for (int i = 0; i < txt; i++)
        {
            newLabel = new Label();
            newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
            newLabel.Size = new System.Drawing.Size(25, 25);
            newLabel.Text = i.ToString();
            newLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            newLabel.ForeColor = Color.Red;
            newLabel.Font = new Font(newLabel.Font.FontFamily.Name, 10);
            newLabel.Font = new Font(newLabel.Font, FontStyle.Bold);
            newLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;  

            this.Controls.Add(newLabel);
            txtBoxStartPosition -= 35;


        }

そして、MouseMove と MouseDown にいくつかのイベントがあり、コントロールをマウスでつかんでドロップできるようにします。

        private Point MouseDownLocation;

    private void MyControl_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void MyControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            label1.Left = e.X + label1.Left - MouseDownLocation.X;
            label1.Top = e.Y + label1.Top - MouseDownLocation.Y;
        }
    }

私の質問は、これらのイベントを新しく作成したラベルに割り当てる方法はありますか?

お時間をいただきありがとうございます。

4

2 に答える 2

3

これを試して:

newLabel.MouseMove += MyControl_MouseMove;
newLabel.MouseDown += MyControl_MouseDown;

ジェイ

于 2013-09-30T21:33:00.820 に答える