I have two forms . In the form1 I have a DGV with the data from the XY table , irrelevant. When I want to change the information about the current record or add a new record I want to make it in the new input / edit form. I tried to transfer data into form and call them through the structure or class that instantiates the form to insert into the table . How to do this elegant ? This second form shoud bee like "black box" and use for input/edit data from form1, form 4, form5..... something like that... problem for me is when data is input into textbox and validate, how to return 5 textbox value back?
Loading

Wim SturkenboomPosted Oct 23, 2014, 1:41 AM
Create a class to hold the parameters that you want to pass; adjust to your needs.
public class myDataClass
{
public string txt1 = "";
public string txt2 = "";
}
In the editor form, add a variable to hold the data and add a second constructor that will take data. Changes highlighted
public partial class Form2 : Form
{
myDataClass data;
public Form2()
{
InitializeComponent();
}
public Form2(myDataClass theData)
{
InitializeComponent();
data = theData;
}
...
...
}
Also add a form_load event handler to the editor form that will populate the controls of the editor form
private void Form2_Load(object sender, EventArgs e)
{
if (data != null)
{
textBox2.Text = data.txt1;
}
}
Lastly add a method to the editor form to retrieve the modified data
public myDataClass getData()
{
data = new myDataClass();
data.txt1 = textBox2.Text;
return data;
}
In the form where you want to call the editor form to edit an existing record
private void buttonEdit_Click(object sender, EventArgs e)
{
// store data for transfer
myDataClass theData = new myDataClass();
theData.txt1 = textBox1.Text;
// create form2
Form2 frm2 = new Form2(theData);
// and show it
frm2.ShowDialog();
// get the modified data
theData = frm2.getData();
// do something with it
// get rid of form2
frm2.Dispose();
}
In the form where you want to call the editor form to create a new record
private void buttonNew_Click(object sender, EventArgs e)
{
// create form2
Form2 frm2 = new Form2();
// and show it
frm2.ShowDialog();
// get the data
myDataClass theData = frm2.getData();
// do something with it
// get rid of form2
frm2.Dispose();
}
I hope that this shows the basics of what your looking for. Adjust to your needs.
GoGi FeboPosted Oct 23, 2014, 5:58 AM
private void btnDodaj_Click(object sender, EventArgs e) // Add
{
frmKoordinatoriUI frmVanjski = new frmKoordinatoriUI("", ""); // send nothing...
frmVanjski.Name = "Unos"; // Name of form, Volpes give me idea... important
frmVanjski.ShowDialog();
PopuniGrid();
}
private void btnIzmjeni_Click(object sender, EventArgs e) // Edit
{
var red = this.dgvKoordinatori.CurrentCell.RowIndex;
frmKoordinatoriUI frmVanjski = new frmKoordinatoriUI(dgvKoordinatori.Rows[red].Cells[0].Value.ToString(), dgvKoordinatori.Rows[red].Cells[1].Value.ToString());
frmVanjski.Name = "Izmjena"; // Name of form, Volpes give me idea... important
frmVanjski.ShowDialog();
PopuniGrid();
}
and in frmKoordinatoriUI..... // add/edit form...
public partial class frmInvestitoriUI : Form
{
Investitor ulagac = new Investitor(); class like your suggest
/*
* s0 - id
* s1 - naziv investitora
* s2 - adresa
* s3 - OIB
* k1-k5 - kontakti
*/
public frmInvestitoriUI(string s0, string s1, string s2, string s3, string k1, string k2, string k3, string k4, string k5)
{
InitializeComponent();
// "preuzimanje" podataka...
ulagac.id = s0;
ulagac.naziv = s1;
ulagac.adresa = s2;
ulagac.oib = s3;
ulagac.kontakt1 = k1;
ulagac.kontakt2 = k2;
ulagac.kontakt3 = k3;
ulagac.kontakt4 = k4;
ulagac.kontakt5 = k5;
}
private void frmInvestitoriUI_Load(object sender, EventArgs e)
{
if (this.Name == "Izmjena") // control Name of form !!!!!!! add or edit depend on Name
{
this.Text = "Izmjena podataka o investitoru";
Priprema(); // priprema polja za uredenje podataka...
}
}
But I want to use this form for 4 DGV views... Now, I think to put all methods and properties from class in form!!! And forgot evrithing. Just call form with arguments and all logic is in form. Not good idea??