-1

In Form1 I have one DataGridView and multiple textboxes. When I click A button in Form2 I need to save the data from DataGridView and multiple textboxes to Database. How to Implement in C sharp Windows Application

Form1 Button Click event. I opened Form2

 public sealed partial class form1 : Form
 {
   private static form1 instance = null;
    public static form1 Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new form1();
            }
            return instance;
        }
    }

  private void button1_Click(object sender, EventArgs e)
    {
        textbox2.Text=100;
        form2 CO = new form2();
        CO.Show();
    }
}

I want to attach textboxes data and Datagridview content to object SO and Call InsertSale function .textboxes and datagridview are in form1

This is Button Click Event in Form 2

  private void button1_Click(object sender, EventArgs e)
  {
      clsSale SO = new clsSale();
      SO.Totamount = Convert.ToDecimal(form1.Instance.textBox2.Text);

      SO.InserSale(SO);
   }
Vimal
  • 20
  • 1
  • 5
  • You need to share the existing code which is opening Form1 and Form2 and how they are related to each other... Did you try any approach to solve this issue? What problem you are facing in that? – Chetan Feb 26 '20 at 05:06
  • I can not access Textbox and Datagridview content in form2 .How can i access it ? – Vimal Feb 26 '20 at 05:23
  • https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form – Chetan Feb 26 '20 at 05:26
  • SO.Totamount = Convert.ToDecimal(form1.Instance.textBox2.Text); i can not access textbox2(which is form1) in Form2 .it shows " ".I used Singleton Design Pattern. – Vimal Feb 26 '20 at 05:37
  • How are you showing the Form1 in first place? by doing `Applicaiton.Run(new Form1())` ? – Chetan Feb 26 '20 at 06:44
  • NO ,it opened in the same way from MainFrom like form1 CF = new form1(); CF.Show(); ,Then i opened form2 from form1 as shown in Question – Vimal Feb 26 '20 at 11:20
  • @Vimal : If you call cf = new Form1(); It’s not singleton. If you want a single instance, call Cf=Form1.Instance. However I think my answer is a better approach. – CharithJ Feb 26 '20 at 13:38
  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.parentform?view=windowsdesktop-6.0 This article is best to implement. – Hammad Sajid Aug 18 '22 at 15:22

2 Answers2

2

If Form2 wants to access the Form1 properties.

Pass ParentForm instance to the ChildForm constructor. Add a public method in the parent form to update its properties from child form.

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

    public void SetTextBoxValue(string val)
    {
        this.textBox1.Text = val;
    }

    private void CreateForm2()
    {
         var form2 = new Form2(this);
         form2.Show();
    }
}

public partial class Form2: Form
{
    private Form1 form1;

    public Form2(Form1 frm1)
    {
        InitializeComponent();

        form1= frm1;
        form1.SetTextBoxValue("Value from Form2");
    }   
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131
-1
  1. Create a global class, such as Global.cs, in the project. Then declare the following variables:

    public static Form1 frm1
    public static Form2 frm2
    
  2. Declare a variable of the Form class - Form frm1 or Form frm2 etc.

  3. Now access the variables from any form as follows:

    Global.frm1 = new Form1() // - for the Home Form1
    Global.frm1.ShowDialog();
    Global.frm2 = new Form2() // - for the Home Form1
    Global.frm2.ShowDialog();
    
  4. If you want to access a control in any form, just extend them as follows:

    frm1.txtBox1.Text
    frm2.button1.Click() // etc.
    
double-beep
  • 5,031
  • 17
  • 33
  • 41