当你不使用MDI时,如何从隐藏形式到当前形式获取fata? [英] How to get fata from hidden form to current form when you are not using MDI?

查看:95
本文介绍了当你不使用MDI时,如何从隐藏形式到当前形式获取fata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#windows应用程序而我没有使用MDI容器,

我的问题是如何在隐藏表单后获取隐藏表单的数据,



对于Ex:

Form1:包含该值的表单,并在保存值后隐藏它

Form2:表格当前已激活并需要Form1的数据



如何在这种情况下获取或传递数据,任何想法?



Form2 [checknonstd]

i am working on c# windows application and i am not using MDI Container,
my question is how to get the data of hidden form once we hide the form,

For Ex:
Form1 : Form that consists that the value and it is hidden after saving values
Form2 : Form Which is currently activated and needed the data of Form1

How to get or pass data in this situation,any idea?

Form2[checknonstd]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pms
{
    public partial class checknonstd : Form
    {
       //To Get boxes From Form1
        public string Boxes
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }
        // To Get standard KGs From Form1
        public string Kgs
        {
            get { return textBox2.Text; }
            set { textBox2.Text = value; }
        }
        // To Get standard pcs From Form1
        public string Pcs
        {
            get { return textBox3.Text; }
            set { textBox3.Text = value; }
        }
        
        
        public class nonstd
        {
            private IList<int> nboxes;
            private IList<float> nkgs;
            private IList<float> nPcs;
            private IList<string> nremarks;
            public IList<int> BoxesList { get { return nboxes; } set { nboxes = value; } }
            public IList<float> KgsList { get { return nkgs; } set { nkgs = value; } }
            public IList<float> PcsList { get { return nPcs; } set { nPcs = value; } }
            public IList<string> RemarkList { get { return nremarks; } set { nremarks = value; } }

        }

        public checknonstd()
        {
            InitializeComponent();
            dataGridView1.Columns.Add("Boxes", "Boxes");
            dataGridView1.Columns.Add("Kgs","Kgs");
            dataGridView1.Columns.Add("Pcs","Pcs");
            dataGridView1.Columns.Add("Remark", "Remark");

          
        }
 
        //to write datagridview first column 1 as per textbox53 data
        private void checknonstd_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
                int a = Int32.Parse(textBox1.Text);
                for (int i = 0; i < a; i++)
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[i].Cells[0].Value = "1";
                }
        }

       
        //button to close form
        private void btnclose_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1;i++ )
            {
                nonstd s=new nonstd();
                s.BoxesList.Add(Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()));
                s.KgsList.Add(float.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString()));
                s.PcsList.Add(float.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()));
                s.RemarkList.Add(dataGridView1.Rows[i].Cells[2].Value.ToString());
            }
                MessageBox.Show("1" + textBox1.Text + " 2" + textBox2.Text + " 3" + textBox3.Text);
            this.Hide();
        }

       
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex==1)
            {
                var sw = float.Parse(textBox2.Text);
                var sp = float.Parse(textBox3.Text);
                var cvalue=float.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

                var fpcs = (cvalue * sp) / sw;
                dataGridView1.Rows[e.RowIndex].Cells[2].Value = fpcs;
            }
        }
    }
}

<big>Form 1 code To Get Form2 nonstd class data</big>
<pre>private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                checknonstd.nonstd ss=new checknonstd.nonstd();
                List<int> list= ss.BoxesList.ToList<int>();
                for(int i=0;i<list.Count;i++)
                {
                    MessageBox.Show(list[i].ToString());
                }
            
            }
            catch(Exception eli)
            {
                MessageBox.Show(eli.Message);
            }
        }





我的尝试:



i试过这个,但我不认为这是有效的解决方案


来自Form2 textbox1的






What I have tried:

i have tried this, but i don't think it is valid solution

from Form2 textbox1

form2 f=new form2();
f.textBox1.Text





i拥有datagridview的属性但没有工作



i have trried property of datagridview but not working

推荐答案

这不起作用 - 您正在创建表单的新实例,因此您无法获取隐藏表单上的数据,只需将手机放入手套即可我的车的箱子,您可能会在以后的汽车的手套箱中找到它!



您需要使用您隐藏的表格实例。究竟如何取决于两种形式之间的关系。

看看这些,其中一个适合

创建另一个实例的形式: br />
That won't work - you are creating a new instance of the form, so you don't get the data which is on the form you hid, any more than if you put your mobile in the glove box of my car, you would expect to find it in the glove box of your car later!

You need to use the instance of the form you hid. Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit
The form that creates an instance of another:
MyForm mf = new MyForm();
mf.Show();

是父,另一种形式是孩子。

(这并不意味着任何正式的MDI关系)



转让两种表格之间的信息,第1部分:父母与子女 [ ^ ]

在两种表格之间传递信息,第2部分:儿童到父母 [ ^ ]

在两种形式之间传递信息,第3部分:儿童到儿童 [ ^ ]



Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

引用:

如果我关闭表单,那么当它嵌入时如何访问该数据



关闭表单不会杀死它或它包含的数据 - 直到表单被处理时才会发生。

想一想:打开文件时你使用模态的对话框显示:


Closing the form doesn't "kill" it or the data it contains - that doesn;t happen until the form is Disposed.
Think about it: when you open a file dialog you use a modal Show:

OpenFileDialog ofd = new OpenFileDialog()
if (ofd.ShowDialog() == DialogResult.OK)
   {
   string path = ofd.FileName;
   ...
   }

因为ShowDialog在关闭对话框之前没有返回,如果对话框中的数据不可用,你将永远无法获得文件根本就没有!



它与你的表单完全相同:关闭表单对它的数据没有任何作用 - 它只是释放所有句柄和其他资源所以它们可以被回收(它们是Windows中的稀缺资源,占用它们是一个坏主意)。

因此相同的代码适用于您的表单:

Because ShowDialog does not return until the dialog is closed, if the data in the dialog was unavailable you'd never be able to get the path to the file at all!

It's exactly the same with your forms: closing the form doesn't do anything to it's data - it just releases all the handles and other resources so they can be recycled (they are scarce resources in Windows, and "hogging them" is a bad idea).
So the same code works with your form:

MyForm mf = new MyForm()
if (mf.ShowDialog() == DialogResult.OK)
   {
   MyClass data = mf.TheDataINeed;
   ...
   }


这篇关于当你不使用MDI时,如何从隐藏形式到当前形式获取fata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆