When I start app form1 labelStatus = "Can't Edit" and after I click btnedit form2 is open
and
labelStatus = "Editable "; but after I close form2 by button close on windows it don't change
back labelStatus to "Can't Edit" again
Form1
public static string txt;
private void btnEdit_Click(object sender, EventArgs e)
{
editForm edit= new editForm();
editForm.Show();
labelStatus.Text = "";
}
In form1
Where I should put labelStatus.Text=txt;
Form2
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1.txt = "Can't Edit";
}
Find Form1 by Form1 name from OpenForms in Appliction
use following code
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
var form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "formName").FirstOrDefault();
form1.labelStatus.Text = "Can't Edit";
}
Related
I doing my school project and I can't get multiple pictures from form 1 passing to form 2. More specifically, on form 1, users will click on any pictures they liked and on the next form, the picture will slowly show. I've tried to research but for most of the topic outside, they can only pass an image. This is the code I try to put down on every pictureBox click event. This will run but showing error when I try to click on picture.
Form1
namespace CyberShop_Gia
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text == "")
{
MessageBox.Show("You've to enter your name first");
}
else
{
panel1.Visible = false;
panel2.Visible = true;
button1.Visible = false;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox2.Visible = false;
pictureBox3.Visible = false;
PictureBox pb = pictureBox1 as PictureBox;
Form2 f1 = new Form2(pb.Image, textBox1.Text);
f1.Show();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
pictureBox3.Visible = false;
PictureBox pb1 = pictureBox2 as PictureBox;
Form2 f2 = new Form2(pb1.Image, textBox1.Text);
f2.Show();
}
private void pictureBox3_Click(object sender, EventArgs e)
{
pictureBox2.Visible = false;
pictureBox1.Visible = false;
PictureBox pb2 = pictureBox3 as PictureBox;
Form2 f3 = new Form2(pb2.Image, textBox1.Text);
f3.Show();
}
}
}
Form2
public partial class Form2 : Form
{
public Form2(Image pic, string username)
{
label1.Text = "Welcome " + username;
InitializeComponent();
pictureBox1.Image = pic;
}
}
This is the error when I tried to click on any picture on form 1, and the full detail of the error can be seen here. Here is the full code of my program.
You are assigning text to label1.Text before InitializeComponent(); do it after InitializeComponent();
I have form 1 and form 2. In form 1, I have 4 button which button 1 and 2 is use to open form 2 by provide the form 2 with specific name which is form2A and form2B. Button 3 and 4 is use to close the opened form base on the form name. How I can close the form2A when click button 3 but not close the form2B?
public static List<Form> forms = new List<Form>();
Form Open
TagItem.TagItemInfo tagItemInfo = new TagItem.TagItemInfo(symbol_id);
tagItemInfo.Name = symbol_id.ToString();
TagList.forms.Add(tagItemInfo);
tagItemInfo.Show();
Form close
if(TagList.forms.Count > 0)
{
foreach(Form frm in TagList.forms)
{
if(frm.Name == o.SymbolID.ToString())
{
frm.Dispose();
frm.Close();
}
}
}
My system can open as many as form which will specify a name for it.
You have to define Form2A and Form2B as 2 different vars:
private Form2 _form2a, _form2b;
//open Form 2A with Windowtext "Form 2A"
private void Button1_Click(object sender, EventArgs e)
{
_form2a = new Form2 { Text="Form 2A" };
_form2a.Show();
}
//open Form 2B with Windowtext "Form 2B"
private void Button2_Click(object sender, EventArgs e)
{
_form2b = new Form2 { Text="Form 2B" };
_form2b.Show();
}
//close Form 2A
private void Button3_Click(object sender, EventArgs e)
{
_form2a.Close();
}
//close Form 2B
private void Button4_Click(object sender, EventArgs e)
{
_form2b.Close();
}
I want to show data in DataGridView which resides in form1 and data reside in form2. Form2 has a button named “ADD” which adds all the data in form1’s grid. Following code is working properly for the same form, how do I edit this code in order to show data from another form.
private void btn_Add_Click(object sender, EventArgs e)
{
InputUserInfo frm1 = new InputUserInfo();
frm1.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
frm1.dataGridView1.RowTemplate.Height = 120;
frm1.dataGridView1.AllowUserToAddRows = false;
int numberOfRows = frm1.dataGridView1.Rows.Count;
if (numberOfRows < 5)
{
MemoryStream ms = new MemoryStream();
thumb_pic.Image.Save(ms, thumb_pic.Image.RawFormat);
byte[] img = ms.ToArray();
frm1.dataGridView1.Rows.Add(lbl_CP_UserID.Text, lbl_CP_Name.Text, img);
}
else
{
MessageBox.Show("Please insert Only 5 Images");
}
this.Hide();
frm1.Show();
}
Maybe you have found a solution already but this might help someone in future.
The form (see pic Form1) with grid
Code behind this Form:
public Form1()
{
InitializeComponent();
}
public DataGridView DataGridView1
{
get
{
return dataGridView1;
}
}
private void btnAddItems_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.TopMost = true;
frm.ShowDialog();
}
Form (See pic Form2) to add data into Form1 dataGridView
Code behind this Form:
public Form1 frm1;
public Form2(Form1 gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (frm1.DataGridView1.Rows.Count < 5)
{
frm1.DataGridView1.Rows.Add(txtOrderNo.Text, txtDesc.Text);
frm1.DataGridView1.Refresh();
txtOrderNo.Text = txtDesc.Text = "";
if (frm1.DataGridView1.Rows.Count == 5) this.Close();
}
else
{
MessageBox.Show("You can only add up to 5 items.");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
I am using Visual studio 2012. I created two forms, form1 with a button to open form2 and the form2 has a button "exit" which will take me back to form1.
this is my code from form 1:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Hide();
}
and from form 2:
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
}
I know I can use frm2.Show(); this.Hide(); instead of frm2.showdialog();. But, I need my form 1's state to be unaltered. My form1 contains a login form, which only enables the buttons (such as the new form button) if the login is correct. So if I hide form1 and just show it again, the login resets;
On Form2 class add a property to store the reference to the parent form:
public Form ParentForm { get; set; }
Then on Form1, you can show Form2 this way, hiding Form1 at the same time:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ParentForm = this;
this.Hide();
frm2.ShowDialog();
}
When closing Form2, you can show Form1 again:
private void btnExit_Click(object sender, EventArgs e)
{
this.ParentForm.Show();
this.Close();
}
Or even better, close Form2 this way:
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.ParentForm != null)
this.ParentForm.Show();
}
This will also show Form1 back if we the user closes Form2 using the cross button in the title bar.
Why don't you handle the login in the form_load event of the main form. The form_load does not run every time the form regains focus. Is it appropriate to close the form on them if they do not login on load? In my case, I send an email with the username of windows user to Domain administrators and close the program. They have to reopen the program to give it another go.
private void frmMain_Load(object sender, EventArgs e)
{
//Check login
Form frmLogin = new Form();
frmLogin.ShowDialog();
if (frmLogin.LoginSucessful == true)
{
btnRecords.Enabled = true;
lblWarning.Visible = false;
}
else
{
btnRecords.Enabled = false;
lblWarning.Visible = true;
lblWarning.Text = "You must first Login";
}
//other setup code here
}
private void button5_Click(object sender, EventArgs e)
{
if (domainUpDown2.Text == "Battlefield: Bad Company 2")
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
All that does is open a new blank form, but i need it to open a new form with a webbrowser in it so i can set it url dependent on the if statement..
Assuming your form2 has a WebBrowser control, and that it has a property you can set like this:
public Uri WebLocation
{
set { webBrowser1.Url = value; }
}
Then modify your code as follows:
private void button5_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
if (domainUpDown2.Text == "Battlefield: Bad Company 2")
form2.WebLocation = new Uri("http://badcompany2.yoursite.com");
if (domainUpDown2.Text == "Some Other Item")
form2.WebLocation = new Uri("http://someotheritem.yoursite.com");
form2.ShowDialog();
}