How to pass data from childform to parentform - c#

I have two forms ,Form1 and Form2.
Form1 - Parent
Form2 - Child
Form1 Contains the following,
Textbox - it loads the file path,
Datagridview - it loads the file with its data,
ButtonNext -when button cliked it opens Form2,
Form2 Contains the following,
BrowseButton - it broswe for the file from the directory
Textbox - it then shows the path
ButtonFinish - it will tabes you back to Form1
*Now i want to access datagridview from Form1(Parent) from Form2(child). Now i can broswe the file on Form2 and when i click finish i can see my file path on Form1(parent) from the textbox but with no databeing loaded.
How can i load the data on Form1 into the datagridview ?
this is my code so far..
Form2.
public frmInputFile(frmMain_Page _frmMain)
{
InitializeComponent();
this._frmMain = _frmMain;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
BrowseFile();
}
private void btnFinish_Click(object sender,EventArgs e)
{
_frmMain.SetFilepath(txtInputfile.Text);
_grid.Rows.Clear(); //cant get the grid from form1
string PathSelection = "";
if (txtInputfile.Text.Length > 0)
{
PathSelection = txtInputfile.Text;
}
oDataSet = new DataSet();
XmlReadMode omode = oDataSet.ReadXml(PathSelection);
for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
{
string comment = oDataSet.Tables["data"].Rows[i][2].ToString();
string font = Between(comment, "[Font]", "[/Font]");
string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
string commentVal = Between(comment, "[Comment]", "[/Comment]");
string[] row = new string[] { oDataSet.Tables["data"].Rows[i][0].ToString(), oDataSet.Tables["data"].Rows[i][1].ToString(), font, datestamp, commentVal };
_grid.Rows.Add(row);
}
this.Hide();
Program._MainPage.Show();
Form1
private void btnLoadfile_Click(object sender, EventArgs e)
{
frmInputFile frmInput = new frmInputFile(this);
frmInput.Show();
}
public void SetFilepath(string Filepath)
{
txtInputfile.Text = Filepath;
}
//I dont know how i can handle the gridview here
public void Loadgrid(string LoadGrid)
{
Gridview_Input.ToString();
}

First things first. Please avoid duplicate posts.
What is the variable _grid doing here ?? Your way of passing data from one form to the other looks very strange. Nevertheless, I tried to simulate your problem and I am able to add the rows in Form1 from From2. The code listing is given below. Just a thing to note,I have added four columns in to my DataGridView in the designer. In your case you might want to add the columns as well programmatically.
public partial class Form2 : Form
{
public Form2(Form1 frm1)
{
InitializeComponent();
Form1Prop = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
Form1Prop.SetFilepath("HIHI");
Form1Prop.DataGridPropGrid.Rows.Add("HIH", "KI", "LO", "PO");
}
public Form1 Form1Prop { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
public void SetFilepath(string filepath)
{
textBox1.Text = filepath;
}
public DataGridView DataGridPropGrid
{
get
{
return dataGridView1;
}
}
}
Cheers

Related

C# : Modifying form1's listbox using form2's command button

I'm trying to make this simple program using Visual Studio C# 2013.
program screenshot: http://i.imgur.com/4QVbaa2.png
The listbox named receiptbox modifier was set to public using the properties panel.
Basically I am using 2 forms, what I want to happen is to show the quantity + the name of the food in the form 1's listbox.
This is the code when you click the food icon on form1:
private void pictureBox1_Click(object sender, EventArgs e)
{
FoodQty form2 = new FoodQty();
form2.Show();
}
It will show form2.
This is the source-code in the form2 and when you click its Ok button:
public partial class FoodQty : Form
{
Form1 mainfrm = new Form1();
Record recordInstance = new Record();
public FoodQty()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
mainfrm.receiptBox.Items.Add((int)numericUpDown1.Value + recordInstance.foodMenuArray[1]); // converts numupdown to int and appends the string array
}
}
Try this :
in form 1 :
private void pictureBox1_Click(object sender, EventArgs e)
{
FoodQty form2 = new FoodQty(this);
form2.Show();
}
in fom2 :
public partial class FoodQty : Form
{
Form1 mainfrm = new Form1();
Record recordInstance = new Record();
public FoodQty( Form1 fr)
{
InitializeComponent();
mainfrm =fr;
}
private void btnOk_Click(object sender, EventArgs e)
{
mainfrm.receiptBox.Items.Add((int)numericUpDown1.Value + recordInstance.foodMenuArray[1]); // converts numupdown to int and appends the string array
}
}

c# devexpress gridview row collect

c# devexpress gridview selected row count error
i want to get row from anouther form(gridcontrol), i tryed form.gridview.selected.row.count but its not working. what can i write for the get this count? and from other forms of money, i want to print labels to collect this form.
""
gridView1.Columns["money"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
gridView1.Columns["money"].SummaryItem.DisplayFormat = "Toplam:{0:n2}";
gridView1.OptionsView.ShowFooter = true;
""
i tryed this code its working but on the gridview but i want to write other form in the label.
form1 have gridView1.
form2 have label2. label2 display Sum from gridView1.
form1 must has form2 as its field or property.
form2 must has method public void SetSum(money sum) to set sum for label2.
Every count sum (e.g by click button) call form2.SetSum(sum)
MainForm.cs - Hosts form1 and form2
public partial class MainForm : Form
{
private Form1 _form1;
private Form2 _form2;
public MainForm()
{
InitializeComponent();
_form2 = new Form2();
_form1 = new Form1(_form2);
}
private void btnForm1_Click(object sender, EventArgs e)
{
_form1.Show(this);
}
private void btnForm2_Click(object sender, EventArgs e)
{
_form2.Show(this);
}
}
Form1.cs
public partial class Form1 : Form
{
private Form2 _receivedForm;
public Form1()
{
InitializeComponent();
}
public Form1(Form2 receivedForm) : this()
{
_receivedForm = receivedForm;
}
private void btnSend_Click(object sender, EventArgs e)
{
_receivedForm.SetSum(txtSum.Text);
}
}
Form2.cs:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void SetSum(string sum)
{
lblSum.Text = sum;
}
}
Hope this help. :D
ı did with this code now is working. thanks for all answer
DataBaseDataContext db = new DataBaseDataContext();
var sum = (from ord in db.safes select ord.money).Sum();
lbl_safein.Text = sum.ToString();

Add datagridview cell content into textbox in other form c#

I have 2 forms. One have a textbox to show name of chosen Customer and one have datagridview to show list of customers.
When i click on textbox the second form will open. I want to know is it possible to double click on datagridview cell then the text of textbox change by datagridview selected cell on first form?
Here is my codes.
I wrote this code to open form2:
private void textBox1_Click(object sender, EventArgs e)
{
frmCustomer customer = new frmCustomer();
customer.ShowDialog();
}
and in form2 :
private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
string name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
form1 f = new form1();
f.txtCustomer.Text = name;
this.close();
}
there is nothing in textbox when form2 closed.
any Help? Thanks a milion
You can try passing Form1 as a Parameter when creating the Form2.
private void textBox1_Click(object sender, EventArgs e)
{
frmCustomer customer = new frmCustomer(this); // this represents the Form1
customer.ShowDialog();
}
then on the form2
private Form1 frm_1;
public Form2(Form1 frm)
{
InitializeComponent();
frm_1 = frm;
}
private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
string name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
frm_1.txtCustomer.Text = name;
this.close();
}
this will do the work.
Closing the second form does not dispose it off. First form will still have access to its public members.
The second form can have a public property which can be accessed by first form after it is closed.
Try the following:
first form should contain:
private void textBox1_Click(object sender, EventArgs e)
{
frmCustomer customer = new frmCustomer();
customer.ShowDialog();
textBox1.Text = customer.Name;
customer.Dispose();
}
second form should contain:
public string Name { get; private set; }
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
this.close();
}

C# Pass selection from listbox in form1 to textbox in form2

I've got two forms and a custom class. I have populated a listbox in form1 using my custom class which holds several data types. I want to pass each of those values in the class located in the listbox to individual text boxes in form2. I'm having trouble figuring out how to access the individual values in each listbox instance of my class and then split them among the text boxes in form2. I thought I was on the right track by creating a property on form2 for my first textbox. I only have the one property set up right now because I wasn't sure it would work and was only testing. In form1 I was trying to set it up so I could access my class values from the selected item.
Form 1
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
DialogResult result = editProperties.ShowDialog();
object employeeSelect = lstBoxEmployees.SelectedValue;
editProperties.TextFirstName = Convert.ToString(employeeSelect);
}
form 2
public partial class frmProperties : Form
{
public string TextFirstName
{
get { return txtFirstName.Text; }
set { txtFirstName.Text = value; }
}
public frmProperties()
{
InitializeComponent();
}
}
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}
Form 2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}

C# how to pass the value of form3 back to form1

This question is the followup to the following question:
C# Text don't display on another form after double clicking an item in listbox
Now I have typed my value in the textbox of form3. How am I going to pass back the value to form1 to show it in the listbox10 after pressing "OK" in form3? Below is my form3 coding but it don't work:
private void button1_Click(object sender, EventArgs e)
{
//This is the coding for "OK" button.
int selectedIndex = listBox10.SelectedIndex;
listBox10.Items.Insert(selectedIndex, textBox1.Text);
}
You can put public property on form3:
public partial class form3 : Form
{
public String SomeName
{
get
{
return textbox1.Text;
}
}
...
private void buttonOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
In form1, where you are open form3, after ShowDialog, you will write:
if (form3.ShowDialog() == DialogResult.OK)
{
int selectedIndex = listBox10.SelectedIndex;
if (selectedIndex == -1) //listbox does not have items
listbox10.Add(form3.SomeValue);
else
listBox10.Items.Insert(selectedIndex, form3.SomeName);
}
do do something like that:
//form1:
public void add(int num)
{
//add num to the list box.
}
now, form3 should get an instance of form1 in the constructor, and save it:
//in form3:
private form form1_i
public form3(form i_form1)
{
.
.
.
form1_i = i_form1;
}
and on button click in form3, call the fumction add in form1.
It should go like this, this is the safest way to do it, in fact if you are working on Windows Mobile this is the only way that won't crash the application. In desktop versions it can crash in debug versions.
public partial class Form1 : Form
{
public string name = "something";
public Form1()
{
InitializeComponent();
}
public delegate void nameChanger(string nme);
public void ChangeName(string nme)
{
this.name = nme;
}
public void SafeNameChange(string nme)
{
this.Invoke(new nameChanger(ChangeName), new object[] { nme });
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(this);
f3.Show();
}
}
public partial class Form2 : Form
{
Form1 ff;
public Form2(Form1 firstForm)
{
InitializeComponent();
ff = firstForm;
}
private void button2_Click(object sender, EventArgs e)
{
ff.SafeNameChange("something different from the Form1");
this.Close();
}
}

Categories

Resources