How can I Change something in a Form1 from a DialogBox? - c#

Lets say I have two forms ( Form1 And Form2 ).
Form1 has a PictureBox
Form2 I has an OpenFileDialog
Form1 is the main form, so when I run the project I see Form1.
How can I change the Image in the PictureBox in Form1 from Form2?

How do I pass a value from a child back to the parent form?
Basically, expose the value that gets returned in the open file dialog with some property and let the parent form grab it.

In the Program.cs file you can set any value, either FormOptions to the form's instance .
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var frm = new Form1();
// Add the code to set the picturebox image url
Application.Run(frm);
}
In addition you can add the Constructor to Form1 and pass the parameter via constructor.

Pass one form as a parameter to a constructor of the second form or add a method that passed the reference. After you have the reference to your form you can do whatever you want with the from.
Whether to share picture box as a public member is up to you. However, I would suggest making a public method SetImage() in the first form. Second form would call form1.SetImage().
[Update]
Wait, why do you need to set image from OpenFileDialog, you just need receive fileName from the dialog, and then open the file and load into the form.
Like this:
private void button1_Click(object sender, EventArgs e)
{
using (var dialog = new OpenFileDialog())
{
var result = dialog.ShowDialog();
if (result != DialogResult.OK)
return;
pictureBox1.Image = Image.FromFile(dialog.FileName);
}
}
This is code inside of Form1.
[Update]
Here is the basic idea how to access one form from another.
public class MyForm1 : Form
{
public MyForm1()
{
InitializeComponent();
}
public void SetImage(Image image)
{
pictureBox1.Image = image;
}
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2(this);
form2.Show();
}
}
public class MyForm2 : Form
{
private MyForm1 form1;
public MyForm2()
{
InitializeComponent();
}
public MyForm2(MyForm1 form1)
{
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
form1.SetImage(yourImage);
}
}

You can very simply do this.
At first change your code(in Form1) that show Form2 to looks like this:
<variable-of-type-Form2>.ShowDialog(this);
or if you dont want form2 to be modal
<variable-of-type-Form2>.Show(this);
Next when you got path to image, you can access pictureBox like this
((PictureBox)this.Owner.Controls["pictureBox1"]).Image=Image.FromFile(<filename>);
Assume that you have PictureBox on Form1 with name "pictureBox1"

Ideally, you want to structure your code in a ModelViewController pattern. Then, you simply have a reference in your model to the image in the picture box. When interacting with the OpenFileDialog in Form2, you would call your model adapter interfaces hooked into the views (Form1 and Form2) to change the image being held in the model. In short, you need a callback from the view to the model. If you don't want to redesign your code to be MVC, simply have a shared object holding the image reference that both Form1 and Form2 receive in their constructors, so they can both modify it.

Related

How to make form2 update when value changed in form1

In C# I have two forms "Form1" and "Form2". Form1 creates images that are stored in a folder. Form2 displays the number of images in that folder.
Say I have made 2 images with Form1 and then I open Form2. Form2 now says there are 2 images. Now while keeping both forms open I want to be able to add a new image and Form2 updates. At the moment if I use Form1 to add more images while Form2 is open Form2 continues to display the number of images that were in the folder when Form2 opened.
I have found solutions that involve Form2 closing and reopening but I don't want this. It's jarring for the user having windows opening and closing every time they press a button. I just want Form2 to update live as I make changes with Form1.
Any help would be greatly appreciated!! Thanks!
You can do this with public method in Form2
In Form1 you need to save Form2 object in property
FORM 1
public partial class Form1 : Form
{
public Form2 MyProperty { get; set; }
public Form1()
{
InitializeComponent();
}
// for opening form 2
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
MyProperty = f2;
}
// adding new image
private void button_ADD_Click(object sender, EventArgs e)
{
MyProperty.updateCounter();
}
}
When you add new image, then you can call the metoh from Form2 to update counter.
In FORM 2 you need crate PUBLIC method to update counter
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// default value
label1.Text = "0";
}
// update counter
public void updateCounter()
{
label1.Text = (int.Parse(label1.Text) + 1).ToString();
}
}
You may want to take a look at events and delegates. Basically, whenever you do something to Form1 that can affect other forms, you want to trigger an event. In Form2, you have a method which you call whenever that event triggers. This way you can have multiple forms open, and as long as you set them to "listen" to a given event, they can all react.
You can read more about them here, in the Microsoft Documentation.

How to manipulate label of form2 in form3

I am trying to have a button in Form3 change a label's text in Form1 and Form2.
I have gotten it to work somewhat but in order to have the label change I have to have the mouse click on it. This is my current code in form 1 and 2:
label1.Text = Form3.myNameClass.myName;
and this is code in form 3
public class tournamentNameClass
{
public static string tournamentName;
}
public void button1_Click(object sender, EventArgs e)
{
myNameClass.myName = textBox3.Text;
}
How can I make it so that I dont need to press the label to get it to change?
Since you are at the start of learning, I'm not going to go in events and delegates.
My example demonstrates how you manipulate a control on Form1 from Form2 directly. You should be able to figure this out for Form3 easily by yourself, and is together a good practice of understanding.
(I want to state that there are many different methods/techniques for passing data or manipulate controls between forms, I guess this is the easiest one for you to understand as a beginner, as this is the most simplistic approach of all)
Form1 Designer
First we make label1 its modifier to Public (so we can reach it in another class) in the designer like so:
Form1 code behind
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// With the keyword "this" we pass in Form1, the current instance we are in
Form2 form2 = new Form2(this);
form2.Show();
}
}
Form2 code behind
public partial class Form2 : Form
{
private Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_form1.label1.Text = "lets change the text";
}
}
We are passing Form1 completely in the constructor, this could easily be the label1 control alone.
Result
I think you only need to trigger a redraw on forms 1 and 2.
after you set the text of the label, try calling labelControl.Refresh()
Or you could call refresh on form1 and form2 if that's any easier (that is a bit heavy handed though)
This will instruct the forms to redraw their visual representation based on how the data is set in the form currently.
Update:
You will need to invert your logic somewhat..
Rather than having a reverence to form 3 from form 1 and form 2, you will need to have a reference to form 1 and 2 from form3.. (woah so many form)
So then your button click method would look like this
public void button1_Click(object sender, EventArgs e)
{
form1.label1.Text = textBox3.Text;
form2.label1.Text = textBox3.Text;
form1.label1.Refresh();
form2.label1.Refresh();
}

WPF + Windows Forms, open a second Form as child of MainWindow from the first Form

Probably I will not make myself clear in the title, change it if necessary. Now I step to explanation:
I have a program done in WPF (cause graphics). The MainWindow open a first Form with a button, the Form1 do some stuff, changing the UI of the MainWindow and when it is done should open a Form2 that it should also do some things changing the UI of the MainWindow, but I can't open the Form2 through Form1.
Here's some code:
MainWindow.xaml.cs
internal static MainWindow main; //It allows me to put the Form1 in the same context in order to change the UI
private void start_button_Click(object sender, RoutedEventArgs e)
{
Form1 frm = new Form1(this);
frm.Show();
}
Form1.cs
//function that allow me to communicate with the MainWindow
private MainWindow main1 = null;
public Form1(MainWindow callingForm)
{
main1 = callingForm as MainWindow;
InitializeComponent();
}
//Load function
private void Form1_Load(object sender, EventArgs e)
{
//do some stuff changing MainWindow UI
Form2 frm = new Form2(main1); //no compilation error
frm.Show();
}
Now, the Form2 does not open. I also tried to do it calling a void that is in the MainWindow with the Form1 but nothing. What I have to do? NOTE WELL: need to change the UI of the main window also with the Form2 and I need that Form2 is opened only at the end of the Form1 calculations.
public Form1(MainWindow callingForm)
{
main1 = callingForm as MainWindow;
InitializeComponent();
this.Load += Form1_Load;
}
If you are trying to open a window from the MainWindow, I would recommend you to use events to instanciate the window.
It is a bit weird from a structure standpoint to a child view to say to his parent: "Hey, I want YOU to open this form" - yet it is the Form1 that creates and shows it.
I would do something like this:
MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void start_button_Click(object sender, RoutedEventArgs e)
{
Form1 frm = new Form1();
frm.MyEvent += Form1EventMethod;
frm.Show();
}
private void Form1EventMethod(object sender, EventArgs e)
{
// You can add here what you need to change in the MainWindow.
Form2 frm = new Form2();
frm.Show();
}
}
Form1:
public partial class Form1 : Window
{
public event EventHandler MyEvent;
public Form1()
{
InitializeComponent();
}
private void show_form_Click(object sender, EventArgs e)
{
if(MyEvent != null)
MyEvent(this, new EventArgs());
}
}
Also this way, you may subscribe more than just the MainWindow if you want to do something when this event is raised. Also, you may pass differents parameters to your subscribers. Your views will be seperated and not dependant on one and another.
I know what you mean, you want to call a window form from a WPF application main window. I've prepared this test Program to help you understand the how to programmatically achieve this.
Consider the following simple program:
1) takes 2 numbers as user input (using TextBoxes).
2) displays the result in a customized window form that pops up as response to a calculate button.
When the user press the 'Calculate' button ( not show in this answer), the program adds the 2 number then displays the result in a customized window form (that acts like a dialog box ).
Assuming this is what you're looking for here's the caculateclick method that responds to the user's click :
In MainWindow.xaml.cs:
private void calculateClick(object sender, RoutedEventArgs e)
{
try
{
int leftHandSide = System.Int32.Parse(lhsOperand.Text); //user input
int rightHandSide = System.Int32.Parse(rhsOperand.Text); // lhsOperand and rhsOperand are just references to TextBoxes that takes user input
string result_txt=addValues(leftHandSide,rightHandSide).ToString(); // add two numbers that returns the result
//addValues method is not defined in my answer, it's just a method I use in this example to add 2 values together
CustomDialog.show(result_txt); // diplays result in a custom window form
}
catch (FormatException exception )
{
exception.ToString();
}
All you need to do is to create a class for the user-defined Form then inside this class define the show static method which main window is going to call from the caculateClick() method seen above ( I will show you the code just keep reading ). Notice the call to the static method 'CustomDialog.show(result_txt);'
CustomDialog is a partial class ( just like the Form1 class you created) displays the result of the addition, in a user-defined form that's going to act like a dialog box. ( I assume you know to design the user interface for the form using the design view in Visual Studio ).
Here's the code for CustomDialog:
public partial class CustomDialog : Form // CustomDialog
{
static CustomDialog MsgBox; //used in show method as a reference
static DialogResult result; //retuned by show method
public CustomDialog()
{
InitializeComponent();
result = DialogResult.OK; //initialise the result according to the function of the button, here I only use the OK button to close the dialog }
public static DialogResult show (string calculationResult) // show static method called by the Main Window
{
MsgBox = new CustomDialog();
MsgBox.result_text.Text = calculationResult; // result_text is a textbox implemented in the form using design view
MsgBox.StartPosition = FormStartPosition.CenterParent; // center dialog in the middle of the parent (WPF application main window)
MsgBox.ShowDialog(); // displays the dialog
return result; // returns the result so the button event is handled
}
We define two fields in the CustomDialog class,
static CustomDialog MsgBox; static DialogResult result; the MsgBox object is used in the static method called 'show ( string calculation )' in order to display the dialog box and return the 'result' variavle which is a DialogResult object. ( DialogResult is returned so we can see the result in the TextBox and handle event clicks ).
Important NOTE:
To avoid any errors it's crucial that you maintain the form using the following methods that does some background work and handle button clicks and label clicks on the dialog ( even if your not going to use them, you can just leave them empty). Also make sure that 'CustomDialog' and the 'MainWindow' for the WPF application is implemented in the SAME namespace
Consider adding the following methods to your CustomDialog class as good practice in case you face some errors :
private void Form1_Load(object sender, EventArgs e) // the name of this method depends on what you name it when you first create the form
{
}
private void button1_Click(object sender, EventArgs e)
{
result = DialogResult.OK; // handle result returned by show() method, here I only used an OK button
MsgBox.Close(); //closes the dialogbox
}
private void label1_Click(object sender, EventArgs e)
{
}
Design Notes: Be careful when you create the form in Design View you must assign the button you dragged into it as an Acceptbutton in the Misc section within the Properties tab in Visual Studio. Also keep in mind that I refer to MessageBox, Dialogbox and Form as the same entity. Actually if you carefully examine my code, the 'MsgBox' static variable is a CustomDialog Object.
Sorry for the long post but hope this helps ! if you need screen shots to show you that it works and if it's helpful, let me know.

Fully access a control from another Form

I have a main Windows Form (From1) which has a TextBox in it. I've also created another Windows Form (FindReplaceForm) which I'm going to use for implementing a form of find and replace dialog. I need to fully access all the properties and methods of my textbox in From1 from FindReplaceForm window.
Although I set the Modifiers property of my TextBox to Public, in FindReplaceForm window I can't access the text in it.
You can access the the owner form in the child using:
((Form1)Owner).textBox1.Text = "blah";
Assuming you have called your the child form using:
Form2 form = new Form2();
form.Show(this);
You need to pass a reference to the control or the form to your constructor so that you can reference the instance of the class. Add an argument of the same type as the calling form to the constructor:
private Form1 calling_form;
public FindReplaceForm (Form1 calling_form)
{
this.InitializeComponent()
this.calling_form = calling_form;
}
Then in your button call you can say:
calling_form.TextBox_value_text.SelectedText = "";
In your code, Form1 is a CLASS, not a variable. When you show your FindReplaceForm you should specify the Owner (just use this).
Now you can the Owner property on FindReplaceForm to get access to Form1.
Showing FindReplaceForm:
FindReplaceForm.Show(this);
In your button click event:
void Buttton1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).TextBox_value_text.SelectedText = "";
}
Even if you set the textbox access modifier back to private, you can simply pass a reference to the textbox in the second form's constructor, thus enabling the second form to manipulate any property of the textbox:
first form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this.textBox1);
frm.Show();
}
}
second form:
public partial class Form2 : Form
{
private TextBox _OwnerTextBox;
public Form2(TextBox ownerTextBox)
{
InitializeComponent();
_OwnerTextBox = ownerTextBox;
this.textBox1.Text = _OwnerTextBox.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
_OwnerTextBox.Text = this.textBox1.Text;
}
}
In your FindReplaceForm.button1_Click function you want to control an object of class Form1. The error in your code explains that you don't call a function of an object of class Form1, but a function of class Form1 itself. The latter can only be done on static functions
You describe that you have an existing Form1 object and that your FindReplaceForm needs to change Form1 by calling functions in Form1. Probably there might be more than 1 Form1 object. Therefor your FindReplaceForm instance somehow needs to know which Form1 object it needs to control. Someone needs to tell this to your FindReplaceForm. This is usually done using the constructor of the FindReplaceForm or using a property.
public class FindReplaceForm
{
private Form1 formToControl = null;
public FindReplaceForm(Form1 formToControl)
{
this.formToControl = formToControl;
}
private void OnButton1_Click(...)
{
this.formToControl.TextBox_Value_Text.SelectedText = ...
}
Another method would be not to put the formToControl in the constructor, but as a property that you can set separately. Both methods have their advantages:
via constructor: your FindReplaceForm knows for sure there is alway a formToControl. The only place you have to check if formToControl really exists is in the constructor.
Using a default constructor with a separate FormToControl property may cause run time errors if people forget to set the FormToControl.
If you have a lot of properties, or some properties may have default values, or may be changed later, then the property method is more flexible.

Visible Property of Picture box not working from another form in C#

Hi I am using windows forms in C#. I am trying to modify the visible property of a picture from main form to another. Initially, the visible property of the picture box is set to false. On a button click from another form, the visible property of the picture box is modified to true.
This is the code written in the Form2 method:
private void button_Click(object sender, EventArgs e)
{
public Form1 frm1 = new Form1();
frm1.pictureBox.Visible= true;
}
Form1 is an instance type, so when you do
public Form1 frm1 = new Form1();
frm1.pictureBox.Visible= true;
you're really just creating a new instance of Form1 completely unrelated from your original Form1, changing a picture-box's visible property on it, and then discarding it.
What you can do, is put a reference to the "parent" Form1 inside your Form2 class.
Here's an example
public partial class Form2 : Form
{
public Form2(Form1 parent)
{
InitializeComponent();
this.Parent = parent;
}
Form1 Parent;
private void button1_Click(object sender, EventArgs e)
{
Parent.pictureBox.Visible= true;
}
...
}
there you create an instance of a form :
public Form1 frm1 = new Form1();
This is then obviously NOT the form you already may have in your page, which you could simply access by its ID.
According to your written code it will create new instance of the desired form, and NOT take the existing open form. Hence to identify the existing open form containing target picture box you need the target form and controlling form be related by like Parent form or MDI Parent Form.
Assuming case of MDI Parent Form (i.e. Controlling form is MDI Parent of Target Form), you need following codes to identify to existing open form:
foreach (Form frm in MdiChildren)
{
if (frm is myTargetForm)
{
//do your code to find control using id of picture box and change the required properties
}
}

Categories