getting values from previous forms in c# winforms

how to pass textbox value from one form to another form in c#
how to redirect from one form to another in c# windows application
how to pass datatable from one form to another in c# windows application
how to link two forms in c#
how to run second form in c# windows application
get value from another form c#
c# how to return a value from a form
how to pass list from one form to another in c#

i have 2 forms that consist of around 20 TextBoxes and i want to save all 20 entries and show them all in the 3rd form, each being shown in different labels, i have saved all 20 data in individual public static stringglobal variables, but having 20 static global variable takes a lot of memory and slows down the program, is there any other way to save these data and store them individually in labels ?

here is what i have tried:

first form:

public static string place_of_application;
public static string gender;

private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
    place_of_application = PlaceOfApplication.Text;
    gender = identity.Text;
    ...
}

second form:

private void PrintTemplateForm_Load(object sender, EventArgs e)
{
    label36.Text = userform2.place_of_birth;
    label34.Text = userform2.gender;
    ...    
}

thanks for the help


Let's say you have two forms, the main form and the secondary form. You have bunch of text boxes in main, and upon clicking a button you want to grab data from those and show them in form 2.

First I would define a class that represents data you want to send/receive. It seems to be some sort of personal data, so let's create a Person class:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Obviously, you will have all your 20 things you want as properties in the class instead of just this two.

Now the idea is grab data from your text boxes and save in an instance of this object. Next, In the second form you want to launch, let's pass a Person object in the constructor. So the second form's code behind will look like this. We will save that Person information in form two, and then display on it's labels.

public partial class SecondForm : Form
{
    public Person Person { get; set; }

    public SecondForm(Person person)
    {
        InitializeComponent();
        Person = person;
    }

    private void SecondForm_Load(object sender, EventArgs e)
    {
        lblName.Text = Person.Name;
        lblAge.Text = Person.Age.ToString();
    }
}

Now in main form, when a button is clicked, we'll save the data to an instance of the Person class, then pass it into the constructor of SecondForm and then show it.

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

    private void button1_Click(object sender, EventArgs e)
    {
        var person = new Person();
        person.Name = txtName.Text;
        if (int.TryParse(txtAge.Text, out int age))
        {
            person.Age = age;
        }

        var form2 = new SecondForm(person);
        form2.Show();
    }
}

Getting a value from Another Form using C#, Declare your Form1's txtbox1 Modifier as public . And in Form2 Declare the following: System.Windows.Forms.Form f = System.Windows.Forms  In this tutorial we will learn how to pass data from one form to another form in Windows Forms applications using C#. Let's use the following procedure to create a Windows Forms application. Step 1


One approach could be to select all of your textboxes in your first form and in Properties windows of Visual Studio (i'm guessing you use this IDE) go to Modifiers property and select Public from the list,

Or you can directly edit Form1.Designer.cs file and change all of the textboxes you want from Private to Public, then you can access them directly from your 2nd form like this:

Form1 frm = new Form1();
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
    label1.Text = frm.textBox1.Text;

C# - Get value from another Form, then select C# Windows Forms Application then click Ok. window form application So I use three global variables. Write the following code in  You can do a search for "passing between forms winforms" and you'll get quite a few hits. Here are a few good ones. I haven't read them all through end-to-end, but they're thorough and will give you something to think about, at least. Passing Data Between 3 Separate WinForms; Passing Values Between C# Winforms Working Intermittently


I think you have the XY problem. You are asking for a very specific problem, when your issue is far more general with the architecture of the application.


Recommendation

Try to take advantage of the object oriented features of C# and build a data model (i.e. a class that contains all the data). Then you can pass around this class (or actually a reference to the class) from one form to the other. Each form will be responsible for populating their own text boxes from the data model, or generating a new instance of the data model from the text boxes.

As an added bonus, to this setup is that you can use data binding to automate the synchronization between your data model and the UI (i.e. the text boxes).

I strongly recommend reading a book on windows programming, or find online resources with examples. [SO] is not a good place to learn how to build windows applications properly.


Example

  1. Data model is a class that holds data

    public class PersonalInformation
    {
        public string PlaceOfApplication { get; set; }
        public string Gender { get; set; }
        // .. more here
    }
    
  2. Main form can receive or send data via a PersonalInformation object.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        public PersonalInformation PersonalInformation
        {
            get
            {
                return new PersonalInformation()
                {
                    PlaceOfApplication = placeOfApplicationTextBox.Text,
                    Gender = genderTextBox.Text
                };
            }
            set
            {
                placeOfApplicationTextBox.Text = value.PlaceOfApplication;
                genderTextBox.Text = value.Gender;
            }
        }
    }
    
  3. In the second form create a method to receive data

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    
        public void SetPersonalInformation(PersonalInformation value)
        {
            placeOfApplictionLabel.Text = value.PlaceOfApplication;
            genderLabel.Text = value.Gender;
        }
    }
    
  4. In the first form handle the button press by preparing and sending the information to the second form

    public partial class Form1 : Form
    {
        ...
    
        private void secondFormButton_Click(object sender, EventArgs e)
        {
            var dlg = new Form2();
            dlg.SetPersonalInformation(this.PersonalInformation);
    
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                // user pressed OK in second form
            }
        }
    }
    
  5. Test the process

How to Pass Data One Form to Another in Windows Form Application, I don't know) to pass data between forms in Windows application. In this article, let me Add a property in form1 to retrieve value from textbox : Hide Copy Code. Now in second row if i select "offer on previous item" I need to fill this row corresponding to previous row. I need to get price amount and if offer is 5% i perform some calculation and show this result in Total. so I need to get the previous row cell value.


Passing Data Between Forms, Getting and Setting values on multiple VB NET user forms. lesson follows on from the previous two lessons: How to create a second form Windows.Forms. Add a property in form1 to retrieve value from textbox: public string _textBox1 { get {return textBox1.Text;} } Step 2. Add a property in form2 to set the labels’ text: public string _textBox { set {label1.Text= value;} } Step 3. In form1’s button click event handler, add the following code:


Visual Basic .NET: Getting at Values on other Forms, Windows.Forms; public class Form1 : Form { private TextBox textBox1; public With the TextBox control, the user can enter text in an application. Gets a value indicating whether the user can undo the previous operation in a text box control. A windows form application is any application, which is designed to run on a computer. it becomes a web application. Visual Studio and C# are used to create either Windows Forms or Web-based applications. we use following controls Group Box, Label, Textbox, Listbox, RadioButton, Checkbox, Button


TextBox Class (System.Windows.Forms), Gets or sets a value that indicates that the text box control has been modified by the Windows.Forms.dll Browsable(false)] public bool Modified { get; set; } code to indicate that changes were made to the text box control by the application​. Previous Version Docs · Blog · Contribute · Privacy & Cookies · Terms of Use  Here Mudassar Ahmed Khan has explained with an example, how to get Selected Text and Selected Value of ComboBox control in Windows Forms (WinForms) Application using C# and VB.Net. The Selected Text and Selected Value of ComboBox control will be fetched in Button Click event in Windows Forms (WinForms) Application using C# and VB.Net.