views:

14

answers:

2

Hello! I have a main form with a textBox1 with string value : asd

namespace Crystal
{
    public partial class MainForm : Form
    {
       //here is a textBox1 with text "asd"
    }
}

I want to change this textBox1 text from this class:

namespace Crystal.Utilities
{
   public class Logging
   {
       //textBox1.Text = "dsa";
   }
}

The problem is, I cant change the value of textBox1 from the Logging class, because it doesnt exists there :/ How to do this?

A: 

You have to create a public property (supposing you're writing C#) or a method. Then access it from the other place.

namespace Crystal
{
    public partial class MainForm : Form
    {
        //here is a textBox1 with text "asd"

        public TextBox MyTextBox {
            get { return textBox1; }
        }
    }
}


namespace Crystal.Utilities
{
   public class Logging
   {
       var foo = MainForm; // Get an instance of your MainForm here somehow.
       foo.MyTextBox.Text = "dsa";
   }
}
Wernight
Iam doing this with VS 2008 form editor:The code is auto generated. Where should I insert this snippet that you gave me?private System.Windows.Forms.ListBox general_log;this.general_log = new System.Windows.Forms.ListBox();this.main_page.Controls.Add(this.general_log);this.general_log.Name = "general_log";
Dominik
Your *MainForm* class is partial. There is source code for it in 2 places. Use right click > View code.
Wernight
A: 
Oren A