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.
Related
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!
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;
}
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;
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.
i have 2 forms on visual studio,
form1 have textbox1.text
form2, have textbox2.text and btnSave
obs: form2 open when i click on another button on form 1:
Form new = new form2();
nova.Show();
how can i send textbox2 content from form2 to form1 (textbox1) clicking on btnSave ?
What code will be necessary inside this click button event.
Thanks
Try this please:
Step1: Create a constructor for form2 class as below:
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text = strTextBox;
}
Step2: Instantiate form2 class in form1’s button click event handler as below:
private void button1_Click(object sender, EventArgs e)
{
Form2 obj1 = new Form2(textBox1.Text);
obj1.Show();
this.Hide();
}
Create an event on your second form that can be fired when the form is saved:
public event Action Saved;
Then create a property on that form that allows the textbox's text to be accessed:
public string SomeTextValue //TODO: rename to meaningful name
{ get{ return textbox2.Text;} }
Then you need to fire off the Saved event when you save your form:
if(Saved != null)
Saved();
Then when you first create the form in Form1 attach an event handler to that event:
Form2 child = new Form2();
child.Saved += () => textbox1.Text = child.SomeTextValue;
child.Show();
Note that if you are also closing the second form right when you save it then you don't need a custom event, you can just utilize FormClosing instead.
researching i was able to make it work, lost some hours but now all is perfect, this is code that worked for me:
On form2:
public partial class form2 : Form
{
private string nome;
public string passvalue
{
get { return nome; }
set { nome = value; }
}
form2, button save:
private void btnSalvar_Click(object sender, EventArgs e)
{
passvalue = txtMetragemcubica.Text;
this.Hide();
}
on form1 (this button open form2):
private void btnMetragemcubica_Click(object sender, EventArgs e)
{
form2 n = new form2();
n.ShowDialog();
txtMetragem.Text = n.passvalue
}
Now it work this way: Open on form 1, then i click on button btnMetragemcubica and form2 open, then i insert values on different textbox and have result on txtMetragemcubica, when i click on save button (btnSalvar) it close form2 and send value to form1 in txtMetragem textbox.
Working perfect here, hope help another persons too.
Anyway thanks for all help