I have 2 forms, form1 and form2. In form1 I call form2, where I input 2 numbers, one for height and for width of a picturebox. Then I want to pass that data from form2 to form1, where I create picturebox with said size.
Then I want to store height and width to class and then access that info from form1.
Here is my code:
Form1
namespace NPA_projekt
{
public partial class Form1 : Form
{
private Form2 f2 = new Form2();
image img = new image();
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
f2.ShowDialog();
}
private void btnTest_Click(object sender, EventArgs e)
{
pbMainArea.Width = img.width;
pbMainArea.Height = img.length;
}
}
}
Form2
namespace NPA_projekt
{
public partial class Form2 : Form
{
image img = new image();
public Form2()
{
InitializeComponent();
}
//reset btn
private void button1_Click(object sender, EventArgs e)
{
nudWidth.Value = 640;
nudLength.Value = 400;
}
//cancel btn
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
//ok btn
private void btnOK_Click(object sender, EventArgs e)
{
img.width = Convert.ToInt32(nudWidth.Value);
img.length = Convert.ToInt32(nudLength.Value);
this.Close();
}
}
}
Class image
namespace NPA_projekt
{
class image
{
public int width = 0;
public int length = 0;
}
}
Values, that are stored in form2, are set to their original values, when I want to use them in form1. Could someone please elaborate what's happening.
Thank you all!
img is declared twice. Once in Form1 and again in Form2. When you are setting the width and height of img in Form2 you are setting it for the image instance you declared in Form2 not Form1. You need to make the img in Form1 visible to Form2 and perform the operation on that.
So, make img in Form1 public:
public image img {get; set;}
public Form1()
{
InitializeComponent();
img = new image();
}
Then you need to access it in Form2 (one way should be the Parent property of the form):
private void btnOK_Click(object sender, EventArgs e)
{
var form1 = (Form1)this.Parent
form1.img.width = Convert.ToInt32(nudWidth.Value);
form1.img.length = Convert.ToInt32(nudLength.Value);
this.Close();
}
I haven't tested this all out, but the approach is valid. The key to eliminating the confusion is getting rid of the img declaration in Form2 and realizing that you need to get access to Form1 from Form2
Related
I have two forms. The main form contains a treeview. After I show the second form, the treeview loses focus. That's okay, but I want to activate the treeview when the second form closes.
Form1.cs
namespace ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.StartPosition = FormStartPosition.CenterParent;
form2.ShowDialog();
}
}
internal void example()
{
treeView1.SelectedNode = treeView1.Nodes[1];
}
private void Form1_Load(object sender, EventArgs e)
{
TreeNode node = new TreeNode("aaaa");
treeView1.Nodes.Add(node);
node = new TreeNode("bbbb");
treeView1.Nodes.Add(node);
node = new TreeNode("cccc");
treeView1.Nodes.Add(node);
}
}
}
Form2.cs
namespace ex
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
form1.example();
//not working
form1.treeView1.Focus();
form1.treeView1.Select();
}
}
}
Form2 really shouldn't get so intimate with Form1. Try turning your code around like this:
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
if (form2.ShowDialog(this) == DialogResult.OK) {
treeView1.Select();
example();
}
}
}
If Form2 is supposed to supply any information to add to your TreeView control, you would set up a property on Form2 and access it from within this same code block.
This is the first form( it contains an OK button and a textbox)
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
MessageBox.Show(dimx.ToString());
}
}
}
This is the second form (it contains an OK button + a messageBox when OK is pressed)
namespace Testt
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1();
MessageBox.Show(f.dimx.ToString());
}
}
}
I want to write the value of 6 in the textbox press OK, then form2 pops up and when i press OK on the second form it should display 6 not 0..what am i doing wrong?
You could make it so that your form takes dimx as a variable, so it would look like this
public partial class Form2 : Form
{
private int dimX;
public Form2(int dimx)
{
InitializeComponent();
dimX = dimx;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dimX.ToString());
}
}
alternatively you could pass the form itself, changing
public Form2(int dimx)
into
public Form2(Form1 f1)
You would then also have to replace
private int dimX;
//and
dimX = dimx;
//and
MessageBox.Show(dimX.ToString());
with
private Form1 f;
//and
f = f1;
//and
MessageBox.Show(f.dimx.ToString());
You are creating a NEW form object on every onclick event and that is not the way it works...
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1(); // no that way!!!
MessageBox.Show(f.dimx.ToString());
}
use instead a callback, delegate
So im using this now
Form1:
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
//MessageBox.Show(dimx.ToString());
}
}
}
and Form2:
namespace Testt
{
public partial class Form2 : Form
{
private Form1 f;
public Form2(Form1 f1)
{
InitializeComponent();
f=f1;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(f.dimx.ToString());
}
}
}
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
}
}
I have two forms, form1 and form2.
In form1, there is are two buttons, button1 and button2.
In form2, there is a listview, ListView1.
button1 should hold a string value called "Vanilla".
When button2 is pressed it opens form2.
On form2, in listview1 it should show "Vanilla" in the first column.
Form1
public partial class form1 : Form
{
public static string buttonValue = "";
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanilla";
}
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
this.Hide();
}
Form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can design the second form as bellow:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string _passedValue = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can pass the value stored in the first button using the bellow code.
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}
i want to have 2 forms in which the first form has a button that will load up form2 in a dialog form. form2 will show a listview displaying the data of a student. now i need to extract the 1st index of the selected row. once i double click on the row, form2 would close and pass the data into a textbox in form1.
i have used the code below which closes my form1 and creates a new instance of form1 in form2.
from form2:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1 wa= new Form1();
wa.loadid(cl);
wa.Show();
this.Close();
}
from form1:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Close();
}
public void loadid(String ms)
{
String newstring = ms;
studentid.Text = newstring;
}
I suggest using a Dialog, it makes it very easy:
This is Form1. You instantiate and open f2 as Dialog and then wait for its result.OK
private void Button1_Click(System.Object sender, System.EventArgs e)
{
var f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK) {
studentId.Text = f2.SelectedStudentId;
} else {
studentId.Text = "Select a Student!!!!";
}
}
This in Form2, where you have created your listview and a public property to expose:
public string SelectedStudentId { get; set; }
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
SelectedStudentId = cl;
DialogResult = DialogResult.OK; //will close this dialog (form2)
}
This should work for you
In Form1 create a public variable like this:
public partial class Form1: Form
{
//New variable
public static string StudentIDVal;
Then, change the button click on Form1 to be:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
studentid.Text = StudentIDVal;
}
Then, on Form2 on the item click you can simply have:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1.StudentIDVal = cl.ToString();
this.Close();
}