Passing Value from One Form to Another (C#) - c#

I have a search form in my program. When the user double-clicks a cell (of the dgv) on the search form, I want the program to close that form and jump to the item on the main form.
I'm doing this by identifying each item with a unique id.
I'm trying to pass the value of the rows id to the other form. The problem is that, it says that I'm passing the value zero each time. But when I insert some message boxes on the search form, it says that the integer 'id' has successfully been assigned to the variable on the main form: public int CellDoubleClickValue { get; set; }
Here is my code:
Search Form:
private int id;
private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex1 = e.RowIndex;
this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
invmain inv = new invmain();
inv.CellDoubleClickValue = this.id;
this.DialogResult = DialogResult.OK;
this.Close();
//MessageBox.Show(inv.CellDoubleClickValue.ToString());
//Above, it shows it got assigned successfully.
}
Main Form:
public int CellDoubleClickValue { get; set; }
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
search form1 = new search();
form1.ShowDialog();
if (form1.DialogResult == DialogResult.OK)
{
MessageBox.Show(CellDoubleClickValue.ToString());
}//Here it shows that the value is: 0

I suggest you do as follows:
Search Form:
public int SelectedId { get; set; }
private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex1 = e.RowIndex;
this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
this.DialogResult = DialogResult.OK;
this.Close();
}
Main form:
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
search form1 = new search();
form1.ShowDialog();
if (form1.DialogResult == DialogResult.OK)
{
int selectedId = form1.SelectedId;
// Do whatever with selectedId...
}

Related

Why after linking to Form the data doesn't transfer from Form2 to Form1

I am trying to do a big coding project however I hit a wall.
I need to show the name and score once the data has been entered in.
I tried using youtube tutorials, classes for the code. But no such luck.
Any help would be great!
form1:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.Show();
SudentBox.Items.Clear();
}
form2:
public object StudentBox { get; private set; }
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
try
{
string name = txtName.Text;
int score = Convert.ToInt32(txtScore.Text);
txtStoreScores.Text += score.ToString() + " ";
}
catch (Exception x)
{
MessageBox.Show("Please enter a number");
}
}
private void bClearScores_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtScore.Text = "";
txtStoreScores.Text = "";
}
Examples of what the forms should look like with the final result.
If I'm correct, you are trying to code a form of DialogBox.
Say, you want to get a name from the dialog (e.g. from a TextBox in Form2), you can have a model like this (in Form2 of course).
public string Name
{
//where myTextBox is the design name of your textbox
get => myTextBox.Text;
set => myTextBox.Text=value;
}
Simple Ok Button
public void OkBtnClick(object sender, EventArgs e)
{
this.Close();
}
Now, you need to actually get this info to display in your Form1. That is easy.
Just like you have started above:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.ShowDialog();
//Note that you won't be able to access form1.
SudentBox.Items.Clear();
//You can now get the name
string _nameResult=link.Name;
NameTextbox.Text=_nameResult;
}
I hope this gets you started!
You do this by using the property.
Add public static property on Form 2 and set the values of the text to the property respectively and then access them on the Form 1.
On Form 2 in the Ok button click event do this
public static string Name { get; set; }
public static string Scores { get; set; }
private void bOk_Click(object sender, EventArgs e)
{
Name = txtName.Text;
Scores = txtStoreScores.TextBox;
}
Then in the Form 1 OnLoad event access those properties and display them in the TextBox
private Form1_Load (object sender, EventArgs e)
{
StudentBox.Items.Add(string.Format("{0} {1}", Form2.Name, Form2.Scores);
}

How to send Datagridview Values to Main form

I would like to send my Datagridview values to my Combobox in my mainform.
This is my code in my Datagridview form :
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
//Beam Description default value
DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
}
Below code is what i've used but nothing happened :
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
EquipmentList ELform = new EquipmentList();
//Beam Combo Box default values
var BCBi1 = ELform.DataTable.Rows[0].Cells[1].Value.ToString();
this.BeamCB.Items.Add(BCBi1);
}
When i run it i'm getting this error:
error when running
An advance thank you for all those who will help me.
DataTable.Rows[0].Cells[1].Value is assigned in EquipmentList form when it is loaded.
EquipmentList ELform = new EquipmentList(); - here form is created but not loaded (you need to invoke Show or ShowDialog for that).
I suggest to declare publicly accessible default value (and not rely on existance of DataTable object with 1 row, which is a low-level implementation detail)
public const string DefaultSize = "8.000m x 0.400m x 0.550m";
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
DataTable.Rows[0].Cells[1].Value = DefaultSize;
}
and then
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.BeamCB.Items.Add(EquipmentList.DefaultSize);
}
To the question "So is there any possible to load that values wihtout clicking/showing my equipmentlist form." Absolutely. Start by building a class for your data:
public class gridviewdata
{
public string content { get; set; }
public int row { get; set; }
public int cell { get; set; }
public gridviewdata(string content, int row, int cell)
{
this.content = content;
this.row = row;
this.cell = cell;
}
}
add a mediator class:
public static class mediator
{
public static List<gridviewdata> gridviewdatalist;
}
then add this code to your ELform load event:
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
//Beam Description default value
DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
mediator.gridviewdatalist.Add(new gridviewdata("8.000m x 0.400m x 0.550m", 0, 1);
}
then add a new method identifying your specific row to your MainForm:
private gridviewdata selectdata(int row, int cell)
{
foreach(gridviewdata data in mediator.gridviewdatalist)
{
if(data.row == row && data.cell == cell)
{
return data;
}
}
return null;
}
And finally call that code in your MainForm:
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
//and call it there
var BCBi1 = selectdata(0, 1);
this.BeamCB.Items.Add(new Item(BCDi1, 1)); //Add value and index
}
Hope this helps.

How to get value from one form to an other?

I made a form for deleting/removing products.
Now my question is how do I get the value from one form to the other so I can use this to update the amount of products or delete the products from a database?
I'm trying to get the value of tbAantal.Text, so I can use this in my other form to update the amount of products.
Or should I do it in a other way?
public partial class Bevestiging : Form
{
public Bevestiging()
{
InitializeComponent();
Aantal = 0;
}
public int Aantal { get; set; }
private void btnOk_Click(object sender, EventArgs e)
{
int aantal;
if (!int.TryParse(tbAantal.Text, out aantal))
{
MessageBox.Show("U kunt alleen numerieke waardes invullen.", "Fout");
return;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void BtUp_Click(object sender, EventArgs e)
{
Aantal++;
tbAantal.Text = Aantal.ToString();
}
private void BtDown_Click(object sender, EventArgs e)
{
Aantal--;
tbAantal.Text = Aantal.ToString();
}
So I can use this to update it here:
private void gridGeregistreerd_ColumnButtonClick(object sender, ColumnActionEventArgs e)
{
var dialog = new Bevestiging();
if (DialogResult.OK != dialog.ShowDialog()) ;
}
You've already made the public property "Aantal" on your first form with the right get/set so to retrieve the value on your second form use this:
using (Bevestiging myForm = new Bevestiging())
{
DialogResult result = myForm.ShowDialog();
if (result != DialogResult.OK)
{
int returnedValue = myForm.Aantal;
}
}
In you form, you have already define a public property :
public partial class Bevestiging : Form
{
public int Aantal {
get; set;
}
}
Then in your callee form, you can access it :
private void gridGeregistreerd_ColumnButtonClick(object sender, ColumnActionEventArgs e)
{
var dialog = new Bevestiging();
if (DialogResult.OK != dialog.ShowDialog())
{
int aantal = dialog.Aantal;
/* Save it to database or whatever you want */
}
}

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();
}

Passing data to DataGridViews

I have text boxes on my second form and a send button which is in the code shown below.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.PassName = richTextBox1.Text;
f1.PassLastName = richTextBox2.Text;
f1.PassAge = comboBox1.Text;
f1.PassGender = richTextBox3.Text;
f1.ShowDialog();
}
and a DataGridView on form 1 with this code
public partial class Form1 : Form
{
private string name;
private string lastName;
private string age;
private string gender;
public string PassName
{
get { return name; }
set { name = value; }
}
public string PassLastName
{
get { return lastName; }
set { lastName = value; }
}
public string PassAge
{
get { return age; }
set { age = value; }
}
public string PassGender
{
get { return gender; }
set { gender = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = name;
dataGridView1.Rows[n].Cells[1].Value = lastName;
dataGridView1.Rows[n].Cells[2].Value = age;
dataGridView1.Rows[n].Cells[3].Value = gender;
}
private void mnuExit_Click(object sender, EventArgs e) //adding the quit on the top file with caution message
{
if (MessageBox.Show("Do you really want to Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
private void addTask_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //show form2 so user can input data
f2.ShowDialog();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}`
This is fine if I want to send one set of data to the DataGridView, but if I add new information again then this opens a new DataGridView and stores it to another seperate DataGridView then I have two DataGridView forms. I want to put all the data onto one DataGridView and keep adding rows. So when the user clicks the add button on the first form with the DataGridView, it opens up the TextBox form which is form 2, then the user fills out the information and clicks the send button which sends the information back to the DataGridView, however this then opens up a new window with a new DataGridView. I dont want this to happen I want it to keep adding rows on the first form.
Could someone show me how to do this?
You can use ShowDialog(this) and Owner to get the parent form's property.
Form1
private void Form1_Load(object sender, EventArgs e)
{
//Move to Form1_Activated
this.Activated += new System.EventHandler(this.Form1_Activated); //connect
}
private void Form1_Activated(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = name;
dataGridView1.Rows[n].Cells[1].Value = lastName;
dataGridView1.Rows[n].Cells[2].Value = age;
dataGridView1.Rows[n].Cells[3].Value = gender;
}
private void addTask_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //show form2 so user can input data
f2.ShowDialog(this);//set this form as Owner
}
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;//Get the Owner form
f1.PassName = richTextBox1.Text;
f1.PassLastName = richTextBox2.Text;
f1.PassAge = comboBox1.Text;
f1.PassGender = richTextBox3.Text;
//f1.ShowDialog();
f1.Show();
this.Close();
}

Categories

Resources