I'm working on a WindowsFormProject in C# of which carry the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
}
this is code for Form1;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textBox1.Text = "prova";
f1.Refresh();
}
}
}
end this is code for Form2.
the goal is to write a text in the textbox of the form 1, I set the access modifier as public, but does not work
Any solution?
Try this
In Form2:
public Form _parent;
public Form2(Form Parent)
{
InitializeComponent();
this._parent = Parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova";
}
In Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.Show();
}
Note that in the "button1_Click" in Form2 you are creating a new instance of "Form1" so Form2 does'nt really "knows" the Form1 that is allready open,
You should pass this object reference (Form1) to Form2,
Similar to the solution of Marcio but instead of passong the inherite class "Form" you should pass "Form1"
In Form2:
public Form1 _parent;
public Form2(Form1 parent) //<-- parent is the reference for the first form ("Form1" object)
{
InitializeComponent();
this._parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova"; // you won't get an exception here because _parant is from type Form1 and not Form
}
In Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this); //<- "this" is the reference for the current "Form1" object
f.Show();
}
Related
Hey I got Code Written in Class Form1 And Form2. I want to call the method openkindForm() from Form2. I tried every soloution I found. I got this one at the moment which is not working it gives me a System.NullReferenceException. I do not know why it isnt working. I tried it so long but whatever I do it will not workout somehow. I would be thankfull for an answer.
Kind regards
First Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FBDP00
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
submenupanel.Visible = false;
}
private void funktionenSM_Click(object sender, EventArgs e)
{
switch (submenupanel.Visible)
{
case true:
submenupanel.Visible = false;
break;
case false:
submenupanel.Visible = true;
break;
}
}
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2());
}
public Form activeForm = null;
public void openkindForm(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
backgroundPanel.Controls.Add(childForm);
childForm.Show();
}
}
}
Class 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static FBDP00.Form1;
namespace FBDP00
{
public partial class Form2 : Form
{
public Form1 testform;
public Form2()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void funktionenSM_Click(object sender, EventArgs e)
{
}
public void baukontrolleb_Click(object sender, EventArgs e)
{
testform.openkindForm(new Form3());
}
}
}
In Form2 you have defined a variable for Form1 (testform), but you don't set this anywhere. So this is why you get a Null reference error when you try to use it, because it is null!
So when you create your Form2 then you need to set this value. In your case, maybe in the constructor like this.
public Form1 testform;
private Form2(Form1 f1)
{
InitializeComponent();
testform = f1;
}
Then call it like this:
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2(this));
}
However, looking at your openkindForm method, it seems to me that this really has nothing to do with Form1, and shares no variables, so should not be in this class.
You should either make this static (together with the activeForm variable), or make it a Singleton class instead. But certainly this should be a separate class.
When I try to connect two forms, I successfully connect them, but the problem is that when I open Form2 from Form1 then Form2 opens but Form2 does not show its buttons, text etc.
I used these instructions.
I am using Visual Studio 2012. I believe Visual Studio is doing this mistakenly and I might have to install new visual studio.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
// Create Form2.
public class Form2 : Form
{
public Form2()
{
Text = "Form2";
}
}
}
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login_Form
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
Now remove following code snippet from Form1 class
// Create Form2.
public class Form2 : Form
{
public Form2()
{
Text = "Form2";
}
}
Is there any way to retrieve the updated size of panel after FormWindowState.Maximized? I can get the size of the panel but it's giving the original size of panel.
Thanks;
By the way this is the code for usercontrol
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
//this is the way i retrieve the panel size
Form1 f = new Form1();
this.Size = f.Size;
}
}
}
this is forms code, the panel has 4 activated anchors
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 n = new UserControl1();
panel1.Controls.Add(n);
}
private void Form1_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
}
}
If what you want to do is to resize the user control every time the parent Form is resized, you could use the Resize event of the ParentForm:
Main form:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ans
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
var uc = new UserControl2();
uc.Location = new Point(0, 0);
this.Controls.Add(uc);
WindowState = FormWindowState.Maximized;
}
}
}
User control:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ans
{
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
private void UserControl2_Load(object sender, EventArgs e)
{
this.BackColor = Color.Aqua;
if (this.ParentForm != null)
{
this.Size = this.ParentForm.Size;
this.ParentForm.Resize += ParentForm_Resize;
}
}
private void ParentForm_Resize(object sender, EventArgs e)
{
this.Size = ((Form)sender).Size;
}
}
}
If you want the UserControl you create dynamically at runtime to grow and shrink with the control that it is placed on, then set the Dock property of the UserControl to Fill at runtime:
private void button1_Click(object sender, EventArgs e)
{
MyUserControl userControl = new MyUserControl();
panel1.Controls.Add(userControl);
userControl.Dock = DockStyle.Fill;
}
I have two form and i want to change backGround of first form from 2nd form. i have already choose a backGround image for form1 and button1 in form2, but nothing happens. thanx in advance (Windows Form)
1st form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
2nd Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.BackgroundImage = button1.BackgroundImage;
}
}
}
In your second form, add a private member that will hold a reference to the first form:
private Form _form1 = null;
Then in the constructor for Form2, allow that reference to be passed in:
public Form2(Form form1)
{
InitializeComponent();
_form1 = form1;
}
Now, in that button click handler, you can:
private void button1_Click(Object sender, EventArgs e)
{
_form1.BackgroundImage = button1.BackgroundImage;
}
An alternative approach would be to add a method to Form1 that receives the image to be set as background. Assume the same _form1 reference exists in Form2, you add this to Form1:
public void ChangeBGImage(Image bgImage)
{
this.BackgroundImage = bgImage;
}
And from Form2, you call it:
private void button1_Click(Object sender, EventArgs e)
{
_form1.ChangeBGImage(button1.BackgroundImage);
}
try this,
FOrm1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//check if button1 clicked and then change the background
if(frm2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.BackgroundImage = frm2.GetBackImage();
}
}
}
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Form1 frm1 = new Form1();
//frm1.BackgroundImage = button1.BackgroundImage;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
public Image GetBackImage()
{
return this.button1.BackgroundImage;
}
}
}
The problem is that you haven't access to the form1 from form2 to change it. If you want to change something in form1 you shouldn't create new instance of Form1. You should get the instance in the constructor.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
Form1 frm1;
public Form2(Form1 frm1)
{
InitializeComponent();
this.frm1 = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
frm1.BackgroundImage = button1.BackgroundImage;
}
}
i need your help again.. i doing this program that if the checkbox from Form2.cs is checked then i will clicked a button to show another WindowsForm(another Form.cs) that has a listbox that will show a the text from that checkbox. here is my work on the other form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication7
{
public partial class computationOfDineIn : Form
{
Form2 form2 = new Form2();
public computationOfDineIn()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (form2.checkBox1.Checked)
{
listBox1.Items.Add(form2.checkBox1.Text.ToString());
}
}
}
}
i change the modifier of the checkbox in Form2.cs to Public so i can use it in another form. but it doesn't work, am I missing somehthing? please somebody tell me. (Q. how can i make it appear in the listbox from another form when the conditions is met?) I knmow this is a silly question but thank you in advance!!! :D
update: code on where Form2 is shown.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
It seems you're creating new instance of Form2 inside computationOfDineIn. You should use the same instance of form2 which you show to user.
And making the UI elements public is not the recommended way. IMO you've to create a property which says whether the checkbox is checked or not.
You need to save the form2 instance somewhere and somehow you need to pass the instance to computationOfDineIn form then check the property in that instance.
One way is to pass it through constructor.
public partial class computationOfDineIn : Form
{
Form2 form2 = null;
public computationOfDineIn(Form2 form2)//pass the form2 you created in button click
{
this.form2 = form2;
InitializeComponent();
}
}