Hello geeks,
this is my first post. ive gone through all the help in this forum but cant get this working. i will upload my project for you to help me. it contains a main form which calls a thread running on another class which runs a tcplistener. i want to update the value from the tcplistener in the textbox on the main form. i tried invoke begin invoke and everything.
I got into the conclusion that the problem is caused due to a new form1 is initiated which is not the main form. i tried another code from here but unable to get it working. can anyone edit the code for me pls? i need to update the value "IP" in any textbox on the main form.

here is the link
http://www.sendspace.com/file/1nhcb5
Thankyou

Recommended Answers

All 4 Replies

You need to use delegate, look at this my example:

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

namespace Feb27Exercise1
{
    public partial class Form1 : Form
    {
        System.Threading.Thread thread;
        delegate void TextBoxDelegate(string message);

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            CreatingNewThread();
            while (thread.IsAlive)
                Application.DoEvents(); //waiting for termination!
            thread.Join();  //termninate thread!
        }

        private void CreatingNewThread()
        {
            int myValue = 40; //some value to pass as parameter to the other Thread
            OtherClass other = new OtherClass(this); //passing param to constructor 
            thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(other.SomethingToDo));
            thread.Start(myValue);
        }

        public void UpdatingTextBox(string msg)
        {
            if (this.textBox1.InvokeRequired)
                this.textBox1.Invoke(new TextBoxDelegate(UpdatingTextBox), new object[] { msg });
            else
                this.textBox1.Text = msg;
        }
    }

    public class OtherClass
    {
        Form1 form1;
        public OtherClass(Form1 _form1)
        {
            this.form1 = _form1;
        }

        public void SomethingToDo(object obj)
        {
            //I will create an example loop which, 
            //from which I will pass the value to textBox on form1 class:
            int myValue = Convert.ToInt32(obj);
            for (int i = 0; i < myValue; i++)
            {
                form1.UpdatingTextBox(i.ToString());
                //wait for a bit:
                System.Threading.Thread.Sleep(200);
            }
        }
    }
}

If it does not help, let me know, and dont forget to past some of your code in here -so that I can see whats it your problem, ok?

Mitja

thanks for the reply Mitja, but im a beginner in c# and im unable to implement this in my code. i tried using delegate but i dont know how to implement it in my code. I uploaded my code in the link provided. i didnot paste it here because its a lengthy code. if posting lengthy code is not a problem, i can paste it here :)

this is the code link.
http://www.sendspace.com/file/1nhcb5

some one please help. i tried to implement the delegates invoke and all that i found in this and other forums. still i cant get it resolved.

Mitja bro thanks a million. i got it solved after 2 weeks of crazy searching. thanks the problem was exactly the calling of form1 from class. thanks a tonne >:D<

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.