3

I have 2 forms:

  1. Form1 contains DataGridView controls

  2. Form2 contains Textbox controls (are in mode read only), checkBox and Button.

When I select a DataGridView Row it will show me Form2 and display their values in TextBoxes. Everything seems better just now. What I want know is after displaying data in textboxes, I check the RadioButton then click the button it will return to Form1 of the selected Row and Change the value of Cell 4 automatically.

Here there is My code:

Form1

private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)  
   {  
       DataGridViewCell cell = null;  
       foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)  
       {  
           cell = selectedCell;  
           break;  
       }  
       if (cell != null)  
       {  
           DataGridViewRow row = cell.OwningRow;  
           string objet = row.Cells[0].Value.ToString();  
           string objectif = row.Cells[1].Value.ToString();  
           string date = row.Cells[2].Value.ToString();  
           string commentaire = row.Cells[3].Value.ToString();  
           string client = row.Cells[5].Value.ToString();  
           string commercial = row.Cells[6].Value.ToString();  

           Détails_RDV détails = new Détails_RDV();  

           détails.obj = objet;  
           détails.objectif = objectif;  
           détails.date = date;  
           détails.comm = commentaire;  
           détails.clt = client;  
           détails.commer = commercial;  

           détails.Show();  

       }  
   }  

Form2

public partial class Détails_RDV : Form  
{  
    public string obj ;  
    public string objectif;  
    public string date;  
    public string comm;  
    public string clt ;  
    public string commer;  

    public Détails_RDV()  
    {  
        InitializeComponent();  

    }  

    private void Détails_RDV_Load(object sender, EventArgs e)  
    {  

        txtObjet.Text = obj;  
        txtObjectif.Text = objectif;  
        txtDate.Text = date;  
        txtCommerci.Text = commer;  
        txtClient.Text = clt;  
        txtCommentaire.Text = comm;  
    }  

    private void btnValider_Click(object sender, EventArgs e)  
    {  
        if (checkEffectue.Checked == true)  
        {  
           //What should I write here??
             Liste_RDV lrdv = new Liste_RDV();
             lrdv.Show();

        }  
    }  

How Can I do That ?

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104

2 Answers2

1

In the button click event on Form2, check if the check box is checked, just set DialogResult to OK, else set it to Cancel:

if (checkBox1.Checked)
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
else
    this.DialogResult = System.Windows.Forms.DialogResult.Cancel;        

In Form1, instead of Show, use ShowDialog and check if the result is OK, perform the update which you need:

var f2 = new Form2();
//Set values
if(f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    //Perform update here
}

This way each form has its own responsibility and you write the codes which are related to Form1 in Form1 and you don't need to write them in Form2.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I don't see the benefit to do that . My Problem is not in **DialogResult** – Yosra Chourou Jun 24 '16 at 11:44
  • It's your decision. This you each form has its own responsibility and you write the codes which are related to `Form1` in `Form1` and you don't need to write them in `Form2`. – Reza Aghaei Jun 24 '16 at 11:48
  • I believe the current post answers your question. But if you need more information about interaction between forms, you may find this post useful: [Interaction between Forms - How to Change a Control of a Form from Another Form?](https://stackoverflow.com/questions/38768737/interaction-between-forms-how-to-change-a-control-of-a-form-from-another-form) – Reza Aghaei Sep 19 '16 at 16:22
  • Let me know if you have any question about the answer or if you find it useful :) – Reza Aghaei Sep 19 '16 at 16:22
0

You could create an event on the sub-form and fire it, as in the example below :

public partial class Détails_RDV : Form
{
    public string obj;
    public string objectif;
    public string date;
    public string comm;
    public string clt;
    public string commer;

    **// Event fired on effectue checkbox change
    public class EffectueEventArgs : EventArgs
    {
        public EffectueEventArgs(bool val)
        {
            Effectue = val;
        }
        public bool Effectue { get; private set; }
    }
    public delegate void EffectueChangeHandler(object src, EffectueEventArgs e);
    public event EffectueChangeHandler OnEffectueChange;**

    public Détails_RDV()
    {
        InitializeComponent();
    }

    private void Détails_RDV_Load(object sender, EventArgs e)
    {
        txtObjet.Text = obj;
        txtObjectif.Text = objectif;
        txtDate.Text = date;
        txtCommerci.Text = commer;
        txtClient.Text = clt;
        txtCommentaire.Text = comm;
    }

    private void btnValider_Click(object sender, EventArgs e)
    {
        if (checkEffectue.Checked == true)
        {
            **// Notify any event listener of change
            if (OnEffectueChange != null)
                OnEffectueChange (this, new EffectueEventArgs(true);**

            this.Close();
        }
    }
}

Then when you create the sub-form youc an subscribe to the event and register an appropriate handler, e.g.

        Détails_RDV détails = new Détails_RDV();
        détails.obj = objet;
        détails.objectif = objectif;
        détails.date = date;
        détails.comm = commentaire;
        détails.clt = client;
        détails.commer = commercial;
        **détails.OnEffectueChange += (src, e) => { row.Cells[4].Value = e.Effectue; };**
        détails.Show();  

Or you could leverage INotifyPropertyChanged, or indeed specify an explicit callback delegate, Action or Function.

Ben Jackson
  • 1,108
  • 6
  • 9