ReUp: Passing multiple pictures between forms in C# - c#

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

Related

How to change label after close form

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

Passing multiple pictures between forms in C#

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:
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)
{
InitializeComponent();
pictureBox1.Image = pic;
}
}
Edit: 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

How do I show data in DataGridView of another form?

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

How to open form 2nd time when it's closed

i doing window app using c#.net.
i have a form name form_1 with menu-strip.
from the menu-strip of form_1, i am opening same form form_1 and closing the same form_1 after using it but if i click that for the second time it is not showing,if i click that for third time it is showing.
edit:
mainform
form fm;
bool frm= false;
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (frm== false)
{
fm= new form();
fm.MdiParent = this;
fm.Show();
frm= true;
}
else
{
if (fm.IsDisposed)
{
frm= false;
}
}
}
form
form fm = new form();
fm.MdiParent = this;
fm.Show();
this.Close();
If you expect your function addToolStripMenuItem_Click to always open fm (assuming it is disposed), then you'll need fm.show() in the else condition as well. You could try something like this instead...
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!frm || fm.IsDisposed)
{
if (fm != null && fm.IsDisposed) { frm = false; }
fm = new form();
fm.MdiParent = this;
fm.Show();
frm = true;
}
}
This probably makes your bool frm obsolete, but I left it in in case you're using it for something else.

how to call MdiChild from MDIParent form

I create a new MdiChild from MainForm using this method:
AdminLogInForm adminForm;
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null)
{
adminForm = new AdminLogInForm();
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
Why when i close my child, using in chld form "this.close()" using that method i cant open it anymore?
there i call close();
private void cancelLogInButton_Click(object sender, EventArgs e)
{
this.MdiParent.Activate();
if(this.MdiParent!=null)
((MainForm)this.MdiParent).LogInAsAdminMenuItem.Enabled = true;
this.Close();
}
by the way to make work that I asked before I hed to plase this.Close(); after all statements .
By closing the form you are not making adminForm instance to null (Which is what your if condition will check when you try to open it next time.)
On diposal of your form make adminForm = null and then your if condition will work next time.
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null)
{
adminForm = new AdminLogInForm(this);
adminForm.Disposed += new EventHandler(adminForm_Disposed); //Add Disposed EventHandler
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
void adminForm_Disposed(object sender, EventArgs e)
{
adminForm = null;
}
As Described by Marshal that the closing of a form makes it disposed you should add a condition for disposing as well like this
AdminLogInForm adminForm;
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null || adminForm.IsDisposed)
{
adminForm = new AdminLogInForm();
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
Or you can also create a function to use a form as mdi
like this

Categories