0

很难提供代码来显示正在尝试做的事情,但这是失败的:

我在 Form1 上有一个按钮,上面写着“锁定”,并且在 Form2 中有一个语句,它在按下“锁定”之前从 Form1 读取数据(就像加载所有表单时一样)。如果按下“锁定”,我希望 Form1 读取数据,而不是在它之前。

有没有办法做到这一点?

如果按下它,这就是我想要运行的:

private void FOne_Reader_Tick(object sender, EventArgs e)
    {
        //Reads fruit information

        //if lock on is pressed, do this:
        FOne_Reader.Enabled = true;
        Fruit fruity = new Fruit();
        string FileToRead = "../../../DLL/XML/Fruit_Event.xml";
        XmlSerializer serializer = new XmlSerializer(typeof(Fruit));
        fruity = (Fruit)XMLHandler.READXML(FileToRead, serializer);

        //if not
      ...
4

1 回答 1

0

尝试使用事件:

表格1代码:

public partial class Form1 : Form
{
    public event Action PressLock;
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        Form2 dlg=new Form2();
        dlg.OtherFrom=this;            
        dlg.Show();
    }

    private void lockOnButton_Click(object sender, EventArgs e)
    {
        if(this.PressLock!=null)
        {
            this.PressLock();
        }
    }
}

表格2代码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    Form1 form;
    public Form1 OtherFrom
    {
        get { return form; }
        set
        {
            this.form=value;
            this.form.PressLock += new Action(ReadFromForm1);
        }
    }

    public void ReadFromForm1()
    {
        // xml reading here
    }
}
于 2013-04-19T18:38:53.820 回答