【问题标题】:How can I pass a value from child form to parent form and back? [closed]如何将值从子表单传递到父表单并返回? [关闭]
【发布时间】:2011-12-27 14:05:11
【问题描述】:

我有一个这样的父表单 first.cs

public partial class first : Form
{
    public Graph graphi { get; }
    Graph g = new Graph();
    string s1 = null;

    private void button1_Click(object sender, EventArgs e)
    {
        Stream myStream = null;

        var parser = new Notation3Parser();
        var graph = new Graph();
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "RDF files (*.n3)|*.n3";
        openFileDialog1.FilterIndex = 1;
        openFileDialog1.RestoreDirectory = true;
        openFileDialog1.Multiselect = false;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        string s = openFileDialog1.FileName.ToString();

                        string w = Directory.GetCurrentDirectory().ToString();

                        string Fname = openFileDialog1.SafeFileName.ToString();
                        File.Copy(s, Path.Combine(w, Fname), true);

                        // Insert code to read the stream here.
                        Win32.AllocConsole();
                        s1 = Path.Combine(w, Fname);

                        showPath.Text = s1;
                        String parentvalueadress = this.s1;
                        showPath.Visible = true;
                        insertNodeButton.Visible = true;
                        delBut.Visible = true;


                        showNodes showNodes1 = new showNodes(s1);
                        g = showNodes1.returngraph();



                        Console.Read();
                        Win32.FreeConsole();


                        this.Show();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

    private void insertNodeButton_Click(object sender, EventArgs e)
    {
        addTriple a1 = new addTriple();
        a1.BringToFront();
        a1.ShowDialog();
        g.SaveToFile(this.s1);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        delete a1 = new delete();
        a1.BringToFront();
        a1.ShowDialog();
    }
}

在 insertNodeButton_Click 方法中,我想将图形 g 的值传递给它的子表单,代码如下:

public partial class addTriple : Form
{
    Graph gr;
    String childvalueadress;

    private void addTriple_Load(object sender, EventArgs e)
    {
        var parser = new Notation3Parser();
        var graph = new Graph();    
        gr = graph;
        parser.Load(graph, childvalueadress);    
    }

    private void subComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (subComboBox.SelectedItem.ToString() == "URI")
        {
            subUriTB.Visible = true;
            label1.Visible = true;
        }
        else
        {
            subUriTB.Visible = false;
            label1.Visible = false;
        }
    }

    private void objComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((objComboBox.SelectedItem.ToString() == "URI") || (objComboBox.SelectedItem.ToString() == "Literal"))
        {
            objUriTB.Visible = true;
            label1.Visible = true;
        }
        else
        {
            objUriTB.Visible = false;
            label1.Visible = false;
        }
    }

    private void addTripleButton_Click(object sender, EventArgs e)
    {
        if (subComboBox.Text.ToString() == "select" || objComboBox.Text.ToString() == "select")
            MessageBox.Show("please select node types");

        else if ((subComboBox.SelectedItem.ToString() == "URI") && (subUriTB.Text.ToString() == ""))
            MessageBox.Show("please fill text box for URI");
        else if ((preUriTB.Text.ToString() == ""))
            MessageBox.Show("please fill text box for URI");
        else if ((objUriTB.Text.ToString() == "") && ((objComboBox.SelectedItem.ToString() == "URI") || (objComboBox.SelectedItem.ToString() == "Literal")))
            MessageBox.Show("please fill text box for object name");
        else if ((objComboBox.SelectedItem.ToString() == "URI") && (objUriTB.Text.ToString() == ""))
            MessageBox.Show("please fill text box for URI");
        try
        {
            if ((subComboBox.Text.ToString() == "URI") && (objComboBox.Text.ToString() == "URI"))
                addUUU(subUriTB.Text.ToString(), preUriTB.Text.ToString(), objUriTB.Text.ToString(), gr);
            else if ((subComboBox.Text.ToString() == "URI") && (objComboBox.Text.ToString() == "Literal"))
                addUUL(subUriTB.Text.ToString(), preUriTB.Text.ToString(), objUriTB.Text.ToString(), gr);
            else if ((subComboBox.Text.ToString() == "Blank") && (objComboBox.Text.ToString() == "Literal"))
                addBUL(preUriTB.Text.ToString(), objUriTB.Text.ToString(), gr);
            else if ((subComboBox.Text.ToString() == "URI") && (objComboBox.Text.ToString() == "Blank"))
                addUUB(subUriTB.Text.ToString(), preUriTB.Text.ToString(), gr);
        }
        catch
        {
            MessageBox.Show("please correct the uri");
        }
        gr.SaveToFile("c:\\n.n3");
    }
}

并在此子表单中使用此值,然后将其传递给父表单 first.cs

我该怎么做?

【问题讨论】:

  • 为什么你认为这种(肮脏的)格式在公共场所的任何地方都是可以接受的?!
  • 在 SO 上至少有 ∞ 个关于此主题的问题。提问前请先搜索。
  • 如果您有任何更改或要添加的任何内容,请更新此问题,请勿提出新问题。谢谢。
  • 阅读此stackoverflow.com/faq#howtoask 然后提问

标签: c# .net winforms


【解决方案1】:

为子窗体定义一个Property

// In Parent Form
addTriple a1 = new addTriple();

a1.G = graphicdata //assign graphic data here
a1.BringToFront();
a1.ShowDialog();
g.SaveToFile(this.s1);


// In Child Form
public Graph  G
{
    get { return gr; }
    set { gr = value; }
}

现在您可以随时访问它并在显示表单之前为其分配数据

【讨论】:

  • 你的意思是我用子表单写这个,我可以在我的父表单中访问它?
  • 用什么语法我可以在子窗体中访问它?
  • addTriple a1 = new addTriple(); a1.G= 图形数据
  • 我编辑了,现在更清晰了
  • 年龄 inkaro bokoni nabayad moshkeli bashe baradar ya khahar jan!
【解决方案2】:

对此有两种可能的方法。一种是在子表单中保存对父表单的引用。或者另一种是给子窗体添加一个属性。

参考

在 addTriple 类中添加一个成员变量,它的类型为 first

例如

first _parent;

将 addTriple 表单上的构造函数更改为以下内容

    public addTriple(first p, Graph g)
    {
        _parent = p;
        this.Graph = g; //now you have a reference to the graph from the original form.
    }

显示新表单时使用以下代码:

addTriple a1 = new addTriple(this, g); //g being the graph you wish to use in the modal form.
a1.BringToFront();
a1.ShowDialog();

属性

向 addTriple 类添加一个属性

public Graph Graph {get;set;}

然后显示表单使用以下

addTriple a1 = new addTriple(); //create a new instance of the form
a1.Graph = this.Graph; //set the graph to the one from the parent form
a1.ShowDialog(); //show the form as a dialog
this.Graph = a1.Graph; // once the form is closed get the changed graph back to the parent.

【讨论】:

  • 我应该如何将数据从孩子传递给父母?
  • 查看修改后的答案,了解如何实现这一目标
  • 好的,我这样做了,但它从这一行得到错误 this.Parent = parent;我现在该怎么办
  • 我已经修改了代码,因为该错误作为表单具有 Parent 属性。尝试将您的代码修改为上述内容。它应该修复它。
  • 这不是OO方式,DeveloperX有正确答案。
【解决方案3】:

//// 为 addTriple 形式创建一个这样的构造函数,并将 Graph 对象作为引用传递

  public addTriple(ref Graph g)
        {
            InitializeComponent();
//can access g here ......

        }

然后在主窗体中这样调用它

    private void insertNodeButton_Click(object sender, EventArgs e)
            {

                addTriple a1 = new addTriple(ref g);
                a1.BringToFront();
                a1.ShowDialog();
               //New g will be available here
                g.SaveToFile(this.s1);

            }

不知道这是否是最好的方法

【讨论】:

  • 好的,我该怎么做,反之亦然,将值从孩子传递给父母?
  • @ghasedak 编辑了我的答案,请查看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-12
相关资源
最近更新 更多