0

I am trying to update a SQL databound combobox with new information from a new form. After I make the adds/edits and save on the popup form, I want to refresh the combobox with the new info and select the currently added/edited item. Currently, the code below refreshes the list, but will not modify "myID" on the parent form as I thought a reference variable should. How can I most efficiently do this? I have about 20 forms to do a similar type thing.

In form1

    int newid = 0;
    private void addToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        CoalSeamsForm csf = new CoalSeamsForm(ref newid);
        csf.ShowDialog();
        coalSeamsTableAdapter.Fill(well_Information2DataSet.CoalSeams);
        coalSeamsBindingSource.Find("CoalSeamID", newid);
    }

In form 2

    int myID = 0;

    public CoalSeamsForm(ref int myId)
    {
        this.myID = myId;
        InitializeComponent();
    }

    private void CoalSeamsForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!isOK)
        {
            if (DialogResult.Yes == MessageBox.Show("Would you like to save the changes?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Save();
                DataRowView drv = (DataRowView)coalSeamsBindingSource.Current;
                myID = (int)drv["CoalSeamID"];
            }
        }
    }
}
Mike
  • 245
  • 1
  • 3
  • 9

2 Answers2

1

There is a problem here. I don't even know if this code compiles. (two vars with the same name?)

int myID = 0; 
public CoalSeamsForm(ref int myID) 
{ 
    this.myID = 0; 
    InitializeComponent(); 
} 

However your problem is that you pass by ref the myID inside the form constructor, then your code exit. When you update the var myID in Form_Closing you are no more referencing the var passed inside the constructor. To resolve your problem declare a global property like

public property MyID {get; set;}

then in the constructor

public CoalSeamsForm() 
{ 
    this.MyID = 0; 
    InitializeComponent(); 
} 

update the property when the user clicks a CONFIRM button (not in the form_closing event)

   private void ConfirmButton_Click(object sender, EventArgs e)   
   {   
       ...
       this.MyID = (int)drv["CoalSeamID"];   
       this.DialogResult = DialogResult.OK;
   }   

and finally use the property value when calling

    using(CoalSeamsForm csf = new CoalSeamsForm())
    {
        if(DialogResult.OK == csf.ShowDialog())
        {
            coalSeamsTableAdapter.Fill(well_Information2DataSet.CoalSeams);    
            coalSeamsBindingSource.Find("CoalSeamID", this.MyID);  
        }
    }  
Steve
  • 213,761
  • 22
  • 232
  • 286
  • So am I going to call Form2.Show() instead of Form2.ShowDialog() and then close after accessing the public property? – Mike Apr 20 '12 at 14:14
  • I see your problem now. The cdf.ShowDialog() is there to stop the calling code and wait the user ending its work with the csf form. Usually the modal dialog will be closed setting the DialogResult property inside some methods (button_click) and not calling the form.Close() method. In this way the code returns out of ShowDialog but the form is not closed and you can read the MyID property. I suggest to change the setting the of the MyID property in a different method (a button_click to confirm). I will update the code – Steve Apr 20 '12 at 14:40
0

Reference parameters only work within the calling method/constructor. Once they are outside of that individual method, they become copies within the second object. To implement, from the suggestions of Steve and others, I used a public property and accessed it from the first form. Also, I used a databound combobox in the posted code, which is a more typical application from me vs. setting the BindingSource.Position property.

In form1

private void addEditCoalSeams(bool isAdd)
    {
        int? myId=null;
        if (!isAdd)
        {
            myId = (int?)coalSeamsComboBox.SelectedValue;
        }

        using (CoalSeamsForm csf = new CoalSeamsForm(myId, isAdd))
        {
            if (DialogResult.OK == csf.ShowDialog())
            {
                coalSeamsTableAdapter.Fill(well_Information2DataSet.CoalSeams);
                coalSeamsComboBox.SelectedValue = csf.coalID;
            }
        }
    }

In CoalSeamsForm

    public int? coalID {get; set;}
    bool isAdd = false;

    public CoalSeamsForm(int? coalId, bool isAdd)
    {
        this.coalID = coalId;
        this.isAdd = isAdd;
        InitializeComponent();
    }

    private void okButton_Click(object sender, EventArgs e)
    {
        Save();
        DataRowView drv = (DataRowView)coalSeamsBindingSource.Current;
        this.coalID = (int)drv["CoalSeamID"];
        this.DialogResult = DialogResult.OK;
    }
Mike
  • 245
  • 1
  • 3
  • 9