从test.cs更改form1中的picturebox图像

时间:2021-01-20 00:25:30

I am trying to update picture box image inside "form1" from another cs file my code inside test.cs

我试图从test.cs中的另一个cs文件我的代码更新“form1”中的图片框图像

slot_13.modifier = public;

and inside form1 i wrote this also

而在form1中我也写了这个

CheckForIllegalCrossThreadCalls = false;

test.cs

test.cs中

inventory_Viewer.viewer x = new inventory_Viewer.viewer();
x.slot_13.Image = Image.FromFile(@"C:\Users\Axmed\Google Drive\C# Source Codes\inventory Viewer\inventory Viewer\bin\Release\icon\icon_default.png");

But it doesn't work If i used this line inside "form1"

但它不起作用如果我在“form1”中使用此行

x.slot_13.Image = Image.FromFile(@"C:\Users\Axmed\Google Drive\C# Source Codes\inventory Viewer\inventory Viewer\bin\Release\icon\icon_default.png");

image gets changed

图像变了

1 个解决方案

#1


0  

Your code misses a lot of context, so I'm going to do a few assumptions. Given a MainForm that shows the InventoryViewerForm and also wants to change the image on the InventoryViewerForm, you could hold a reference to the second form like this:

你的代码错过了很多上下文,所以我会做一些假设。给定一个MainForm显示InventoryViewerForm并且还想更改InventoryViewerForm上的图像,您可以保存对第二个表单的引用,如下所示:

// Your inventory_Viewer.viewer
public partial class InventoryViewerForm
{
    public InventoryViewerForm()
    {
    }
}

// The form from which to show the viewer. 
public partial class MainForm
{
    private readonly InventoryViewerForm _inventoryViewerForm;

    public MainForm()
    {
        _inventoryViewerForm = new InventoryViewerForm();
    }

    private void ShowInventoryViewerButton_Click(object sender, EventArgs e)
    {
        _inventoryViewerForm.Show();
    }

    private void ChangeImageButton_Click(object sender, EventArgs e)
    {
        // Dispose the previously loaded image.
        if (_inventoryViewerForm.Image != null)
        {
            _inventoryViewerForm.Image.Dispose();
        }

        _inventoryViewerForm.Image = Image.FromFile("NewImage.png");
    }
}

But this is bad design altogether. You don't want to tightly couple your forms like this, and you want to leverage the data binding of WinForms and the events of .NET for this. In order to properly implement that, you'll need to show more code.

但这完全是糟糕的设计。您不希望像这样紧密地耦合您的表单,并且您希望利用WinForms的数据绑定和.NET的事件。为了正确实现它,您需要显示更多代码。

#1


0  

Your code misses a lot of context, so I'm going to do a few assumptions. Given a MainForm that shows the InventoryViewerForm and also wants to change the image on the InventoryViewerForm, you could hold a reference to the second form like this:

你的代码错过了很多上下文,所以我会做一些假设。给定一个MainForm显示InventoryViewerForm并且还想更改InventoryViewerForm上的图像,您可以保存对第二个表单的引用,如下所示:

// Your inventory_Viewer.viewer
public partial class InventoryViewerForm
{
    public InventoryViewerForm()
    {
    }
}

// The form from which to show the viewer. 
public partial class MainForm
{
    private readonly InventoryViewerForm _inventoryViewerForm;

    public MainForm()
    {
        _inventoryViewerForm = new InventoryViewerForm();
    }

    private void ShowInventoryViewerButton_Click(object sender, EventArgs e)
    {
        _inventoryViewerForm.Show();
    }

    private void ChangeImageButton_Click(object sender, EventArgs e)
    {
        // Dispose the previously loaded image.
        if (_inventoryViewerForm.Image != null)
        {
            _inventoryViewerForm.Image.Dispose();
        }

        _inventoryViewerForm.Image = Image.FromFile("NewImage.png");
    }
}

But this is bad design altogether. You don't want to tightly couple your forms like this, and you want to leverage the data binding of WinForms and the events of .NET for this. In order to properly implement that, you'll need to show more code.

但这完全是糟糕的设计。您不希望像这样紧密地耦合您的表单,并且您希望利用WinForms的数据绑定和.NET的事件。为了正确实现它,您需要显示更多代码。