Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using windows forms C#, I have 2 forms the 1st form contains a menuItem and a pictureBox, when the menuItem is clicked the 2nd form appears. The 2nd form also contains a button, I want when I click the button in the second form, a bitmap image appears in the pictureBox of the 1st form.
this is the menuItem event handler, it's supposed to open the other form and load an image in the pictureBox (when the button in the 2nd form is clicked)
private void imageToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 win = new Form3();
win.ShowDialog();
pictureBox1.Image = myNormal.bmp;
// displaying.Displaybmp(pictureBox1, b);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
'myNormal' is a class which contains bitmap image.
I don't know why the pictureBox doesn't load the image.
Create the Interface first to invoke the method in form1.
public interface IMyForm
{
void ShowImage(Image image);
}
Then implement that interface in your Form1 and assign the image to the picture box in implemented method ShowImage(Image).
public class Form1 : Form, IMyForm
{
public void ShowImage(Image image)
{
this.PictureBox1.Image = image;
}
private void menuitem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2()
frm2.Show(this); //Assign the owner as current form
}
}
Now, write some code to invoke the method in Form1. Create the object of IMyForm and assign the value from this.owner property.
//Form2
private void button1_Click(object sender, EventArgs e)
{
IMyForm frm = (IMyForm)this.owner;
frm.ShowImage(new Bitmap(#"D:\myImage.jpg"));
}
If you don't wish to you Form.Show(owner) method then you can get the available form object in button click event by Application.OpenForms[] Form Collection.
IMyForm frm = (IMyForm)Application.OpenForms("Form1");
frm.ShowImage(new Bitmap(#"D:\myImage.jpg"));
Here is a sample from msdn :
private Bitmap MyImage ;
public void ShowMyImage(String fileToDisplay, int xSize, int ySize)
{
// Sets up an image object to be displayed.
if (MyImage != null)
{
MyImage.Dispose();
}
// Stretches the image to fit the pictureBox.
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ;
MyImage = new Bitmap(fileToDisplay);
pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = (Image) MyImage ;
}
looks like you need to also provide value for ClientSize property.
please refer here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox(v=vs.110).aspx
Related
I am new to windows Forms and didn't know about user control. I have a form 1 with a label and want to change the value of the label using user control. I also called user control in form1. I tested the form function using the usercontrol_click event. It shows the message box but not changing the label value.
Form 1 user control name flowLayoutPanel1
user control name ListView
private void ListItem_Click(object sender, EventArgs e)
{
Form1 a = new Form1();
//a.emailbody();
a.label5.Text = "work";
}
public void emailbody()
{
MessageBox.Show("Welcome");
}
You are creating a new form with Form1 a = new Form1(); in the click event handler, but you never show this form using a.Show(); or a.ShowDialog().
Did you intend to set the label on the current form? If yes, then don't create a new form.
private void ListItem_Click(object sender, EventArgs e)
{
// Uses label in this form:
label5.Text = "work";
}
You simply write emailbody(); to call a method on the current form.
If you intended to set the label on another form that was already open, you need a reference to this form. E.g. you can store it in a field in the current form
private Form1 _form1;
private void OpenOtherFormButton_Click(object sender, EventArgs e)
{
_form1 = new Form1();
_form1.Show();
}
private void ListItem_Click(object sender, EventArgs e)
{
if (_form1 is not null) {
_form1.label5.Text = "work";
}
}
If you want to change the label of your user control, the label must be public. Assuming that the user control is on your form, do something like this:
myUserControl.label5.Text = "work";
If it is on another form, the user control must be public as well:
_form1.myUserControl.label5.Text = "work";
if the user control is on the panel:
flowLayoutPanel1.myUserControl.label5.Text = "work";
If the user control is on the same form as where the label is and ListItem_Click is in the user control, then you must get reference to the form first.
// In the user control
var form1 = FindForm() as Form1;
if (form1 is not null) {
form1.label5.Text = "work";
}
This question already has answers here:
How to access a form control for another form?
(7 answers)
Closed 6 years ago.
1.In c# I want to show panel in form1 when i click a button in form 2. i create a method showpanel(){ panel1.show();} in form1 in this form1 i call showpanel() it is works fine. when i call with form2 it is not work.
In Form2:
private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Form1 f1 = new Form1();
f1.showpanel();
}
In Form1:
public void showpanel()
{
panel1.Visible = true;
}
2.i also make panel public and call directly from form2 also
In Form2:
private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Form1 f1 = new Form1();
f1.panel1.Show();
f1.panel1.Visible = true;
}
but it also not work.
From form2 Before showing the form1 you need to make panel1 visible.
private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Form1 f1 = new Form1();
f1.panel1.Visible = true;
f1.panel1.Show();
}
This question already has answers here:
How to access a form control for another form?
(7 answers)
Closed 7 years ago.
I'm using WinForms. I'm have 2 forms. In form1 i have a picturebox and in form2 i have a button that prints. I'm trying to build an application that will print the images from the picturebox in Form1 using Form2.
I first tried making a panel and testing if the pictures would print in form1, and it did, but the problem is when copied the printing code to form2, the code wouldn't let me access the picturebox in form1.
How do i access the picturebox in Form1 from Form2 so i cant print the images in the picturebox.
I receive error lines under this.pictureBox1.
Form2
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.DrawToBitmap(bmp, this.pictureBox1.ClientRectangle);
e.Graphics.DrawImage(bmp, 25, 25, 800, 1050);
}
You can make your PictureBox from Form1 available to Form2 by passing a reference to it as a property.
I'm not sure how you have your forms set up for loading. But if you are using a Main form that manages Form1 and Form2 you can do something like the following. If you do not have a Main form then this should at least get you on the right track.
Form1
public PictureBox ThePicture
{
get {return this.pictureBox1; }
}
Form2
private PictureBox _thePicture;
public PictureBox ThePicture
{
set { this._thePicture = value; }
get { return this._thePicture; }
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (this.ThePicture != null)
{
var bmp = new Bitmap(this.ThePicture.Width, this.ThePicture.Height);
this.ThePicture.DrawToBitmap(bmp, this.ThePicture.ClientRectangle);
e.Graphics.DrawImage(bmp, 25, 25, 800, 1050);
}
}
Main Form
Form1 form1 = new Form1();
Form2 form2 = new Form2();
form2.ThePicture = form1.ThePicture;
Try to do so:
//Tente fazer assim:
public Form1()
{
InitializeComponent();
}
public Image getImagePic
{
get
{
return this.pictureBox1.Image;
}
}
This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 3 years ago.
First off I know there have been similar Q&A for this. I haven't seem to have found the answer I am looking for, but it's possible I missed it. Second I am new to the C# language as I've mostly worked in C++ so please forgive me if this is a stupid question.
A little background now on what I am trying to accomplish. I'm in the process of making a Paint application. The first form, Form1 as I'll call it is where all of the UI is for my application and where the user will draw. I want to allow the user to select different brush types and sizes. On Form1 I have a button which the user will click to change these options. When this button is clicked it will initiate what I'll call Form2. Form2 will have the options for brush type and size and when the user selects them and hits the OK button the size and brush type should be passed back. I am just using two int variables to hold the brush type and the brush size to keep things simple since Form1 needs to know this, not Form2.
All of the information I have found is for passing information from Form1 to Form2, when I really want to pass information from Form2 to Form1. Is there a simple way to do this? I will be passing information like this for several other buttons as well so I am hoping not to over complicate things.
Thank you very much for your time!!! :)
This is in Form1 which calls Form2
private void brushBtn_Click(object sender, EventArgs e)
{
//New form which will ask which brush type and the size
Form2 paintInfo = new Form2() ;
paintInfo.ShowDialog();
}
This is Form2
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
int typeOfBrush;
//User picked the circle brush
private void circleBrushBtn_Click(object sender, EventArgs e)
{
typeOfBrush = 1 ;
}
//User picked the square brush
private void squareBrushBtn_Click(object sender, EventArgs e)
{
typeOfBrush = 2 ;
}
private void okBtn_Click(object sender, EventArgs e)
{
//PASS THE BRUSH TYPE & SIZE BACK TO FORM1 WHEN USER HITS OK BUTTON
this.Close() ;
}
}
Usually this kind of program has a tool palette that contains the various tools.
The palette is non modal, meaning that you don't block the execution of code in the first form when you show the palette. It stays in some corner and you click over the icons in the palette to change your behavior.
But, if this is the case, then passing back information to form1 requires a delegate and an event handler
So, inside Form2 code you have
public enum ToolEnum
{
Draw = 1,
Rubber = 2,
Fill = 3,
......
}
public class Form2
{
public delegate void OnToolChanged(ToolEnum newTool);
public event OnToolChanged ToolChanged;
....
protected override palette_Click(object sender, EventArgs e)
{
// Code that retrieves the tool clicked....
ToolEnum choosenTool = GetTool();
// If someone is interested to know when the user changes tool
// then it has subscribed to the ToolChanged event passing an
// appropriate event handler
if(ToolChanged != null)
ToolChanged(choosenTool);
}
}
The form1 call the form2 in this way
// Open the form with the palette passing reference to this instance of form1.
// Important to link the two forms together....
Form2 f = new Form2(this);
// Now inform the form2 instance that this instance of form1
// wants to know when the user clicks another tool
f.ToolChanged += myToolChangedHandler;
....
// We receive here the notification of the click event on the palette form
public void myToolChangedHandler(ToolEnum newTool)
{
if(newTool == ToolEnum.Fill)
{
... adapt for the fill operation ...
}
}
EDIT
However, if you want the follow the simpler road of showing modally your Form2, then the code is easy
private void brushBtn_Click(object sender, EventArgs e)
{
//New form which will ask which brush type and the size
using(Form2 paintInfo = new Form2())
{
paintInfo.ShowDialog();
int brushType = paintInfo.BrushType;
}
}
....
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
int typeOfBrush;
public int BrushType
{
get {return typeOfBrush;}
set {typeOfBrush = value;}
}
private void circleBrushBtn_Click(object sender, EventArgs e)
{
this.BrushType = 1 ;
}
}
Don't overthink this. A form is just an object. From within Form1 you will create a new object of type Form2. Form2 can have whatever properties (two ints in your case) that can be set however you wish. Assuming you are using WinForms, you will probably want to show Form2 via ShowDialog() which is a blocking call. When ShowDialog() returns, you can then interrogate Form2 (you still have a handle to the object in Form1) about any of its properties.
If this is not enough to get you started, I'm sure someone else will post an entire coded solution.
I'm having trouble displaying a PictureBox in C#. I have two forms. In my main form I'm calling the other form, where the PictureBox is located.
This is how I am calling the second form:
frmODeck oDeck = new frmODeck();
oDeck.Show();
Now, this is my second form, where the PictureBox is located from main form
namespace Shuffle_Cards
{
public partial class frmODeck : Form
{
private PictureBox picBox;
private Image image;
public frmODeck()
{
InitializeComponent();
}
private void frmODeck_Load(object sender, EventArgs e)
{
image = Image.FromFile("C:\\C2.jpg");
picBox = new PictureBox();
picBox.Location = new Point(75, 20);
picBox.Image = image;
picBox.Show();
}
public void getCards()
{
}
}
}
What am I doing wrong, or what am I missing?
Thanks
The picture-box control needs to be added to the control-collection of the top-level control it should belong to - in the case, the form itself. Relevant: Control.Controls.
Replace:
picBox.Show();
with:
Controls.Add(picBox);
Befor you do a picBox.Show(); , you need to add it to the Controls of the window you are loading, with the code #Ani provided:
Controls.Add(picBox);
That should do it!