-1
TF2SelectDir.txtTF2DirSelect.Text = "";

This is giving me issues, as txtTF2DirSelect is on one form and I'm trying to change it from another. I tried looking it up, and the entire form itself is already public, not private.

Or, to go along with this, how can I create a variable that can be accessed on any form?

Where it goes wrong

if (canFindTF2 == true) { TF2SelectDir.txtTF2DirSelect.Text = "";

The form where TF2SelectDir is is already public

public partial class TF2SelectDir : Form { public TF2SelectDir() { InitializeComponent(); }

Any ideas? Thanks!!

UPDATE

At the bottom of my TF2SelectDir.Designer.cs, I've found

private System.Windows.Forms.TextBox txtTF2DirSelect; private System.Windows.Forms.Button btnSaveTF2Dir; private System.Windows.Forms.Label lblExample;

However, when I changed private to public on txtTF2DirSelect, I got a new error. "An object reference is required for the non-static field, method, or property 'TF2SelectDir.txtTF2DirSelect' - Error Code CS0120

Max Box
  • 41
  • 1
  • 10
  • _a variable that can be accessed on any form_ would most likely be a global variable [which you should avoid](http://c2.com/cgi/wiki?GlobalVariablesAreBad). A better approach to communicate between two forms would be to use events. – Jan Köhler Mar 09 '16 at 04:59
  • if (canFindTF2 == true) { TF2SelectDir.txtTF2DirSelect.Text = ""; } ---------- namespace TF2Overwatch { public partial class TF2SelectDir : Form { public TF2SelectDir() { InitializeComponent(); } etc etc @khlr Awesome, how would I do that? – Max Box Mar 09 '16 at 05:03
  • 1
    You should share your code as an edit to the question. Codes are really hard to read as comments. – Ghasem Mar 09 '16 at 05:11
  • If you search for the definition of txtTF2Select then you will find it is protected by default. – Jeroen Heier Mar 09 '16 at 05:16
  • @AlexJolig I've really not coded much of anything in c#, this is all unexplored territory to me, same with the editor. How would I change it to unprotected? And I'll edit the original question now. – Max Box Mar 09 '16 at 05:23
  • The accessibility of a control in one of its properties called 'Modifiers' to be found and set in the properties tab. It defualts to private. Additionally you stil need a reference to the other form; usually passed on when opening it or stored in a public forms hub variable. – TaW Mar 09 '16 at 15:07

2 Answers2

2

Since I cannot comment, I am posting this as an answer.

Accessing controls from a separate form, may not be the best idea. I would recommend you use properties. Here is Microsoft's definition and usage example of properties.

Another, even better way, in my opinion, to share data between two forms, is to use events. Once again, here is Microsoft's description of events.

If you need a working example of how to use either of these approaches, I would like to see your effort first and then we can build on that.

monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
  • For a working example you may have a look [here](http://stackoverflow.com/questions/27658076/i-need-to-access-a-form-control-from-another-class-c/27659346#27659346). – Jan Köhler Mar 09 '16 at 05:16
  • Man, this site truly follows "do your own darn code". May I ask, why not use a global variable? It's simply going to store a directory to a file in string form which a few other things would look at, would there be such a downside to that? – Max Box Mar 09 '16 at 05:16
  • Will this directory change, or be static, for instance, one common directory, that does not change? – monstertjie_za Mar 09 '16 at 05:22
  • @MaxBox If you really need to go the Global route, have a look here: http://stackoverflow.com/questions/14368129/c-sharp-global-variables If you can avoid this, if your directory path will always be the same, I would recommend storing it in the .config file – monstertjie_za Mar 09 '16 at 05:24
  • Once it finds the proper directory the first time, then it shouldn't change. – Max Box Mar 09 '16 at 05:33
2

Expose control in below way .. why?? @monstertjie_za provided few good links on that already .

namespace TF2Overwatch
{
    public partial class TF2SelectDir : Form
    {
        //Approch 1 - usable when the projects most works are done
        //without following a good architecture 
        //You can use a static variable to preserve the state and intilize each time
        //a new instance is created

        //Approch 2 - Responibilty of preserving text to initlize in textbox should be taken
        //by the form who calling this form        
        //value will pass by consturtor or by exposing property 

        //all approch 2 code are kept commented for better understanding

        private static string strTxtTF2DirSelectTextToInitize;

        public TF2SelectDir()
        {
            InitializeComponent();
            txtTF2DirSelect.Text = strTxtTF2DirSelectTextToInitize;
        }

        public static string TxtTF2DirSelectTextToInitlize
        {
            get
            {
                return strTxtTF2DirSelectTextToInitize;
            }
            set
            {
                strTxtTF2DirSelectTextToInitize = value;
            }
        }

        //public TF2SelectDir(string txtTF2DirSelectText)
        //{
        //    InitializeComponent();
        //    txtTF2DirSelect.Text = txtTF2DirSelectText;
        //} 


        //public string TxtTF2DirSelectTextToInitlize
        //{
        //    get
        //    {
        //        return txtTF2DirSelect.Text;
        //    }
        //    set
        //    {
        //        txtTF2DirSelect.Text = value;
        //    }
        //}

    }

    public partial class SomeAnotherForm:Form
    {
        public SomeAnotherForm ()
        {
            InitializeComponent();           
        }

        protected void InSomeAction(object Sender, EventArgs e)
        {

            if (canFindTF2 == true)
            {                 
                TF2SelectDir.TxtTF2DirSelectText = "Test";
                TF2SelectDir t1 = new TF2SelectDir();
                t1.Show();

                //Approch 2
                //TF2SelectDir t1 = new TF2SelectDir("Test");
                //t1.Show();

                //TF2SelectDir t1 = new TF2SelectDir();
                //t1.TxtTF2DirSelectText="Test"; //look here TxtTF2DirSelectText is setting on instance not by class
                //t1.Show();

            } 
        }
    }
}
Moumit
  • 8,314
  • 9
  • 55
  • 59
  • https://i.gyazo.com/fd59ddd161a56a50c751ffaf2e44b770.png https://i.gyazo.com/b39018433b2d4aa34c710bebf518f707.png That's what I've put in, is it correct? Currently, it doesn't change the text inside to "Test!" as I would want it to. Thanks for the help so far! https://i.gyazo.com/152db38f0fa03230c211ebabb3607263.png https://i.gyazo.com/aa9e8f1399c2d1bf21afac84bd5e1049.png (yes, it looks quite ugly right now xP) – Max Box Mar 09 '16 at 05:37
  • ok .. I think i got your point .. you want store the the text inside the form for future use ... like whenever you call the form it should show the text "test" in txtTF2DirSelect textbox ... right?? – Moumit Mar 09 '16 at 05:42
  • Yes! And eventually I would like to download a file and place it in another folder, reading the text to find the directory on WHERE it would drop the file, but that's the general idea. – Max Box Mar 09 '16 at 05:48