简体   繁体   中英

C# How to get data from the same form open several times and in each form there are different data?

I have Form1 inside an MDI Container, and when I submit the Name and the Age, Form2 is opened inside the same container with the Name and the Age written on 2 labels, and I can do this as many times as I want.

My question is how do I get the data from the 3 Form2 and write it into a.txt file when I click on the Save Button in Form1.

应用示例

I see dumb solution in combination of Form2 fields Name and Age and storing opened Form2. Maybe like that:

// Example of Form1
public class Form1 : Form
{ 
    // In this collection we will store all Form2 which would be opened
    List<Form2> openedForms2 = new List<Form2>();
    
    private void ButtonOpenForm2_Click(object sender, EventArgs e)
    {
        string name = nicknameBox.Text;
        string age = ageBox.Text;
    
        // Open Form2 with some values and store it in a List
        Form2 form2 = new Form2(name, age);
        // Subscribe to FormClosed event to be able to remove it from List of opened forms after closing
        form2.FormClosed += (sender, args) => { openedForms2.Remove(form2); };
        form2.Show();
        openedForms2.Add(form2);
    }
    
    private void ButtonSave_Click(object sender, EventArgs e)
    {   
        StringBuilder sb = new StringBuilder(); // Reference to System.Text should be provided
        
        // Here you getting opened forms, which are stored in List
        foreach (Form2 form2 in openedForms2)
        {   
            // And you able to get values of fields Name and Age of each opened form
            string name = form2.Name;
            string age = form2.Age;
            
            // Combine them in a way you wish
            sb.AppendLine("Name: " + name + " | Age: " + age);
        }
        
        // Finally save all combined lines in StringBuilder to some file
        // Don't forget about System.IO reference
        File.WriteAllText("PathToYourFile", sb.ToString());
    }
}


// Example of Form2
public class Form2 : Form
{
    // This fields will be available for access from Form1 after Form2 opened
    private string Name = string.Empty;
    private string Age = string.Empty;
   
    public Form2(string name, string age)
    {  
       // Set fields values
       Name = name;
       Age = age;
        
       // Set labels text
       form2Name.Text = name;
       form2Age.Text = age;
    }
}

I think what you are trying to do is output a Winform 's textbox value as a.txt file?

If so, you can create an instance of TextWriter using System.IO . This will take a given string input and write it to the destination.txt file of your choice, creating the file if it does not already exist.

Mae sure to have at the top of your class:

using System.IO;

You must put your method inside a try/catch block since it might encounter a lot of errors when it writes. This article tells you about getting textbox values


       static void WriteOutput()
        {
    //A using statement uses the object once, then disposes of it's resources after the block
            using (StreamWriter writer = File.CreateText("YOURPATH/YOURFILE.txt"))
            {
                writer.WriteLine(this.textbox1.Text + "\n");
                writer.WriteLine(this.textbox2.Text);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM