Hi,
I have 2 forms, frmLibrary and frmEdit. I am trying to pass variables from frmLibrary to frmEdit. The variables dont seem to be getting to frmEdit, can someone help? I have the following code:
frmLibrary
var form = new frmEdit();
form.Title = ListGrid.SelectedItems[0].Text;
form.Xexcrc = ListGrid.SelectedItems[0].SubItems[1].Text;
form.MediaId = ListGrid.SelectedItems[0].SubItems[2].Text;
form.Wave = ListGrid.SelectedItems[0].SubItems[3].Text;
form.Burnt = ListGrid.SelectedItems[0].SubItems[4].Text;
form.Protection = ListGrid.SelectedItems[0].SubItems[5].Text;
form.Folder = ListGrid.SelectedItems[0].SubItems[6].Text;
form.Show();
frmEdit
private string title;
private string xexcrc;
private string mediaId;
private string wave;
private string burnt;
private string protection;
private string folder;
public string Title
{
set { title = value; }
get { return title; }
}
public string Xexcrc
{
set { xexcrc = value; }
get { return xexcrc; }
}
public string MediaId
{
set { mediaId = value; }
get { return mediaId; }
}
public string Wave
{
set { wave = value; }
get { return wave; }
}
public string Burnt
{
set { burnt = value; }
get { return burnt; }
}
public string Protection
{
set { protection = value; }
get { return protection; }
}
public string Folder
{
set { folder = value; }
get { return folder; }
}
public frmEdit()
{
InitializeComponent();
txtTitle.Text = title;
txtXEXCRC.Text = xexcrc;
txtMediaID.Text = mediaId;
txtWave.Text = wave;
txtBurnt.Text = burnt;
txtProtection.Text = protection;
txtFolder.Text = folder;
}
Loading
theLizardPosted Apr 10, 2011, 11:25 PM
public partial class Form1 : Form
{
Form2 f = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
f = new Form2();
f.Show();
f.setTextBoxValue("Hello Form 2", null, "te"); //sending just the string for text box te on form 2
f.setTextBoxValue("", teFrm1, "te"); //or, sending the text box to get the text from for text box te on form 2
}
In form 2,
public void setTextBoxValue(string text, TextBox source, string dest)
{
TextBox e = null;
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i].GetType().Name == "TextBox" )
{
e = (TextBox)this.Controls[i];
}
if (e.Name == dest)
{
if (!String.IsNullOrEmpty(text))
{
e.Text = text;
}
else if (source != null)
e.Text = source.Text;
}
}
}
59 lines of code in your form 2 compared to 16 lines of code in my form 2
To do what you want is now simple,
f.setTextBoxValue(ListGrid.SelectedItems[0].Text, null, "txtTitle");
f.setTextBoxValue(ListGrid.SelectedItems[0].SubItems[1].Text, null, "txtXEXCRC");
//do the same for these..
form.MediaId = ListGrid.SelectedItems[0].SubItems[2].Text;
form.Wave = ListGrid.SelectedItems[0].SubItems[3].Text;
form.Burnt = ListGrid.SelectedItems[0].SubItems[4].Text;
form.Protection = ListGrid.SelectedItems[0].SubItems[5].Text;
form.Folder = ListGrid.SelectedItems[0].SubItems[6].Text;
Much easier and you don't need to worry about setting private members unless of course you wanted to use them elsewhere.
FroglegPosted Apr 10, 2011, 8:42 PM