I need to change the textbox value from another class, but I can’t do it. Searching in Google realized that this can be done using events or by changing the textbox access modifier in properties to public, and then creating a form object in the class and changing the textbox value through it, which did not work for me. Help please solve the problem.
Option 1:
public class ClientObject { public delegate void MethodChatLog(string message); public event MethodChatLog writeInChatLog; ... public void Process() { Form1 form1 = new Form1(); try { Stream = client.GetStream(); // Получаем имя пользователя userName = GetMessage(); string message = userName + " вошел в чат."; // Рассылаем сообщение о входе в чат всем подключенным пользователям server.BroadcastMessage(message, Id); writeInChatLog(message); ... public partial class Form1 : Form { ClientObject clientObject = new ClientObject(); static ServerObject server; static Thread listenerThread; public Form1() { InitializeComponent(); try { server = new ServerObject(); listenerThread = new Thread(new ThreadStart(server.Listen)); listenerThread.Start(); // старт потока } catch (Exception exc) { server.Disconnect(); } clientObject.writeInChatLog += MessageChatLog; } public void MessageChatLog(string message) { chatLogTB.Text += message + "\r\n"; } } Option 2:
... public void Process() { Form1 form1 = new Form1(); try { Stream = client.GetStream(); // Получаем имя пользователя userName = GetMessage(); string message = userName + " вошел в чат."; server.BroadcastMessage(message, Id); form1.chatLogTB.Text += message; // ничего не изменяет ...