Show a new form when click a button - c#

I am new in c#. I am trying to show a new form (form2) when click button in form1.
this is my code.
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 SliceEngine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
}
the error show
the type or namespace name 'Form2' could not be found (are you missing
a using directive or an assembly reference?)
this is my code for form2.
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 SliceEngine
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
for form2, i just making the design interface.
all i know when using java, i only need to declare the object first. what should i do for this?

I don't see any reason to fail your code, unless you have any typo. I have tried the same code as yours and it worked well on my machine.
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 winapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
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 winapp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
}

the type or namespace name 'Form2' could not be found (are you missing
a using directive or an assembly reference?)
It means that you forgot to add the namespace that points to the Form2 directory to your code
If you have got a Form2.cs inside a directory named UI and that directory is inside MyForms directory, then the whole tree would be ProjectName >> MyForms >> UI >> Form2.cs
So you should use this namespace in your code
using ProjectName.MyForms.UI;
Now I should be able to start showing it easily cause I've added its location.
new Form2().Show();
OR instead of all that and bother adding a namespace, you can just use:
new ProjectName.MyForms.UI.Form2().Show();

In form1, you are using the constructor for Form2:
public partial class Form1 : Form
{
public Form2()
{
InitializeComponent();
}
if you change it to
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
you should be fine.

Your code claims you don't have a constructor for Form1.
public partial class Form1 : Form
{
public Form2()
{
InitializeComponent();
}
should be:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

I assume below must be the reason why your code is failing.
You have both of your forms in Form1 and Form2, where Form2 definition is done in another namespace directive, which is not integrated in the namespace of Form1, also you can't use same namespace directive name for two namespaces unless you are overriding them.

Try this code.....
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
{
frm2.ShowDialog();
}
}

private void button5_Click(object sender, EventArgs e)
{
Form2.show()
}

My solution:
In click event of Form1 include your button:
string foobar = "Hello world";
Form2 frm2 = new Form2(foobar);
frm2.ShowDialog();
In Form2:
public Form2(string foobar)
{
InitializeComponent();
textbox1.Text = foobar;
}

For anybody still looking for an answer:
At the top of your code, add this namespace:
using YourProjectName;
Then, when you wish to show your form, type this:
var form = YourProjectName.YourFormName();
form.Show(); // Show form using new variable

Related

C# Change usercontrol labeltext and background color

My problem is simple. I want to click a panel in Form1 that will cause label1 in a userControl1, which will be placed upon form2 to change to "Text".
Clicking this panel would also change the background color of said userControl1. I receive the error "'TileInterFaceTest.Usercontrol1.label1' due to its protection level" which frankly baffles me.
Even running the color change code separately it simply doesn't achieve the desired result.
To be clear, I'm quite a novice when it comes to C# and programming. I've been working with Visual Basic until now so the concept of classes, methods and objects are slightly confusing to me.
Here is my 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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
UserControl1 userControl1 = new UserControl1();
form2.Show();
userControl1.BackColor = System.Drawing.Color.Red;
userControl1.LabelText = "Text";
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Code for UserControl1:
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 WindowsFormsApplication1
{
public partial class UserControl1 : UserControl
{
public String LabelText
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
}
}
label1 is a private field, which means you cannot access it outside of the class UserControl1.
What you could do is add a public property in the definition of the class UserControl1:
public String LabelText {
get {
return label1.Text;
}
set {
label1.Text = value;
}
}
Then use this property to modify the value of the Text of label1:
private void panel1_Click(object sender, EventArgs e)
{
form2.Show();
userControl1.BackColor = System.Drawing.Color.Red;
userControl1.LabelText = "Text";
}

How to change backGround Image from another form?

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

How to get checkstate and checkbox.text from different Class

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

Text between two forms with color in C#

I have 2 forms with a main window and a second window. The main window (Form1) shall get text from the second window (Form2)
My second window (Form2) can write text from form 2 to form 1. In the class I can choose color for my text but my problem is that when I push the button who shall send the text it just comes the text without the color I choose so its just black text in Form1 when I send example yellow.
I'm not a C# expert since I'm pretty new at this. I'm sure its a pretty simple problem to fix but for me its not so easy.
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.Windows.Forms;
namespace tester
{
public partial class Form1 : Form
{
public string text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 NewForm2 = new Form2(this);
NewForm2.Show();
}
internal void populate()
{
richTextBox1.Text = text;
}
}
Form2.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 Tester
{
public partial class Form2 : Form
{
Form1 texting;
public Form2(Form1 iForm)
{
texting = iForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
texting.text = richTextBox1.Text;
texting.populate();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if ( MyColorDialog.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.ForeColor = MyColorDialog.Color;
}
}
}
Just as you are making public string text in your Form1, you could make a public Color rictTextBoxColor property. Then setting it as well, and referring to it in your populate method

How to get variable from form1 to form2 with { get; set;}?

All.
I'm newbie in C#. I know this is very popular question. But I didn't understand. I know there is a mistake, but where?
For example - first part of code Form1 include private variable test, I need to get the value of this variable in the Form2. Where is the error?
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string test = "test this point";
Form2 dlg = new Form2();
dlg.test = test;
dlg.Show();
}
}
}
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string test { get; set; }
public Form2()
{
InitializeComponent();
label1.Text = test;
}
}
}
In your Form2 you are using a public property, because it is public you can assign it via the object in form1. For example:
private void button1_Click(object sender, EventArgs e)
{
Form2 dlg = new Form2();
dlg.test = "test this point";
dlg.Show();
}
There are a couple of ways to use this in form 2, if you just want it to set the text property of the label only, this would be the best:
public partial class Form2 : Form
{
public string test
{
get { return label1.Text; }
set { label1.Text = value ;}
}
public Form2()
{
InitializeComponent();
}
}
Within the setter of the property you could also call a function if required.
You are right, this type of question has been asked many times, slightly different versions... Here's some answers I've provided in the past
This might be closest for what you are looking for
One answer getting a value via a method calls
Another, with step-by-step to create two forms and getting values to/from the other with function or Getter(setter)
You're not using the string test anywhere within your method. Try this:
private void button1_Click(object sender, EventArgs e)
{
Form2 dlg = new Form2();
dlg.Test = "test this point";
dlg.Show();
}
See how you're assigning the value to the property Test on the Form2 object dlg.
Note: I used a captial for the property Test since that's the general consensus on style for property names.
test is a property available for Form2 (better case it to Test) while the string test is just scoped for the click event of Form1. It has no relation to the property unless you assign it.
Form2 dlg = new Form2();
dlg.test = test; // this will assign it
dlg.Show();
Now Form2 property has got the value which will be used to display the same in the Label

Categories