3 Replies - 1007 Views - Last Post: 30 July 2014 - 12:42 PM Rate Topic: -----

#1 Leea   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-July 14

Can't figure out how to put text into textbox

Posted 30 July 2014 - 12:30 PM

I am a newbie trying to figure it out the hard way just to get this project done.
This is a new approach to my previous question. I think everything works but the output.

In my code below the first textbox.appendtext works but the second, textbox1.appendtext(i);, gives an error;
The name 'textbox1' does not exist in the current context (CS0103).
I have tried various things and ways to address it but can't figure it out. Is it possible to put text into the textbox from this routine?

thanks
Lee


namespace textboxtest
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			textBox1.AppendText("put text in textbox\r\n");
			program2.routine1.displaycount();
		}
	}
	
	class program2
	{
		public class routine1
		{
			public static void displaycount()
			{
				for(int i = 1; i <= 5; i++)
				{
					System.Threading.Thread.Sleep(i*1000);
					// write to textbox1 here
					textbox1.appendtext(i);
				}
			}
		}
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Can't figure out how to put text into textbox

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15804
  • View blog
  • Posts: 63,309
  • Joined: 12-June 08

Re: Can't figure out how to put text into textbox

Posted 30 July 2014 - 12:34 PM

13	            program2.routine1.displaycount();

People typically create an instance of the object/class (in this case 'program2').. and the use that to interact with the class's methods, properties, functions,e tc.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Can't figure out how to put text into textbox

Posted 30 July 2014 - 12:41 PM

View Postmodi123_1, on 30 July 2014 - 07:34 PM, said:

People typically create an instance of the object/class (in this case 'program2').. and the use that to interact with the class's methods, properties, functions,e tc.

That's a static method so it isn't necessary to instantiate.

But, as the message says, your class routine1 has no knowledge of textbox1 - it lives in the Form's class. You cannot refer to textbox1 in the class routine1.
Was This Post Helpful? 0
  • +
  • -

#4 Leea   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-July 14

Re: Can't figure out how to put text into textbox

Posted 30 July 2014 - 12:42 PM

Unfortunately I am not yet good enough to do that.
My code was just an example of what I am trying to do. In the real project what I wanted to do was to have 'Program2' take over and be the running segment of the whole thing. All it will do is monitor a comm line and print input. So I was trying to do it the easy way just output text from here.
Lee
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1