Sign in to follow this  
precious roy

code access problem

Recommended Posts

Hi I was hoping some on can help me with this problem. I want to access the (SetText) from a different class the (SetText) puts text in to a text box. now I open a new thread in that class and it is suppose to insert some text in to my text box. But I can access it, I can call my (SetText) public void but I sit cant access and if I call it public static void then my (SetText) cant access the textbox any more
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        delegate void SetTextCallback(string text);
        private Thread demoThread = null;
        private Thread demoThread2 = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void ClientButton_Click_1(object sender, EventArgs e)
        {
            this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
            this.demoThread2 = new Thread(new ThreadStart(TestInsert.InsertTest));
            this.demoThread.Start();
            this.demoThread2.Start();
        }

        private void ThreadProcSafe()
        {
            //this.SetText("This text was set safely.");
            SetText("This text was set safely.");
        }

        public void SetText(string text)
        {
            if (this.ClientTextBox.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else

            {
                ClientTextBox.AppendText(text + "\n");
            }
        }
        public static void test(string mm)
        {
            //I can’t access SetText from here
        }
    }
    class TestInsert
    {
        public static void InsertTest()
        {
            Form1.test("test");
            //I can’t access SetText from here
        }
    }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this