I have a server-client application, but my task now is to fill a square of a rectangle when I click it. How can I do this?
using System;
using System.Windows.Forms;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.ComponentModel;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Client
{
public partial class Form1 : Form
{
int xStart = 20, yStart = 20;
int lungCasuta = 20;
int nrCasute = 10;
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;
public Form1()
{
InitializeComponent();
}
private delegate void LetsDraw();
private Graphics m_FormGraphs;
private const int m_iSquare = 8;
private int iCol = 0, iRow = 0, iX = 30, iY = 80;
private const int m_iWidth = 50, m_iHeight = 50;
private void Draw()
{
string[] abc = new string[20] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
string[,] sCoordinates = new string[20, 20];
for (int i = 0; i <= 19; i++)
{
if (i == 20)
{
iCol = 0;
iRow += 1;
}
sCoordinates[iRow, iCol] = abc[i];
iCol += 1;
}
float fLocal = (float)60;
float fX = (float)iX;
float fY = (float)iY;
Font font = new Font("Arial", 10);
SolidBrush sBlack = new SolidBrush(Color.Black);
Point pLocation = new Point(iX, iY);
PointF pfLocation = new PointF(fX, fY);
Pen pen = new Pen(new SolidBrush(Color.Black), 3);
iRow = 0;
iCol = 0;
pfLocation.X += 25;
pfLocation.Y = fLocal;
for (int iCount = 0; iCount < 10; iCount++)
{
m_FormGraphs.DrawString("" + (iCount + 1), font, sBlack, pfLocation);
pfLocation.X += 50;
}
pfLocation = new PointF(10, fY + 10);
for (int iCount = 0; iCount < 10; iCount++)
{
m_FormGraphs.DrawString("" + (char)(iCount + 'A'), font, sBlack, pfLocation);
pfLocation.Y += 50;
}
}
private void button1_Click(object sender, EventArgs e)
{
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + Environment.NewLine);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
}
private void button2_Click(object sender, EventArgs e)
{
readData = "Conected to Chat Server ...";
msg();
clientSocket.Connect("127.0.0.1", 8888);
serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox3.Text + Environment.NewLine);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
Thread ctThread = new Thread(getMessage);
ctThread.Start();
}
private void getMessage()
{
while (true)
{
string bytesFrom = "";
string dataClient = null;
NetworkStream networkStream = clientSocket.GetStream();
StreamReader reader = new StreamReader(networkStream);
serverStream = clientSocket.GetStream();
int buffSize = 1024;
byte[] inStream = new byte[10025];
if (serverStream.CanRead)
{
serverStream.Read(inStream, 0, buffSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
readData = "" + returndata;
msg();
}
}
}
private void msg()
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(msg));
else
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + readData;
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
Pen blackpen = new Pen(Color.Black, 1);
SolidBrush sBlack = new SolidBrush(Color.Black);
Font font = new Font("Arial", 10);
Graphics g = e.Graphics;
for (int i = 0; i <= 10; i++)
{
g.DrawLine(blackpen, xStart, yStart + i * lungCasuta, xStart + nrCasute * lungCasuta, yStart + i * lungCasuta);
g.DrawLine(blackpen, xStart + i * lungCasuta, yStart, xStart + i * lungCasuta, yStart + nrCasute * lungCasuta);
}
for (int i = 0; i < 10; i++)
{
PointF pfLocation = new PointF(xStart - 18, yStart + lungCasuta / 4 + i * lungCasuta);
g.DrawString("" + (i + 1), font, sBlack, pfLocation);
pfLocation = new PointF(xStart + lungCasuta / 4 + i * lungCasuta, yStart - 18);
g.DrawString("" + (char)(i + 'A'), font, sBlack, pfLocation);
}
g.Dispose();
}
private void panel2_MouseClick(object sender, MouseEventArgs e)
{
textBox2.Text = (char)('A'+Math.Abs((e.X - xStart) / this.lungCasuta) )+ "," + Math.Abs(1+(e.Y - yStart) / this.lungCasuta);
DrawRectangle(xStart, yStart, (Math.Abs((e.X - xStart) / this.lungCasuta)), (Math.Abs(1 + (e.Y - yStart) / this.lungCasuta)));
txtXStart.Text =Convert.ToString(xStart);
txtYStart.Text = Convert.ToString(yStart);
txtX.Text = Convert.ToString(Math.Abs((e.X - xStart) / this.lungCasuta));
txtY.Text = Convert.ToString(Math.Abs(1 + (e.Y - yStart) / this.lungCasuta));
button1.PerformClick();
}
private void DrawRectangle(int XStart, int YStart, int XFinal, int YFinal)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(XStart, YStart, XFinal, YFinal));
myBrush.Dispose();
formGraphics.Dispose();
}
private void btnDraw_Click(object sender, EventArgs e)
{
DrawRectangle(xStart, yStart, Convert.ToInt16(txtX.Text), Convert.ToInt16(txtY.Text));
}
}
}
Here is a photo with my server-client application:
http://s9.postimg.org/3q3ff8ilb/tscr.jpg[
^]
Sorry for the long code, but I posted it all to see everything.
Please help me out!
Many thanks!