从form1列表框到form2文本框的数据

时间:2016-02-01 18:57:37

标签: c# winforms textbox listbox

我正在处理的程序分配的目标要求我用数据文件中的项目填充列表框,然后允许用户修改所选项目的部分。为此,我需要帮助确定如何将一个表单中所选项目的一部分传递到另一个表单的文本框中。

这是我在程序中第一个表单的编码:

public partial class Form1 : Form 
{

    const char DELIM = '\\';
    const string FILENAME = @"C:\Visual Studio 2015\Data Files\Training Workshops data";
    string recordIn;
    string[] Fields;
    static FileStream file = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(file);
    public int X;

    public Form1() 
    {

        InitializeComponent();
    }

    public class Workshop 
    {

        public string title { get; set; }
        public int days { get; set; }
        public string categrory { get; set; }
        public double cost { get; set; }

        public string[] categrorynames = 
        { 
            "Application Development", 
            "Databases", 
            "Networking", 
            "System Administration" 
        };
    }

    Workshop shop = new Workshop();       

    private void button1_Click(object sender, EventArgs e) 
    {

        Form2 secondForm = new Form2();
        secondForm.Show();
    }

    private void PopulateList(string filePath) 
    {

        while(recordIn != null) 
        {
            try 
            {
                 recordIn = reader.ReadLine();
                 Fields = recordIn.Split(DELIM);
                 X = Convert.ToInt32(Fields[0]);
                 shop.categrory = shop.categrorynames[X];
                 shop.days = Convert.ToInt32(Fields[1]);
                 shop.title = Fields[3];
                 shop.cost = Convert.ToDouble(Fields[2]);
            }     
            catch (Exception A) 
            {
                if (X < 0 && X > 3) 
                {
                    shop.categrory = "invalid";
                }
                if (shop.days != Convert.ToInt32(Fields[1])) 
                {
                    shop.days = 0;
                }
                if (shop.title != Fields[3]) 
                {
                    shop.title = "invalid";
                }
                if (shop.cost != Convert.ToDouble(Fields[2])) 
                {
                    shop.cost = 0;
                }
            }
        }
    }
}

以下是第二种形式的屏幕截图的链接:

http://i.stack.imgur.com/IRqVh.png

我需要将form1的列表框所选项目的shop.categrory数据传输到第二个表单,并将shop.title,shop.days和shop.cost传输到相应的文本框。从那里我可以做到这一点,无论用户在文本框中输入什么,都会在按下“保存并退出”按钮时更改所选项目的数据。

任何帮助都将不胜感激,如果有人注意到我现在编码中的错误,请随时指出它们。

1 个答案:

答案 0 :(得分:0)

Create a Parameterized constructor in form2 that accepts a String, and create it's instance in first form, and pass the value you want to pass to the second form:

$html = <<<SOURCE
<img style='width:198px;height:279px;'            class='featureImg'             src='image-loader.gif'            data-src='http://somesites.com/med/1455.jpg'            alt="Picture">
SOURCE;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);

$divs = $dom->getElementsByTagName('img');
foreach ($divs as $div) {
    if (preg_match_all('/\bfeatureImg\b/', $div->getAttribute('class'))) {
        echo $div->getAttribute('data-src');
    }
}

and in form2 Create a Constructor:

private void button1_Click(object sender, EventArgs e) 
{
  Form2 secondForm = new Form2("DATA TO BE SENT");
  secondForm.Show();
}