-1

EDITED: Apparently me not being very knowledgable in code in general at the time of making this question caused me to be banned for asking question so here I am trying to correct that. what I initially wanted back then was to reference a class, at the time I thought 2 different class files were regarded as private to another, now I know that upon compiling, all class files(or otherwise known as source files) are all binded together in the end and that what I actually needed to do was reference a class as in the variable, basically:

Form2 form2 = new Form2();

Visual Studio is rather hard to get your head around when you have no knowledge in code whatsoever and I'm sorry that I somehow was bad for the audience.

==========================================================================

I have a windows form (Form2) which is basically a dialog box with comboboxes. I want the text values from 2 comboboxes to carry over to a textbox in Form1 at the click of a button.

If I was doing this all from Form1 it would be:

Textbox1.Text += (value1) + (value2) + "\n";

but since the textbox in Form1 is private, Form2 doesn't recognize it.

My question is this: how do I get Form2 to acknowledge the existence of the textbox in Form1?

  • Which form1? You can have multiple instances of a form, you know. (This is a hint to see if you figure it out for yourself from here). – Joel Coehoorn Sep 08 '17 at 19:05
  • 1
    Generally speaking, if you want to communicate between different class instances, you would formalize that process by creating public properties or methods on those classes, rather than trying to talk to their private members directly. The same is true of forms. – Robert Harvey Sep 08 '17 at 19:06

3 Answers3

0

Make a public static class with form utilities. Create a public static property(probably string). Call that property from the other form. like so:

public static class formutilities
{
   public static string formtext { get; set; }
}

Form one sets the property:

formutilities.formtext = textbox.Text;

Form two calls it like this

textbox.Text = formutilities.formtext;
Freek W.
  • 406
  • 5
  • 20
  • Yeesh... Don't know many people that would put up with using a static class as a middle man to pass data between objects... That just seems like bad style. – Broots Waymb Sep 08 '17 at 20:29
  • Can also create a public class and instantiate it in the form. – Freek W. Sep 08 '17 at 23:01
  • Well yeah, but then you're just doing an extra step when you could just be using straight-up properties in the first place. – Broots Waymb Sep 09 '17 at 21:55
  • I feel like adding properties directly to the form is a bad style, but it comes down to preferences really. – Freek W. Sep 10 '17 at 21:46
  • Why would that be considered bad form??? That's exactly what a public property is for. That is in no way considered bad style. Creating an intermediate class for the sole purpose of information sharing on the other hand... – Broots Waymb Sep 11 '17 at 12:37
0

I would suggest to declare a public method in Form1:

public void UpdateTextBox(string value1, value2)
{
   Textbox1.Text += (value1) + (value2) + "\n";
}

and create new constructor for Form2 to pass the instance of Form1 to Form2 when called:

// Form2 class:
private Form1 _form1;
public Form2(Form1 form1)
{ 
 _form1 = form1;
}

and inside your button click event call the new method on _form1:

 _form1.UpdateTextBox(/*add here your two combo boxes values*/);
Abdullah Dibas
  • 1,499
  • 1
  • 9
  • 13
0

Meh,I pretty much found the answer in the end, after finding this video

https://www.youtube.com/watch?v=CdH8z_JNi_U

the formula pretty much translated to "ths." being equal to "form1."