【问题标题】:Declaring pictureboxes as individual objects and changing their locations将图片框声明为单个对象并更改其位置
【发布时间】:2019-05-23 15:01:23
【问题描述】:

我希望能够声明多个具有不同位置、背景颜色和标签的独立图片框。我想使用标签来引用我的代码中的图片框,以单独更改和/或删除它们。 我该怎么办?

我试图创建一个单独的类,用于创建与公共静态相同的图片框,但遇到了 trbRed.Value 值的一些问题,因为它们在该类中不存在。我不确定这个解决方案是否适合我的事业,我也不知道如何以任何一种方式创建类。

此程序使用拖放,因此使用鼠标位置 创建具有不同背景颜色的图片框。

picBlock 在类的前面声明为 PictureBox picBlock = new PictureBox();.

private void myPanel_MouseMove(object sender, MouseEventArgs e)
        {
            int mouseX = e.X / 25 + 1;
            int mouseY = (myPanel.Size.Height - e.Y) / 25 + 1;

            if (myPanel.BackgroundImage != null)
            {
                lblMousePos.Show();
                lblMousePos.Text = "pos (" + mouseX + ", " + mouseY + ")";
            }
        }

public void myPanel_DragDrop(object sender, DragEventArgs e)
{
    picBlock = new PictureBox
    {
        BackColor = Color.FromArgb(255, trbRed.Value, 
        trbGreen.Value, trbBlue.Value),

        Image = Image.FromFile("my picture here.png"),

        Size = new Size(25, 24),

        Location = new Point((mouseX - 1) * 25, 
        this.myPanel.Height - mouseY * 24),

        SizeMode = PictureBoxSizeMode.Zoom,

        Tag = mouseX.ToString() + ", " + mouseY.ToString()
    };

    listX_pos.Add(mouseX);
    listY_pos.Add(mouseY);

    picBlock.MouseMove += picBlock_mousemove;
    picBlock.MouseDown += picBlock_mousedown;

    this.myPanel.Controls.Add(picBlock);
}

public void picBlock_mousedown (object sender, MouseEventArgs e)
        {
            this.myPanel.Controls.Remove(picBlock);
            for (int i = 0; i < listX_pos.Count; i++)
            {
                if (listX_pos[i] == ((picBlock.Location.X / 25) + 1) && listY_pos[i] == (this.myPanel.Location.Y - picBlock.Location.Y / 25) - 11)
                {
                    listX_pos.RemoveAt(i);
                    listY_pos.RemoveAt(i);
                }
            }
//pcbPicture is a picturebox with the same content of "my picture here.png"
            picBlock.DoDragDrop(pcbPicture.Image, DragDropEffects.Move);
        }

public void picBlock_mousemove(object sender, MouseEventArgs e)
        {
            if (myPanel.BackgroundImage != null)
            {
//a label to show the position of the mouse
                lblMousePos.Show();
                Point point = myPanel.PointToClient(MousePosition);
                int mouseX = point.X / 25 + 1;
                int mouseY = (myPanel.Size.Height - point.Y) / 25 + 1;
                lblMousePos.Text = "pos (" + mouseX + ", " + mouseY + ")";
            }
        }

我在创建图片框时没有问题,但只能从picBlock_mousemovepicBlock_mousedown voids 更改最近创建的图片框的位置。
我希望能够更改任何创建的图片框以及它们自己的控件(mousemove 和 mousedown)的位置。

【问题讨论】:

  • 我的水晶球说你在鼠标移动处理程序中使用picBlock。检查使用 sender 是否会让你开心(在它被投射到 PictureBox 之后)。
  • 您使用的是哪种 GUI 技术/我猜是 winforms,对吗?
  • @ChayimFriedman .NET Framework 4.6.1 (winforms)
  • @rene 我的鼠标移动处理程序包含对象发送者,如果这就是你的意思。 '''picBlock_mousemove(对象发送者,MouseEventArgs e)'''。你的意思是说我在课堂前面对 picBlock 的声明应该包含 sender?
  • 如果您愿意向我们展示您的 mousemove/down 处理程序,也许我们可以帮助您。

标签: c# .net


【解决方案1】:

正如@rene 所说,您的问题是在您的picBlock 鼠标处理程序中,您引用了picBlock,它只包含最后创建的图像。

解决方案在事件中。按照惯例,.NET 中的每个事件都有两个属性:object senderSystem.EventArgs e

e 越不稳定;它是一个从 System.EventArgs(或它自己)驱动的类,它包含有关事件的一些必需信息:按下的鼠标按钮/键盘键、鼠标 X 和 Y 等等 - 任何相关信息。

sender 不太可用。它包含触发事件的对象。

为什么?不能直接访问吗,比如通过成员变量?

答案是您的(和类似的)案例:我们将单个方法附加到某些对象的事件,然后我们想知道要在哪个元素上工作。例如,一个简单的示例,每次单击按钮时都会弹出一个消息框,其中包含其内容:

private void Form_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        var btn = new Button
        {
            Text = "Button #" + i, Top = i * 50, Left = 0
        };
        btn.Click += btn_Click;
        this.Controls.Add(btn);
    }
}

private void btn_Clicked(object sender, EventArgs e)
{
    MessageBox.Show(((Button)sender).Text); // Access the clicked button by `sender`
}

回到你的问题,像这样使用它(你不需要成员变量picBlock):

private void myPanel_MouseMove(object sender, MouseEventArgs e)
{
        if (myPanel.BackgroundImage != null)
        {
            lblMousePos.Show();
            lblMousePos.Text = "pos (" + (e.X / 25 + 1) + ", " + ((myPanel.Size.Height - e.Y) / 25 + 1) + ")";
        }
}

public void myPanel_DragDrop(object sender, DragEventArgs e)
{
    var picBlock = new PictureBox
    {
        BackColor = Color.FromArgb(255, trbRed.Value, 
        trbGreen.Value, trbBlue.Value),

        Image = Image.FromFile("my picture here.png"),

        Size = new Size(25, 24),

        Location = new Point((mouseX - 1) * 25, 
        this.myPanel.Height - mouseY * 24),

        SizeMode = PictureBoxSizeMode.Zoom,

        //Tag = musX.ToString() + ", " + musY.ToString()
        // You don't need the Tag property
    };

    picBlock.MouseMove += picBlock_mousemove;
    picBlock.MouseDown += picBlock_mousedown;

    this.myPanel.Controls.Add(picBlock);
}

public void picBlock_mousedown (object sender, MouseEventArgs e)
{
        var picBlock = (PictureBlock)sender;
        this.panelSpel.Controls.Remove(picBlock);
        for (int i = 0; i < listX_pos.Count; i++)
        {
            if (listX_pos[i] == ((picBlock.Location.X / 25) + 1) && listY_pos[i] == (this.myPanel.Location.Y - picBlock.Location.Y / 25) - 11)
            {
                listX_pos.RemoveAt(i);
                listY_pos.RemoveAt(i);
            }
        }
        //pcbPicture is a picturebox with the same content of "my picture here.png"
        picBlock.DoDragDrop(pcbPicture.Image, DragDropEffects.Move);
}

public void picBlock_mousemove(object sender, MouseEventArgs e)
{
        var picBlock = (PictureBlock)sender;
        if (myPanel.BackgroundImage != null)
        {
            //a label to show the position of the mouse
            lblMousePos.Show();
            Point point = myPanel.PointToClient(MousePosition);
            int mouseX = point.X / 25 + 1;
            int mouseY = (myPanel.Size.Height - point.Y) / 25 + 1;
            lblMousePos.Text = "pos (" + musX + ", " + musY + ")";
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多