如何将数据从一种形式发送到另一种形式 [英] how to send data from one form to another

查看:112
本文介绍了如何将数据从一种形式发送到另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 AllsortingTechs
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load( object sender,EventArgs e)
{

}

private void btnsortitems_Click( object sender,EventArgs e)
{

String [] items = txtBox1.Text.Split(' );
ArrayList sitems = new ArrayList();
ArrayList ritems = new ArrayList();
foreach 字符串输入 项目)
sitems.Add(输入);

if (rbnGeneral.Checked)
{
ritems = Generalsort(sitems);


}

if (rbnBubble.Checked)
ritems = Bubblesort(sitems );

if (rbnStraightInsertion.Checked)
ritems = Insertionsort(sitems);


if (rbnStraightExchange.Checked)
ritems = StraightExchange(sitems);



}
private ArrayList Generalsort(ArrayList sitems)
{
lstbox1.Items.Clear();
int n = sitems.Count;
对象 temp;
lstbox1.Items.Add( generalsort);

for int i = 0 ; i < n; i ++)
{
for int j = 1 + i; j < n; j ++)
{
if (Convert.ToInt32(sitems [i])> Convert.ToInt32(sitems [j])) // 此处获得异常
{
temp = sitems [一世];
sitems [i] = sitems [j];
sitems [j] = temp;
}

}

lstbox1.Items.Add(sitems [i]);
}


return sitems;


}



private ArrayList Bubblesort(ArrayList sitems)
{
lstbox1.Items.Clear();
int n = sitems.Count;
对象 temp;
lstbox1.Items.Add( bubblesort);

for int i = 0 ; i < n; i ++)
{
for int j = 0 ; j < n - i - 1 ; j ++)
{
if ( Convert.ToInt32(sitems [j])> Convert.ToInt32(sitems [j + 1 ]) ) // 此处获得异常
{
temp = sitems [j];
sitems [j] = sitems [j + 1 ];
sitems [j + 1 ] = temp;
}

}

}
for int i = 0 ; i < n; i ++)
{

lstbox1.Items.Add(sitems [i]);
}

return sitems;


}
public ArrayList StraightExchange(ArrayList sitems)
{
lstbox1.Items 。明确();
lstbox1.Items.Add( exchangeort);
int n = sitems.Count;
for int i = 0 ; i < n; i ++)
{
object small = sitems [I];
int pos = i;
for int j = i + 1 ; j < n; j ++)
{

if (Convert.ToInt32(sitems [j])< Convert.ToInt32(small))
{
small = sitems [ D];
pos = j;
}
}
sitems [pos] = sitems [i];
sitems [i] = small;

lstbox1.Items.Add(sitems [i]);
}
return sitems;
}

public ArrayList Insertionsort(ArrayList sinputs)
{
lstbox1.Items.Clear() ;
lstbox1.Items.Add( insertionsort);
int n = sinputs.Count;
for int i = 0 ; i < n; i ++)
{
object small = sinputs [I];
int pos = i;
for int j = i + 1 ; j < n; j ++)
{
if (Convert.ToInt32(sinputs [j])< Convert.ToInt32(small))
{
small = sinputs [j];
pos = j;
}
}
for int k = pos; k > i; k--)
{
sinputs [k] = sinputs [k - 1 ];
}
sinputs [i] =小;

lstbox1.Items.Add(sinputs [i]);
}
return sinputs;
}

private void txtBox1_TextChanged( object sender,EventArgs e)
{

}
}

}





查询:谢谢,在我的应用程序中我设计了一个表单并做了一些排序方法,在选择了相同形式的单选按钮输出显示之后,ihave to将输出发送到另一个表单

解决方案

将控件的属性设置为public

例如:如果你想要将数据传递到另一个表单的Textbox然后将文本框属性设置为

Modifiers从公共窗口中显示

然后

 Form2 frm =  new  Form2(); 
frm.Show();
frm.textBox1.Text = textBox1.Text;






看看这些提示:

转移两种表格之间的信息,第1部分:父母对儿童 [ ^ ]

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

在两种表格之间转移信息,第3部分:孩子对孩子 [ ^ ]


1。在Form1中公开公共属性,然后在Form2中访问。这是紧密耦合的。

2.实现发布者和子网站设计模式以使其松散耦合。


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

namespace AllsortingTechs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnsortitems_Click(object sender, EventArgs e)
        {
            
            String[] items = txtBox1.Text.Split(',');
            ArrayList sitems = new ArrayList();
            ArrayList ritems = new ArrayList();
            foreach(string input in items)
            sitems.Add(input);

            if (rbnGeneral.Checked)
            {
                ritems = Generalsort(sitems);
               

            }

            if (rbnBubble.Checked)
                ritems = Bubblesort(sitems);

            if (rbnStraightInsertion.Checked)
                ritems = Insertionsort(sitems);


            if (rbnStraightExchange.Checked)
                ritems = StraightExchange(sitems);

            

             }
        private ArrayList Generalsort(ArrayList sitems)
        {
            lstbox1.Items.Clear();
            int n = sitems.Count;
            Object temp;
           lstbox1.Items.Add("generalsort");

            for (int i = 0; i < n; i++)
            {
                for (int j = 1+i; j < n; j++)
                {
                    if (Convert.ToInt32(sitems[i]) > Convert.ToInt32(sitems[j]))// here getting exception
                    {
                        temp = sitems[i];
                        sitems[i] = sitems[j ];
                        sitems[j ] = temp;
                    }

                }
                
                lstbox1.Items.Add(sitems[i]);
            }
            

            return sitems;


        }
      


        private ArrayList Bubblesort(ArrayList sitems)  
        {
            lstbox1.Items.Clear();
            int n = sitems.Count;
            Object temp;
            lstbox1.Items.Add("bubblesort");

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n - i - 1; j++)
                {
                    if (Convert.ToInt32(sitems[j]) > Convert.ToInt32(sitems[j + 1]))// here getting exception
                    {
                        temp = sitems[j];
                        sitems[j] = sitems[j + 1];
                        sitems[j + 1] = temp;
                    }

                }

            }
                for (int i = 0; i < n; i++)
                {

                   lstbox1.Items.Add(sitems[i]);
                }
                               
               return sitems; 
                

            }
        public ArrayList StraightExchange(ArrayList sitems)
        {
            lstbox1.Items.Clear();
            lstbox1.Items.Add("exchangesort");
            int n = sitems.Count;
            for (int i = 0; i < n; i++)
            {
                object small = sitems[i];
                int pos = i;
                for (int j = i + 1; j < n; j++)
                {

                    if (Convert.ToInt32(sitems[j]) < Convert.ToInt32(small))
                    {
                        small = sitems[j];
                        pos = j;
                    }
                }
                sitems[pos] =sitems[i];
                sitems[i] = small;

               lstbox1.Items.Add(sitems[i]);
            }
            return sitems;
        }

        public ArrayList Insertionsort(ArrayList sinputs)
        {
            lstbox1.Items.Clear();
            lstbox1.Items.Add("insertionsort");
            int n = sinputs.Count;
            for (int i = 0; i < n; i++)
            {
                object small = sinputs[i];
                int pos = i;
                for (int j = i + 1; j < n; j++)
                {
                    if (Convert.ToInt32(sinputs[j]) < Convert.ToInt32(small))
                    {
                        small = sinputs[j];
                        pos = j;
                    }
                }
                for (int k = pos; k > i; k--)
                {
                    sinputs[k] = sinputs[k - 1];
                }
                sinputs[i] = small;

                lstbox1.Items.Add(sinputs[i]);
            }
            return sinputs;
        }

        private void txtBox1_TextChanged(object sender, EventArgs e)
        {

        }
        }

       }



Query:thanks, in my application i design a form and do some sorting methods and after selecting the radio button proper output display in same form, ihave to send the output to another form

解决方案

set the property of the control to public
for eg: If you want to pass the data to Textbox of another form then set Textbox Property
"Modifiers" to "Public" from the propery window
then

Form2 frm = new Form2();
            frm.Show();
            frm.textBox1.Text = textBox1.Text;


Hi,

Have a look at these Tips:
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[^]


1. To expose a public property in Form1 and then access in Form2. This gets tightly coupled.
2. To implement publisher & subsciber design pattern to keep it loosly coupled.


这篇关于如何将数据从一种形式发送到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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