changing label from program.cs  
Author Message
Deerhake44





PostPosted: Visual C# General, changing label from program.cs Top

ok well i have a listener thread in program.cs that listens for messages from my NetworkStream and then needs to acces a label on my Form1

ive tried

Client.Form1 test = new Client.Form1();

test.label1.Text = data;

Client being my application name



Visual C#7  
 
 
ahmedilyas





PostPosted: Visual C# General, changing label from program.cs Top

whats the problem you are having

from the info given, you wish to access or set text of the label on your form from the thread if so you would need to have some reference/access to the instantiated form from your thread listener and as well as this you would need to invoke the UI component as controlling a UI component from a thread which it was not created from would throw an exception (illegal thread cross operation) And to do this:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=564275&SiteID=1

you would also need to make your label public so that you can access it as by default controls are private meaning on the class that has this control can access it and no one else



 
 
RizwanSharp





PostPosted: Visual C# General, changing label from program.cs Top

lable1 is probably a label in Form1 class and it cannot be access here because it has by default private modifier.

The best way to do it to create a property in Form1 Class like this:

public string SetLabelText

{

get { return this.label1.Text; }

set { this.label1.Text = value; }

}

Put the above code in Form1 class

And access it from other class like this:

Client.Form1 test = new Client.Form1();

test.SetLabelText = data; // This will set the value of data in Form1's Label1

I hope this will work!

Best Regards,



 
 
Deerhake44





PostPosted: Visual C# General, changing label from program.cs Top

doesnt work either
 
 
ahmedilyas





PostPosted: Visual C# General, changing label from program.cs Top

what doesnt work What problems are you having

 
 
Deerhake44





PostPosted: Visual C# General, changing label from program.cs Top

what RizwanSharp said didnt work

i didnt understand what u said


 
 
ahmedilyas





PostPosted: Visual C# General, changing label from program.cs Top

Rizwan pretty much constructed the way you can set text of a label from a thread, and the link I supplied shows you how to invoke the UI component so you can set the label text safely from another thread

So in your example, you would do this:

go into your client form. View the code.

Create this property:

public string SetTextLabel

{

get

{

return this.theLabel.Text;

}

set

{

this.theLabel.Text = value;

}

}

From your program.cs:

Client.Form1 test = new Client.Form1();

test.SetTextLabel = "This label was set from another class";

test.Show();

you could also set the label in the other form as public than private (all controls are private by default) but this would be bad design and should be using the above approach



 
 
RizwanSharp





PostPosted: Visual C# General, changing label from program.cs Top

what RizwanSharp said didnt work

i didnt understand what u said

Hello Deerhake,

My solution didnot work and what Ilyas said you didnot understand that. now the only solution remains is that you post piece of your code here, So one can fix that and explain that what you were doing wrong and what is the right way to do that thing because are seemed to be a newbie and you didnot understand what Cross Thread Exception means.

Try to get some C# Book and Read it from Chapter First to Last.

Best Regards and Best of Luck to You!

Rizwan



 
 
Deerhake44





PostPosted: Visual C# General, changing label from program.cs Top

ok heres my code

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Client
{
    public class Program
    {  
       
        static void Main()
        {
            listner list = new listner();
            Thread trd = new Thread(list.starter);
            trd.Start();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

           
        }

    }
}
class listner
{
    public static NetworkStream stream;
    public void starter()
    {
        Int32 port = 7777;
        TcpClient client = new TcpClient("192.168.0.102", port);
        stream = client.GetStream();
        try
        {
            Byte[] bytes = new Byte[256];
            String data = null;
            while (true)
            {
                data = null;
                int i;
                while (stream.DataAvailable)
                {
                    i = stream.Read(bytes, 0, bytes.Length);
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                }
                if (!(data == null))
                {
                    Client.Form1 test = new Client.Form1();

                    test.change(data);

                }
            }
        }
        catch (SocketException a)
        {
            Console.WriteLine("SocketException: {0}", a);
        }
    }
    public static void sendMessage(string message)
    {
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

        stream.Write(data, 0, data.Length);
    }
}
           

and

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Client
{

    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        public string SetLabelText
        {

            get { return this.label1.Text; }
            set { this.label1.Text = SetLabelText; }
        }

 

        private void button1_Click(object sender, EventArgs e)
        {
            listner.sendMessage(textBox1.Text);
           
        }

       

        private void change(string data)
        {
            string test = data;
            label1.Text = test;
        }
    }
}


 
 
ahmedilyas





PostPosted: Visual C# General, changing label from program.cs Top

sorry but its still not clear what doesnt work and what you are expecting to do.

From the code, all I can see is that its creating and starting up a new thread to listen to a port for incoming data. I see that you are creating ANOTHER instance of form1/client form in there and changes the label. This wont quite work. you need a reference of an EXISTING instance of the client/form1. This could be declared as a global variable in the listener class which you then give it when you are creating the listener class. So....

in your listener class add this as a global variable:

 

private Form1 theMainForm = null;

public Form1 TheMainForm

{

   get { return this.theMainForm; }

   set { this.theMainForm = value; }

}

 

then in your program.cs/main, do this:

Form form1 = new Form();

listner list = new listner();

listner.TheMainForm = form1

Thread trd = new Thread(list.starter);

trd.Start();

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(form1);

 

once done, back into your listner class, do this:

if (!(data == null))

{

this.theMainForm.change(data);

}

 

you also need to make change a public method, not private, so change private to public