• Passing variables between forms c#
    9 replies, posted
How can I easily pass a variable between a form in Visual studio in c#? I need to pass a variable from label5 in Form5 into a textbox in Form4 when I press a button. How do I do this?
Make a constructor on the form4 that accepts the variable and places it as text in the label
You could also try adding a publically accessible property on the originating form, then use an object reference to retrive it
If you're using the Windows Forms editor in Visual Studio, you can click on the textbox in Form4 and change the modifier property to Public from Private. That way you can access the text box from another class and change it's Text property value.
The problem I'm having now is that it keeps opening a new form with the variable in instead of putting the variable in the instance of form4 that is already open. The code I'm using is Form5         private void button2_Click(object sender, EventArgs e)         {             using (Form4 form = new Form4())             {                 form.matres = label5.Text;                 form.ShowDialog();             }         } Form4         public string matres { get; set; }         public Form4()         {             InitializeComponent();         }         private void Form4_Load(object sender, EventArgs e)         {             textBox3.Text = matres;         } It just keeps opening a new form4 with the variable in. Any ideas on how to instead reference an already open instance of form4? Thanks <3
Create a static class acting as a registry for open forms. Create a form subclass that registers itself on creation, and derive all your forms from that. Use this form registry system to find other open forms and pass information to them.
If you can't use the constructor (because the form is already open) just make a method on the form to pass the variable through.
You will have to keep a reference to the opened form to call a method on it
How would I go about doing this? I'm still pretty new to c# so I'm struggling and this is a last-ditch attempt to do something I've been struggling with for literal days now. Thanks for the help so far
I made a minimal example to demonstrate what you need to do: https://files.facepunch.com/forum/upload/133447/8e74e547-5273-4717-98c7-229dc1a177d9/image.png Here's the code for MainForm.cs: public partial class MainForm : Form     {         // We have to declare it as ReceivingForm (not just a normal Form)         // because we want to know of the `SetResultLabel`-method that exists         // only within our ReceivingForm code         ReceivingForm formReference;         public MainForm()         {             InitializeComponent();         }         private void openFormButton_Click(object sender, EventArgs e)         {             formReference = new ReceivingForm();             formReference.Show();         }         private void sendButton_Click(object sender, EventArgs e)         {             // If the form is not opened yet or has just been closed             if (formReference == null || formReference.IsDisposed)             {                 MessageBox.Show("Open the form first!");                 // Return out of this method so the code below is not executed.                 return;             }             // Get the value from the NumericUpDown control             int amount = (int)amountNumericUpDown.Value;             // Send the data to the other form by calling it's `SetResultLabel` method.             formReference.SetResultLabel(amount);         }     } Here's the code for ReceivingForm.cs: public partial class ReceivingForm : Form     {         public ReceivingForm()         {             InitializeComponent();         }         // `Public method` so it can be accessed from the other form         // (Could also be `internal` because we are not using it outside this project.)         public void SetResultLabel(int result)         {             // Set the incoming data onto the label (ToString it, because it's an integer)             resultLabel.Text = result.ToString();         }     } Blaim the shitty forum software if some spaces dissapeared. Good luck!
Sorry, you need to Log In to post a reply to this thread.