Move PictureBox from one form to another form C# - c#

im actually trying to program in C# and want to move an image from a pictureBox1 from Form1 to another pictureBox1 of Form2.
I did following
In Form1.cs:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load("C:\\Users\\img_0.bmp");
}
In Form1.Designer: change from private to public
public System.Windows.Forms.PictureBox pictureBox1;
In Form2.cs:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Form1.pictureBox1.Image;
}
But i get this error: CS0120: An object reference is required for the non-static field, method, or property 'Form1.pictureBox1'
Can somebody help me please? :)

Although it is not appropriate to do it this way, but, Yes, this is possible.
First create the picture box in external class file like this:
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class Class1
{
public static PictureBox pb1 = new PictureBox();
}
}
Then add it into your 1st form, let's say another button click event will open another form. So you will have to first remove the picturebox, which will allow the 2nd form to use it.
public Form2()
{
InitializeComponent();
this.Controls.Add(Class1.pb1);
}
private void button2_Click(object sender, EventArgs e)
{
this.Controls.Remove(Class1.pb1);
Form3 f = new Form3();
f.ShowDialog();
this.Controls.Add(Class1.pb1);
}
Then, while you open the next form, add the picturebox and when the next form is closing, remove it.
public Form3()
{
InitializeComponent();
this.Controls.Add(Class1.pb1);
this.FormClosing += Form3_FormClosing;
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
this.Controls.Remove(Class1.pb1);
}
There, you have move the picturebox from 1st form to 2nd form and then back to 1st form.

Related

C# Back button on visual studio

I am creating a quiz for my As level coursework on visual studio 2019(c#). In this I will be creating a help button that will have information that the user may need if they are stuck. The button to access the help form will be avaliable through a menu strip bar loacted in the top corner of every form. In the help form there will be a menu strip bar with a back button. I would like to know how to code a back button to go back to the previous form eg Question 1-10 forms or the login form. I know how to code it if i wanted to back to a specific form but it is the fact it may need to back to any form as i dont know which form the user will have previously been on.
If you want to code a back button to go back to the previous form, you can refer to the following code:
Code in Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form2 newform = new Form2();
newform.ShowDialog();
this.Show();
}
}
Code in Form2.cs :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form3 newform = new Form3();
newform.ShowDialog();
this.Show();
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Code in Form3.cs :
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form4 newform = new Form4();
newform.ShowDialog();
this.Show();
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
If you want to open other forms, just change this code ‘Form2 newform = new Form2();’.
Commonly, one would store the questions in a list or array. If you also store the index of the current question, then you could use that to return to the correct question.
The button should navigate between different windows:
STEP 1 - add a new button on your current window.
STEP 2 - double click on that button to access cs file:
private void Button_Click(object sender, RoutedEventArgs e)
{
}
STEP 3 - create a new object window (to navigate to) and open that, then close current window
private void Button_Click(object sender, RoutedEventArgs e)
{
NewWindow page2= new NewWindow();
page2.Show();
this.Close();
}
You should be able to move between different pages back and forth

How to use camera capture event of a form in another form?

I am creating a new UI and I want to use the camera capturing event in the form1 on form2(in short, i am trying to transfer data from pictureBox1 of Form1 to pictureBox1 of Form2.) How can i achieve that?
Thanks...
In form 2, implement a public method to accept an image and display in the picture box
In form 1, in the PAINT event of the picture box, call the above method of Form 2
Sample code
Form 1
public partial class Form1 : Form
{
//Object of Form 2 in Form 1
Form2 oFrm2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Load an image in the picture box
pictureBox1.Load(#"E:\Temp\SmartRetire.jpeg");
}
private void Form1_Load(object sender, EventArgs e)
{
//Consume the PAINT event of the picture box to notify a change in image
pictureBox1.Paint += PictureBox1_Paint;
//Show a blank form2
oFrm2.Show();
}
//Raised when the picture box image is changed
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
//Show the image in form 2
oFrm2.ShowImage(pictureBox1.Image);
}
}
Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//Public method called from Form1 when the image is changed in picture box (in form1)
public void ShowImage(Image oImage)
{
//Display the picture in form2
pictureBox1.Image = oImage;
}
}

Position form where the last form used was positioned

Let's say I have 2 movable forms and when I click a button in one form (the fist one which has it's start position CenterScreen) it brings me to another one (but the second one has still centerscreen start position) and let's say I move the first form before I press the button and when I click the button I want to position the new form where I moved my first form... how can I do this?
use this following code,
set StartupPosition = Manual then the Left and Top values (Location) set via the this code
Code for the Form1
using System;
using System.Windows.Forms;
namespace PositioningCs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myButton_Click(object sender, EventArgs e)
{
Form2 frm2=new Form2();
frm2.setLocation(this.Top,this.Left);
frm2.show();
}
}
}
Code for the Form2
using System;
using System.Windows.Forms;
namespace PositioningCs
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private int top_val=0;
private int left_val=0;
public void setLocation(int top_val,int left_val)
{
this.top_val=top_val;
this.left_val=left_val;
}
private void Form2_Load(object sender, EventArgs e)
{
this.Top = top_val;
this.Left = left_val;
}
}
}
Remember the position of a form after it's moved and re-use it later.
private static Point location = null;
private void MyForm_Move(object sender, EventArgs e)
{
location = this.Location;
}
private void MyForm_Load(object sender, EventArgs e)
{
if (location != null) { this.Location = location; }
}
you can relate position with position of parent form...
//if "this" is Form1, in the button event to call Form2...
...
Form2.Position = this.Position;
Form2.ShowDialog(this);
in this link you can find more details: Link
i hope this help you

Passing variables from form to form then creating new instance of class with passed data [duplicate]

I have a form called form1 with controls which are created during run-time.
When I press a button on the Form another Form loads called combat and form1 is hidden so that only 1 form (combat) is visible.
When I press a button on combat I want my form1 form the be shown. However I can't access it.
Here is what I've tried:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat(this);
this.Hide();
combat.Show();
}
public partial class Combat : Form
{
public Combat(Form form)
{
InitializeComponent();
form.Show();
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
You need to store the parent form in a field so that you can access it outside the constructor.
public partial class Combat : Form
{
private form1 form; // Or whatever class you form1 is supposed to be
public Combat(Form form)
{
InitializeComponent();
this.form = form;
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
It's generally not advisable to pass an instance of a parent form to a child. In this case (as is often true) the code is actually simpler when you don't:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat();
this.Hide();
combat.ShowDialog();
this.Show();
}
If you need to show the parent form before the child form is closed then you can do so through events:
in Combat add:
public event Action MyEvent; //TODO rename to a meaningful name
Fire the event in the button click handler:
private void button1_Click(object sender, EventArgs e)
{
MyEvent();
}
And then have your main form add a handler to the event:
private void combatBtn_Click(object sender, EventArgs e)
{
Combat combat = new Combat();
this.Hide();
combat.MyEvent += () => this.Show();
combat.Show();
}

Copying data from form1.richTextBox to form2.richTextbox

I am making a Time clock for fun and to learn C#.
I have the time down, and the start, stop, clear.
However I am having issues with a "Notes" section. Ideally I'd like to be able to write notes into a field, and have an "Edit" button to allow the user to open a window for more options relating to text editing. (with the text from the Form1 rich text box)
My issue comes from copying the data from one form to another.
Here is the 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 PunchOut
{
public partial class PunchOut : Form
{
public PunchOut()
{
InitializeComponent();
}
int i = 0;
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
i++;
TimeSpan t = TimeSpan.FromSeconds(i);
textBox2.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
t.Hours,
t.Minutes,
t.Seconds);
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox2.Text = ("00:00:00").ToString();
}
private void button6_Click(object sender, EventArgs e)
{
}
public void button4_Click(object sender, EventArgs e)
{
new Form2().Show();
richTextBox1.Text = Form2.richTextBox1.Text;
}
}
}
Here is the Form2 code:
namespace PunchOut
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.Text = PunchOut.richTextBox1.Text;
}
}
}
Currently, I get an error that states:
an object reference is required for the non-static field, method, or property 'PunchOut.PunchOut.richTextBox1'
and
an object reference is required for the non-static field, method, or property 'PunchOut.Form2.richTextBox1'
Why do I get these errors?
lots of unneeded work going on there. I hope I explained this well enough
Breakdown:
We add a String Member Variable so that we can put the contents of a RichTextBox into a string and pass that instead of using the RichTextbox control.
We change the constructor to take a string parameter which is the RTF text that you want to change. Now Form2 can change any RTF text and not be specifically tied to just the richTextbox1 on the punchoutForm.
We then change the updating of the member variable to when the form is closing otherwise you are changing it with each keystroke which is a lot of unnecessary method calls.
namespace PunchOut
{
public partial class Form2 : Form
{
public String richText;
public Form2(String rText)
{
InitializeComponent();
this.richTextBox1.Rtf = rText;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
richText = this.richTextBox1.Rtf;
}
}
}
Then in your button4_Click. Use Rtf to include the codes otherwise just use a plain textbox
Now in your button click event handler we create a new form and assign it to a variable. We then call showdialog. The reason for showdialog is that this will make the form the top most form so that a user cannot go back to the punchout form and make a change to the richtextbox which would then make the text in the Form2 obsolete as it would no longer represent the correct RTF text in the punchout form. When the user is done editing the text and closes the form we then request the edited rtf text by accessing the richText Member Variable of Form2. The reason you can access this after the form has closed is that the form is not disposed until the method returns, your local variable lives within the scope of the method.
public void button4_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(richTextBox1.Rtf);
f2.ShowDialog();
richTextBox1.Rtf= f2.richText;
}
You didn't make a new reference to PunchOut in Form2. In your form 2 add this under the class declaration:
PunchOut punchOut;
And you get in Form2:
namespace PunchOut
{
public partial class Form2 : Form
{
PunchOut punchOut;
public Form2(PunchOut PUNCHOUT)
{
punchOut = PUNCHOUT;
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.Text = punchOut.richTextBox1.Text;
}
}
}
In the original replace button4_CLick with:
public void button4_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
richTextBox1.Text = form2.richTextBox1.Text;
}
EDITED: You should pass the old PunchOut instead of creating a new one. I've updated the code.
You could store the text in a more accessible string. Then have your other form call that string.
public string yourText;
// down further
yourText = textBox1.Text;

Categories