How to pass data from another form and back - c#

I've been trying to get this working. I'm sure I've written in correctely
Form 1
private string _temp;
public string Temp
{
get { return _temp; }
set { _temp = value; }
}
private void button1_Click(object sender, EventArgs e)
{
_temp = "test";
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
MessageBox.Show(main.Temp);
}

You have to get the reference to the Form1 instance via the sender parameter (assuming that Form2's button1_Click handler is attached to a button on Form1):
//Form2
private void button1_Click(object sender, EventArgs e)
{
Button button = (Button) sender;
Form1 main = (Form1) button.FindForm();
MessageBox.Show(main.Temp);
}

Related

How to change image of a picturebox from another form?

How can I change the image of a form PictureBox, from another form?
Example. Form2 image needs to change Form1 image.
I tried this.
exmp.Code 1
//This is for Form1
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Form2 Fr2 = new Form2();
Fr2.Owner = this;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
Fr2.Show();
}
//This is for Form2
private void pictureBox2_Click(object sender, EventArgs e)
{
try
{
(this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
}
catch
{
}
}
This code doesn't work!
But if I try this it will work.
exmp.Code 1
//Form1
private void pictureBox1_Click(object sender, EventArgs e)
{
Form2 Fr2 = new Form2();
Fr2.Show();
Fr2.Owner = this;
}
//Form2
private void pictureBox2_Click(object sender, EventArgs e)
{
try
{
(this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
}
catch
{
}
}
But I cannot use the second code because I need to click a ContextMenuStrip to open Form2.
Any question write in comments!
In Form1, "Fr2" needs to be declared at Form level so you can access it from both "pictureBox1_MouseEnter" and "pictureBox2_Click". Set the Owner by passing "this" to Show().
This is for Form1:
private Form2 Fr2;
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Fr2 = new Form2();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (Fr2!=null && !Fr2.IsDisposed)
{
Fr2.Show(this); // <-- pass Form1 as the Owner
}
}
This is for Form2:
private void pictureBox2_Click(object sender, EventArgs e)
{
Form1 Fr1 = this.Owner as Form1;
if (Fr1!=null)
{
Fr1.pictureBox1.Image = pictureBox2.Image;
}
}
To access "pictureBox1" in Form1, though, you'd have to change the Modifiers property of it to public.

How can you hide a button from another form

I have a C# project that has 2 forms. The first has 3 buttons. I need to be able from the second form to hide 2 (button1 and button2 )buttons with a checkbox, and I don't know how to call the buttons from the first form.
this is form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
and this is Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
?????????
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
Another option is to pass the form as the "owner" in the Show() command:
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this); // pass Form1 reference in to our instance of Form2
}
In Form2, cast the Owner property back to Form1 so you can access it (assuming you've changed the modifiers property of the buttons to public as already suggested):
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false; // or whatever your buttons are called
}
}
This is almost exactly what I had posted previously...you need to change the Modifiers property of the buttons so they are public and can be seen from Form2.
this is the final version that works in my case thanks to those who answered my question and helped me to get this answer
Form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm = new Form2();
frm.checkBox1.Checked = Properties.Settings.Default.checkB;
if (frm.checkBox1.CheckState == CheckState.Checked)
{
button1.Visible = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}
Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
The buttons and checkBox are set to Modifiers - Public
you need to make the buttons on the first form public and then you can access them once you create a instance of the first form you will need to pass that form to the second form.
Might want to look into building an event that fires from one form and gets handled by the other form to disable the button.

How Do I pass back value from form2 textbox to listbox value in forn1 c#

Code From Form1
private void EditBtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(textBox1.Text);
frm.ShowDialog();
frm.Show();
}
Code From Form 2
public partial class Form2 : Form
{
private object listBox1;
public Form2(string value)
{
InitializeComponent();
textBox1.Text = value;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form1.show();
}
}
}
I'm not sure if You want to keep open both forms or not. If You want to keep it open, and add items to Form1's ListBox, than there is an answer
public partial class Form1 : Form
{
private void EditBtn_Click(object sender, EventArgs e)
{
// listBox1 is already set on the designer
Form2 frm = new Form2(textBox1.Text, listBox1);
frm.ShowDialog();
frm.Show();
}
}
public partial class Form2 : Form
{
private ListBox _listBox1;
public Form2(string value, ListBox listBox1)
{
InitializeComponent();
textBox1.Text = value;
_listBox1 = listBox1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
_listBox1.Items.Add("returned Value");
}
}
Suggesting the below solution.
Add ListBox and Button to Form1.
Make the ListBox as public and static as in below code snippet to access this from Form2
public static System.Windows.Forms.ListBox listBox1;
Make the button click event as below
private void LoadForm2Btn_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.ShowDialog();
}
Now add another form Form2.
Add a text box and button to it.
Make the button click event as below
private void UpdateBtn_Click(object sender, EventArgs e)
{
if (UpdateBtn.Text != string.Empty)
Form1.listBox1.Items.Add(textBox1.Text);
}
Now,run the program.
open Form2 by clicking the button "LoadForm2Btn" in Form1. Enter the text you want to add in ListBox in Form1 and click "UpdateBtn" button. You text will be added in the Listbox

Passing Data form two different forms

My objective is to be able to enter a value in the textbox in form1 then press enter (when pressing the enter button the value will be pass to a method called setvalue. When the switch button is pressed then the form one will hide and open form2. Form2 has two buttons, show and exit. When show is clicked i need to display a messagebox that display the data in textbox in form1 by calling the getvalue method.
Please I'm open for ideas.
This is form one
public partial class Form1 : Form
{
private int secretValue;
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Visible = true;
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
this form two
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Add a constructor value on your form 2. It should look like this:
public Form2(int secValue)
Then you can call it on your form 1 by passing the value
Form2 frm2 = new Form2(secretValue);
Maybe you can assign it to a global variable in your form 2, so that it can be referenced by your code in all of the form. Your form two can now be:
public partial class Form2 : Form
{
int passedValue = 0;
public Form2(int secValue)
{
InitializeComponent();
passedValue = secValue;
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
}
}
In my opinion, there's no need for the property you created. Always remember, the values you assign to a class will be null if you change from form to form.
Following code will not work becuase you are creating new instance of form1 and trying to get the value from there, It will only have the default vaue of int.
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
}
Simply what you can do is to have a public property defined within Form2. and set the value of this property before showing Form2,
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
frm2.SecrectValueOfForm2 = ValueInForm1;
frm2.Visible = true;
this.Hide();
}
Plz try code as shown below:
public partial class Form1 : Form
{
private int secretValue;
Form2 frm2 = new Form2();
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frm2 .Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
frm2.Owner = this;
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
And On Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = ((Form1 )this.Owner).GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Form2
public Int32 _txtval { get; set; }
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(_txtval.ToString());
}
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._txtval = Convert.ToInt32(textBox1.Text);
frm2.Visible = true;
this.Hide();
}

passing the data to the selected cell of datagridview

I am working in winforms. In that, I have a datagridview. I have transferred the selected cell value to a new form, form2
But now I want to re-transfer the textbox value which is in form2 to the datagridview cell.
How can i do that?
On the form2, along with label1 there is button1 and a textbox. I want that when the textbox is filled and button1 is pressed, it will transfer the text from the textbox to the cell which was selected.
I have used following code for this.
The code of button_click event...
But an error occurs as follwing.
" object reference not set to instance of an object "
You do recreate the main form inside of form 2, which is probably not what you need.
Change the code to this:
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
form2 f2 = new form2();
f2.label1.Text = dataGridView1.SelectedCells[0].Value.ToString();
f2.ShowDialog();
dataGridView1.SelectedCells[0].Value = f2.textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Design Properties of DataGridView
dataGridView1.Modifiers = Public
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var f = new Form2 { Owner = this})
{
f.valueFromSelectedCell = dataGridView1.SelectedCells[0].EditedFormattedValue.ToString();
f.ShowDialog();
}
}
}
public partial class Form2 : Form
{
public string valueFromSelectedCell { get; set; }
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = valueFromSelectedCell;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = this.Owner as Form1;
var currentCell = f.dataGridView1.CurrentCell;
f.dataGridView1[currentCell.ColumnIndex, currentCell.RowIndex].Value = textBox1.Text;
Close();
}
}

Categories