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
Related
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";
}
So i have that aplication:
Menu Picture
and when i press the button "Terminar sessão" it's supposed to log out (and its works)
but when i went back to login the menu does not close!
Login Picture after log out
I am using nested form, the form ends session is closing but not the menu.
This is my menu.cs:
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelChildForm.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
openChildForm(new Transferências());
Slidepanel.Height = (button2.Height - 15);
Slidepanel.Top = (button2.Top + 10);
}
and on "Transferências" form i have it:
private void Sessao_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Tem a certeza que deseja terminar sessão?", "Terminar Sessão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Hide();
Login login = new Login();
login.ShowDialog();
}
}
i already tried Menu.Close(); but it does not work
With the amount of information you provide, it is quite hard to tell what the problem might be. Did you try to debug it to verify that your program actually tries to execute Menu.Close()?
If you show your code people might be able to help you more.
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 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 building a kids learning application, where clicking on a button on panel, I want to show different forms in the same place of the panel. Can you please help with any walk-through or tutorial links?
This question should have been posted on Stackoverflow website rather than here.
But you can use this approach to handle the case.
subForm = new SubFormYouWantToLoad();
subForm.TopLevel = false;
subForm.FormBorderStyle = FormBorderStyle.None;
ContainerPanel.Controls.Add(subForm , 0, 1);
subForm .Visible = true;
You can add this code when you click on the specific button.
Here each subform is added to the Panel as a Control. You should remove the subform from the panel's control list before adding another subform. For this ,it is better to remove,close and dispose the first one.
ContainerPanel.Controls.Remove(activeform);
activeform.Close();
activeform.Dispose();
Instead of Forms use user controls and load them in to panels
Sample if you want to show usercontrol1
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr1 = new UserControl1();
usr1.Show();
panel1.Controls.Add(usr1);
If usercontrol2
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr2 = new UserControl2();
usr2.Show();
panel1.Controls.Add(usr2);
You could create a number of forms as user controls or a control that inheriets from a panel. Then have a parent form with a panel to hold the user controls. You can then change the active user control in the container when the panel needs to be changed.
There is a tutorial on msdn for creating user controls.
http://msdn.microsoft.com/en-us/library/a6h7e207(v=vs.71).aspx
I used this code to close the form on the panel but not worked..
private void button12_Click(object sender, EventArgs e)
{
dontShowPANEL();
//ActiveForm.Close();
MainImaginCp kj = new MainImaginCp();
//kj.Visible = false;
kj.panel2.Controls.Clear();
panel1.Visible = true;
EngABCLearning usr1 = new EngABCLearning();
usr1.Show();
kj.panel2.Controls.Add(usr1);
//kj.Focus();
}
And I used the following code to show the form in the panel.
private void toolStripMenuItem1_LR_ENG_Click(object sender, EventArgs e)
{
//kids.Form2 hj = new kids.Form2();
//hj.Show();
EngABCLearning gh = new EngABCLearning();
//gh.Show();
gh.TopLevel = false;
gh.FormBorderStyle = FormBorderStyle.None;
//Panel2.Controls.Add(subForm, 0, 1);
panel2.Controls.Add(gh);
gh.Visible = true;
}
This is closing my main form and exiting the application.
try this out i have loaded two forms inside a single panel
private void Form1_Load(object sender, EventArgs e)
{
Form2 f1 = new Form2();
f1.TopLevel = false;
f1.AutoScroll = true;
panel1.Controls.Add(f1);
f1.Dock = DockStyle.Left;
f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f1.Show();
//form2
Form3 f2 = new Form3();
f2.TopLevel = false;
f2.AutoScroll = true;
panel1.Controls.Add(f2);
f2.Dock = DockStyle.Left;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Show();
}
try this i used this method to load multiple forms at one panel
private Form activeForm = null;
public void FormLoad(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
panelName.Controls.Add(childForm);
childForm.Visible = true;
}
private void YourBtn1_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm1Name());
}
private void YourBtn2_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm2Name());
}