i want to transfer my code to a winform.

i know how to create buttons, text,,etc... and how to operate them.

The problem is that i made my code in a windows application, and i want to transform it to window form application.. i dont know how to copy and paste the whole code, cause in the window form application there is no main method..

here is my code that i try to transfer to winfrom:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
       
            string name2 = "";
            while (true)
            {
                Console.WriteLine("Enter your name and press submit");
                name2=Console.ReadLine();
                if (name2 != "")
                {
                    sw.WriteLine(name2);
                    break;
                }
            }
            Console.WriteLine("Loop is over");
            Thread t2 = new Thread(Reader);
           
            t2.IsBackground = true;
            t2.Start(connection);

            while (true)
            {
                sw.WriteLine(Console.ReadLine());
                sw.Flush();
            }
        }
  


        public static void Reader(object o)
        {
            TcpClient con = o as TcpClient;
            if (con == null)
                return;
            StreamReader sr = new StreamReader(con.GetStream());
            while (true)
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine();
                }

                Console.WriteLine(sr.ReadLine());
                Console.WriteLine();
            }
        }
    }
}

Here is the winform that i have created:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

      
    }
}

hey I believe there is no difference between winform application and windows form application. Is there any ? The first code portion in your thread is of Console application that's why it has Main(string[] args) method within a cs file.

In the Windows form or winform application, there is program.cs file which has Main( ) method without any argument.

Now what you want if some one click on Button on your Winform application ?

i want to transfer my code to a winform.

i know how to create buttons, text,,etc... and how to operate them.

The problem is that i made my code in a windows application, and i want to transform it to window form application.. i dont know how to copy and paste the whole code, cause in the window form application there is no main method..

here is my code that i try to transfer to winfrom:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
       
            string name2 = "";
            while (true)
            {
                Console.WriteLine("Enter your name and press submit");
                name2=Console.ReadLine();
                if (name2 != "")
                {
                    sw.WriteLine(name2);
                    break;
                }
            }
            Console.WriteLine("Loop is over");
            Thread t2 = new Thread(Reader);
           
            t2.IsBackground = true;
            t2.Start(connection);

            while (true)
            {
                sw.WriteLine(Console.ReadLine());
                sw.Flush();
            }
        }
  


        public static void Reader(object o)
        {
            TcpClient con = o as TcpClient;
            if (con == null)
                return;
            StreamReader sr = new StreamReader(con.GetStream());
            while (true)
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine();
                }

                Console.WriteLine(sr.ReadLine());
                Console.WriteLine();
            }
        }
    }
}

Here is the winform that i have created:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

      
    }
}

how do i pass my variables e.g. string name2.. to the text functions..so that each variables will appear in the text function?!?!?

how do i interact between the winform and my code?

Where is text function in your code ? can you please explain in detail ?

how do i pass my variables e.g. string name2.. to the text functions..so that each variables will appear in the text function?!?!?

how do i interact between the winform and my code?

i know how to create buttons, text,,etc... and how to operate them.

If you know all that, it should be simple.
A Console.WriteLine could be translated to a Label
A Console.ReadLine could be translated to a TextBox

To tell you the truth, it doesnt work like that.

if need a bunch of text to appear on the label, and a bunch of text on the side and a button..

it is all a mess. i dont know how to incorporate the first and the second code i posted above

okay , i am having a look at the code you sent me now

okay , i am having a look at the code you sent me now

How is this code so far?

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;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TcpClient connection;
        StreamReader sr;
        StreamWriter sw;

        public Form1()
        {
            InitializeComponent();


        }
        private void connectServer()
        {
             connection = new TcpClient("127.0.0.1", 5000);
             sr = new StreamReader(connection.GetStream());
             sw = new StreamWriter(connection.GetStream());
        }
  


   

        private void button1_Click(object sender, EventArgs e)
        {
            RichTextBox box1 = new RichTextBox();
            box1.Text = Console.ReadLine();
            sw.WriteLine(box1.Text);
            sw.Flush();

        }

        private void label1_Click(object sender, EventArgs e)
        {
            Label lbl1 = new Label();
            lbl1.Text = sr.ReadLine();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Do not use the Console class in a WindowsFormsApplication. See line 42 of your code.
A TextBox or RichTextBox if you want has a Text property. Work with this property to do anything you want.

the user will be typing in the Richbox. how do i obtain that data and send it by StreamWriter?

i have changed the last part of my code to this!!:

private void button1_Click(object sender, EventArgs e)
        {
     
         // i want to get the information the user types from the richTextBox.
// then i want to send it to the server by writeline() function
           
                sw.WriteLine(richTextBox1.Text);//nullpointer exception is thrown and i dont know why!?!?!?
                sw.Flush();
           
            

        }

        private void label1_Click(object sender, EventArgs e)
        {
// here i try to obtain what the server wrote me back.. and print it on the big label on my winform
            label1.Text = sr.ReadLine();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Mitja, i know how to build a console application.. i have one that works.
what i need is to put it into that partial class so that it will print me the messages sent and received on a window in the winform

Debug line 8 of your last code.
Or richTextBox1.Text is null, or sw is null.
You can only know this by checking it.

okay here is my modified code

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TcpClient connection;
        StreamReader sr;
        StreamWriter sw;
        public Form1()
        {
            InitializeComponent();
            try
            {
                connection = new TcpClient("127.0.0.1", 5000);
                sr = new StreamReader(connection.GetStream());
                sw = new StreamWriter(connection.GetStream());
            }
            catch
            {
                MessageBox.Show("Couldnt form a connection");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                sw.WriteLine(richTextBox1.Text);
                sw.Flush();
                richTextBox2.Text = sr.ReadLine();
            }
            catch
            {
                MessageBox.Show("Cant Type Message");
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {
        }
    }
}

now i dont think there is the problem that there was before. the richtext2 should receive the input from the server and print it. when i press the button everything freezes.

This is the code of the server (i dont remember if i have already posted it):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 5000);
            server.Start();
            Console.WriteLine("Server started");
            string word = "";
            savedObject saved = new savedObject();

            while (true)
            {
                TcpClient connection = server.AcceptTcpClient();
                Console.WriteLine("connection accepted");
                ThreadPool.QueueUserWorkItem(saved.ProssecClient, connection);
            }
        }
    }
}





class savedObject
{
    string word;

    public string AllWords(string sit)
    {

        word += sit + " ";

        return word;
    }


    public string word2()
    {
        return word;
    }
    StreamReader[] sr1 = new StreamReader[5];
    StreamWriter[] sw1 = new StreamWriter[5];
    int m ;

    int a;
    public void ProssecClient(object o)
    {

        string word2 = "";
        TcpClient connection = o as TcpClient;
        if (connection == null)
        {
            return;
        }

        StreamReader sr = new StreamReader(connection.GetStream());

       sr1[a++] = new StreamReader(connection.GetStream());
       sw1[m++] = new StreamWriter(connection.GetStream());


        try
        {
            while (true)
            {
               int  i = 0;

                word2 = AllWords(sr.ReadLine());
// the part below should print to all the TCP connections. 
                for ( i = 0; i < 3; i++)
                {
                    if (sw1[i] != null)
                    {
                        sw1[i].WriteLine(i+" " +word2);

                        sw1[i].Flush();

                    }

                }  

            }
        }
        catch { Console.WriteLine("client left"); }
    }

}
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.