C# passing data between forms without ShowDialog() - c#

I have a MainForm which has a Textbox and a Button in it. Then I have a second form with a single button. On program start, the MainForm is initialized and when I click the button, the second form shows up (ShowDialog()) still keeping open the MainForm.
So I have these two forms opened next to each other. The thing I want is, that when I click the button, the button will send a string to the MainForm. MainForm will take the text and display it in it's textbox. But I want to make the change happen immediately - without hiding and showing the MainForm again. Sort of like refresh it, when the button on the second form is pressed.
How can I do that?
Note: It's important to have the text, which is send to the MainForm, have declared in the second form. (In my program, the text is dynamically being changed on the second form level)

Try sending the TextBox to the constructor of the second form and the second form, when you give click the button, change the Text property of the TextBox and it will appear as if it will be updated as they are referring to the same place.
public partial class Form1 : Form
{
public Form1(TextBox txt)
{
InitializeComponent();
this.txt = txt;
}
//variable
TextBox txt = null;
private void button1_Click(object sender, EventArgs e)
{
txt.Text = "Your text";
}
}

If I correctly understand, you need create a property in the winform.
ex:
public partial class frmLogin : Form
{
public bool LoggedIn
{
get { return loggedIn; }
}
public frmLogin()
{
InitializeComponent();
}
}
// Now, in your forms, you can do.
frmLogin frm = new frmLogin ();
frm.ShowDialog();
var value = frm.LoggedIn;

Related

How to let the program detect the previous form where a new form was accessed so that the input from the new form is transferred to that previous form

I have multiple forms (e.g. Form1, Form2) that both contains a button that opens another form (Form3). In Form3 (pop-up form), the user is prompted to pick among the options, and once these were submitted through a button in Form3, the selected options will be transferred to the previous form where it was opened (either form1 or form2). Both forms1 and 2 are linked to one input form3, so im thinking of using a conditional statement upon clicking the "Submit" button in Form 3 that will determine whether the active form/currently maximized form is Form1 or Form2, and will let the program redirect and transfer the data accordingly to the specific form.
In maximized Form1 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form1
In maximized Form2 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form2
private void button1_Click(object sender, EventArgs e)
{
if (Form1.ActiveForm != null)
{
Form1.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form1.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
else if (Form2.ActiveForm != null)
{
Form2.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form2.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
}
It shows the output for Form1, but for Form2 there's no output. I tried placing Form2 in the first condition (if) and that works but not for Form1 this time. Apparently, what comes first is the only condition performed by the program, and the else if is not executed.
I tested if (Form1.Visible = true) works, but I've already tried and there was an error in the program. Should there be additional declarations or such or perhaps a new public class?
You're thinking about it backwards. Instead of "pushing" from Form3 back to Form1 or Form2, you should "pull" from Form3 directly from within the code in either Form1 or Form2. You can do this by showing Form3 with ShowDialog(), which will cause execution in the current form (Form1 or Form2j) to STOP until Form3 is dismissed. In Form3, you can make public properties that can be accessed to retrieve the values from it.
For example, here's a boiled down Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public String LabQuantity
{
get
{
return label8.Text;
}
}
public double AmountwFee
{
get
{
return Convert.ToDouble(label12.Text) + 100;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Then in either Form1 or Form2, you'd do something like:
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
if (f3.ShowDialog() == DialogResult.OK) // code STOPS here until "f3" is dismissed!
{
// ... do something with the data from "f3" ...
Console.WriteLine(f3.LabQuantity);
Console.WriteLine(f3.AmountwFee);
}
}
Thank you for all the suggestions! I'm learning a lot from the feedback because I'm only new to this as a student. Since coding is still very complicated for me, I decided to learn how to duplicate forms and proceeded to make a separate form3 for both form1 and form2. They were the common "copy, paste" techniques plus the renaming of forms, but it worked well in the end.
I will try to implement the suggestions the next time we have a coding assignment. Thank you!

How do I pass the text of a textbox from a pop-up window to the main form?

My program is basically reading barcodes, but I want to allow users to enter barcode manually, so I created a pop up window for that. After entering the barcode, I want the pop up window to disappear and send data to the main form when ENTER is hit, but I have no clue how this data can be passed to the main form.
Ok, this is pretty straightforward, I'll show you with an example. You have two forms: Form1 mainForm and Form 2 subForm.
mainForm calls subForm as follows:
using (Form2 subForm = new Form2())
{
if (subForm.ShowDialog() == DialogResult.OK)
{
string my_text = subForm.TextToReturn;
// Do stuff with my_text
}
}
In SubForm you will have something like this declared in the class scope:
public string TextToReturn;
private void button1_Click(object sender, EventArgs e)
{
TextToReturn = text_box.Text;
this.DialogResult = DialogResult.OK;
}

C# Changing Button Text Automatically from another Form

I'm creating a Windows application. On the Form I have 3 Buttons that will become user configurable from a Setup Menu. I have created a Second Form as a Popup where the user can add the relevent information for each button. When they Click "Done" on this Popup form I want to update the Button Text on Form 1.
public string ButtonVNC1Text
{
get
{
return btn1VNC.Text;
}
set
{
this.btn1VNC.Text = value;
}
}
Then on Form 2 when the Done Button is pressed I have the Following Code.
private void btn1VNCSetup_Click(object sender, EventArgs e)
{
//Collect Entered Data
VNCVars.VNC1Description = txtVNC1Des.Text;
//Update Button Text
BespakHMI main = new BespakHMI();
main.ButtonVNC1Text = txtVNC1Des.Text;
//Save the Data that has been entered into the Setup Fields for VNC1/2/3
SaveXML.SaveData();
this.Close(); // closes the Form2 instance.
}
But when Form 2 Close's the Text hasnt been updated. If I add a button on Form 1 and do the Following then the text does change.
private void button1_Click(object sender, EventArgs e)
{
ButtonVNC1Text = VNCVars.VNC1Description;
}
Thanks in advance.....
Here
// Update Button Text
BespakHMI main = new BespakHMI();
main.ButtonVNC1Text = txtVNC1Des.Text;
you are not updating the existing BespakHMI form, but a new invisible instance.
One way to resolve is to find the existing form like this
var main = Application.OpenForms.OfType<BespakHMI>().FirstOrDefault();
if (main != null)
{
main.ButtonVNC1Text = txtVNC1Des.Text;
// ...
}
try to pass form1 in the constructor of form 2
step1 :
instead of
Form2 form2 = new Form2();
use
Form2 form2 = new Form2(this);//You give to form2 the 'address' of form1 by using 'this'
step2 :
write the following in Form2:
Form1 creator = null;
public Form2(Form1 form1)
{
creator = form1;//the 'this aka address of form1' is saved in the creator variable
}
step 3:
use
creator.btn1VNC.Text = "hello there";// sets btn1VNC in the original form to "hello there" in form 2, using the address of form 2.

Open form2 from two buttons (form1) and matching button that was clicked

I have a little issue, I have form1 in which I got button1 and button2 and I got form2 which I'm able to open with both buttons. Button1 serves as opening form2 and inserting details into SQL DB which can be seen in form1 datagridview. Button2 opens the same form2 but it selects data from form1 and automatically is filling them into textboxes in form2 - it is edit-like.
When I created the button2 (edit button) a problem occured, because form2 didn't knew from which button the was opened.
I thought that everytime I open the form2 I should pass integer so when form2 is loaded it should decide from which button it was opened and act according to that.
Would someone help me solve this thing out?
Thanks
You need to change the constructor of your form 2 to open your form in a different "mode"
Just like this :
Form2.cs
public Form2(bool fromButton2)
{
InitializeComponent();
//Do whatever with that bool
}
And you open your form like this :
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(false);
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(true);
frm.Show();
}
Then you can apply your logic with the fromButton2 bool
personally, rather than passing the button or text or a bool I would be explicit and create an enum - pass this to the constructor so you know if you're in editing or display mode. (This covers you if new 'modes' become a requirement) E.g.
public enum EditingType
{
Display,
Editing
}
public class Form2
{
private EditingType _editingType;
public Form2(EditingType editingType)
{
_editingType = editingType;
}
public void DoSomething()
{
if (_editingType == EditingType.Display)
{
// display mode
}
if (_editingType == EditingType.Editing)
{
// editing mode
}
}
}
and to call - Form2 form2 = new Form2(EditingType.Editing);
(passing in editing or display depending on which button click you're handling)
You should create a) a new constructor, which takes button reference ( better name, or whatever You can put into their unused property "Tag" to identify them )
or b ) a public method, which You call before opening the form ( but after instantiating it ) or c) a property in form2, which can take anything You would decide to use as "thing to diffentiate.
Ok ?
Define a new constructor in form2 that takes a string, name of the calling button, as a paremeter and from buttons send the name of the button to the form2 as a parameter and in form2 check the name ButtonName pararmeter for detecting the caller button.

how to change a control property from another form

I have two forms in a windows form application. lets call them "first form" and "second form".
i want by clikcing on a button on second form, change the property of one of the controls of the first form. i've defined an event for this. by that i mean when i click on the second form's button, a method within the first form is called. here's the method:
// changes the visibility of the specified control
public void change_visibility()
{
this.new_customer_label.Visible = true;
}
but when i set a breakpoint on this method and check the value after it is executed. the property has't changed. what is wrong? thanks in advance
note: on the second form button's click event, i also close the form.
So firstly, Open up Form1.designer.cs and change the control to public
Form1 this will open Form 2.
Form2 frm2 = new Form2();
frm2.Owner = this;
frm2.Show();
Form2 this will change the property of a control in Form 1
(this.Owner as Form1).label1.Visible = true;
Here is an example of what you can do:
class Form1 : Form {
private Label labelInForm1;
public string LabelText {
get { return labelInForm1.Text; }
set { labelInForm1.Text = value; }
}
}
class Form2 : Form {
Form1 form1; // Set by the property
private Form1 Form1 {
get { return form1; }
set { form1 = value; }
}
private ChangeVisibility()
{
Form1.labelInForm1.Visible = true;
}
}
"note: on the second form button's click event, i also close the form."
Then it would probably be a better design to display the second form with ShowDialog() instead of Show(). Something like:
Form2 f2 = new Form2();
f2.ShowDialog(); // code STOPS here until "f2" is closed
this.new_customer_label.Visible = true;
By default, the designer generates code in the 'Form1.Designer.cs' class. In there, you can see that all the controls are set private, change them to public and then try again...
As you will search this problem on internet you will find different solutions but I think the best solution is to make controls public then you can access these controls from any form.
Follow these instructions.
Open your desire form whose control properties you want to access
Open Form.Designer.cs
Change the desire control class to public
Go to the main form where you want to access or change property
Write this code
Form Form2 objForm=new Form2();
Now access your control property here
objForm.new_customer_label.Visible=true;
I hope this will be helpful to you!!!

Categories

Resources