Добавить класс в winforms приложение
У меня есть winform приложение клиент-серверные шашки, сейчас речь конкретно про клиента. Всё написано в одной форме, я будучи не знамо как сделать правильно, сделал следующим образом, создал ещё пару форм одна - "CheckresLogic", другая "Chat" потому что в шашках ещё есть чат. Сделал я эти формы partial и теперь у меня всё работает отлично, ну понятное дело всё компилируется в один класс, но ведь это не правильно я полагаю. Подскажите как сделать по уму? Нужно проект library в solution туда положить методы, которые в классах "CheckresLogic" и "Chat" и добавить ссылку на этот library? Но тогда у меня всё поедет, у меня много где используется класс Button, а в library Button'а не будет, как быть подскажите? Вот основная форма: (не вся)
private delegate void printer(string data);
private delegate void cleaner();
printer Printer;
cleaner Cleaner;
private Socket _serverSocket;
private Thread _clientThread;
private string _serverHost;
private int _serverPort;
public ClientForm()
{
InitializeComponent();
Printer = new printer(Print);
Cleaner = new cleaner(ClearChat);
this.Text = "Checkers";
Init();
}
private void Listener()
{
while (_serverSocket.Connected)
{
byte[] buffer = new byte[8196];
int bytesRec = _serverSocket.Receive(buffer);
string data = Encoding.UTF8.GetString(buffer, 0, bytesRec);
if (data.Contains("#updatechat"))
{
UpdateChat(data);
continue;
}
if (data.Contains("#Updateboard"))
{
UpdateBoardd(data);
continue;
}
if (data.Contains("#updateTbxClient"))
{
UpdateTbxClient(data);
continue;
}
}
}
private void UpdateBoardd(string data)
{
string coords = data;
var reg = new Regex(@"(?<open>\%).*?(?<final-open>\%)");
var matches = reg.Matches(coords).Cast<Match>()
.Select(m => m.Groups["final"].Value).ToList();
foreach (string strCoords in matches)
{
coords = strCoords;
}
string[] subs = coords.Split(',');
string ast = subs[0];
string bst = subs[1];
int a = int.Parse(ast);
int b = int.Parse(bst);
foreach (var btn in buttons)
{
if ((btn.Location.X == a) && (btn.Location.Y == b))
{
ActionsWithBoard(btn);
}
}
}
public void Print(string msg)
{
if (this.InvokeRequired)
{
this.Invoke(Printer, msg);
return;
}
if (ChatBox.Text.Length == 0)
ChatBox.AppendText(msg);
else
ChatBox.AppendText(Environment.NewLine + msg);
}
public void Send(string data)
{
try
{
byte[] buffer = Encoding.UTF8.GetBytes(data);
int bytesSent = _serverSocket.Send(buffer);
}
catch { Print("Server disconnected"); }
}
int CountUsers { get; set; } = 0;
private void enterApp_Click_1(object sender, EventArgs e)
{
string Name = tbxUserName.Text;
if (string.IsNullOrEmpty(Name)) return;
ViewFigures();
Send("#setname&" + Name);
ChatBox.Enabled = true;
chat_msg.Enabled = true;
Chat_Send.Enabled = true;
tbxUserName.Enabled = false;
enterApp.Enabled = false;
}
Вот CheckersLogic (тоже не весь)
public partial class ClientForm
{
const int mapSize = 8;
const int cellSize = 50;
int currentPlayer;
int countEatSteps = 0;
int[,] map = new int[mapSize, mapSize];
bool isContunue = false;
bool IsMoving = false;
Button pressedButton;
Button prevButton;
Button[,] buttons = new Button[mapSize, mapSize];
List<Button> simpleSteps = new List<Button>();
Image whiteFigure;
Image blackFigure;
public void SwitchControls()
{
Chat_Send.Enabled = true;
ChatBox.Enabled = true;
tbxUserName.Enabled = true;
enterApp.Enabled = true;
chat_msg.Enabled = true;
btnDisc.Enabled = true;
}
public void DeactivateButtons()
{
try
{
for (int i = 0; i < mapSize; i++)
{
for (int j = 0; j < mapSize; j++)
{
buttons[i, j].Enabled = false;
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message + "Error with Deactivate");
}
}
public void ShowDiagonal(int IcurrFigure, int JcurrFigure, bool isOneStep = false)
{
int j = JcurrFigure + 1;
for (int i = IcurrFigure - 1; i >= 0; i--)
{
if (currentPlayer == 1 && isOneStep && !isContunue) break;
if (IsInsideBorders(i, j))
{
if (!DeterminePath(i, j)) // делает поле активным и окрашивает в жёлтый
break;
}
if (j < 7)
j++;
else break;
if (isOneStep)
break;
}
j = JcurrFigure - 1;
for (int i = IcurrFigure - 1; i >= 0; i--)
{
if (currentPlayer == 1 && isOneStep && !isContunue) break;
if (IsInsideBorders(i, j))
{
if (!DeterminePath(i, j))
break;
}
if (j > 0)
j--;
else break;
if (isOneStep)
break;
}
j = JcurrFigure - 1;
for (int i = IcurrFigure + 1; i < 8; i++)
{
if (currentPlayer == 2 && isOneStep && !isContunue) break;
if (IsInsideBorders(i, j)) // выходит ли за границы доски
{
if (!DeterminePath(i, j))
break;
}
if (j > 0)
j--;
else break;
if (isOneStep)
break;
}
j = JcurrFigure + 1;
for (int i = IcurrFigure + 1; i < 8; i++)
{
if (currentPlayer == 2 && isOneStep && !isContunue) break;
if (IsInsideBorders(i, j))
{
if (!DeterminePath(i, j))
break;
}
if (j < 7)
j++;
else break;
if (isOneStep)
break;
}
}
public bool IsButtonHasEatStep(int IcurrFigure, int JcurrFigure, bool isOneStep, int[] dir)
{
bool eatStep = false;
int j = JcurrFigure + 1;
for (int i = IcurrFigure - 1; i >= 0; i--)
{
if (currentPlayer == 1 && isOneStep && !isContunue) break;
if (dir[0] == 1 && dir[1] == -1 && !isOneStep) break;
if (IsInsideBorders(i, j))
{
if (map[i, j] != 0 && map[i, j] != currentPlayer)
{
eatStep = true;
if (!IsInsideBorders(i - 1, j + 1))
eatStep = false;
else if (map[i - 1, j + 1] != 0)
eatStep = false;
else return eatStep;
}
}
if (j < 7)
j++;
else break;
if (isOneStep)
{
break;
}
}
j = JcurrFigure - 1;
for (int i = IcurrFigure - 1; i >= 0; i--)
{
if (currentPlayer == 1 && isOneStep && !isContunue) break;
if (dir[0] == 1 && dir[1] == 1 && !isOneStep) break;
if (IsInsideBorders(i, j))
{
if (map[i, j] != 0 && map[i, j] != currentPlayer)
{
eatStep = true;
if (!IsInsideBorders(i - 1, j - 1))
eatStep = false;
else if (map[i - 1, j - 1] != 0)
eatStep = false;
else return eatStep;
}
}
if (j > 0)
j--;
else break;
if (isOneStep)
break;
}
j = JcurrFigure - 1;
for (int i = IcurrFigure + 1; i < 8; i++)
{
if (currentPlayer == 2 && isOneStep && !isContunue) break;
if (dir[0] == -1 && dir[1] == 1 && !isOneStep) break;
if (IsInsideBorders(i, j))
{
if (map[i, j] != 0 && map[i, j] != currentPlayer)
{
eatStep = true;
if (!IsInsideBorders(i + 1, j - 1))
eatStep = false;
else if (map[i + 1, j - 1] != 0)
eatStep = false;
else return eatStep;
}
}
if (j > 0)
j--;
else break;
if (isOneStep)
break;
}
j = JcurrFigure + 1;
for (int i = IcurrFigure + 1; i < 8; i++)
{
if (currentPlayer == 2 && isOneStep && !isContunue) break;
if (dir[0] == -1 && dir[1] == -1 && !isOneStep) break;
if (IsInsideBorders(i, j))
{
if (map[i, j] != 0 && map[i, j] != currentPlayer)
{
eatStep = true;
if (!IsInsideBorders(i + 1, j + 1))
eatStep = false;
else if (map[i + 1, j + 1] != 0)
eatStep = false;
else return eatStep;
}
}
if (j < 7)
j++;
else break;
if (isOneStep)
break;
}
return eatStep;
}
private void ActionsWithBoard(Button pressedButton)
{
if (prevButton != null)
prevButton.BackColor = GetPrevButColor(prevButton);
if (map[pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize] != 0 && map[pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize] == currentPlayer)
{
CloseSteps();
pressedButton.BackColor = Color.Green;
DeactivateButtons();
pressedButton.Enabled = true;
countEatSteps = 0;
if (pressedButton.Text == "D")
{
ShowSteps(pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize, false);
}
else
{
ShowSteps(pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize);
}
if (IsMoving)
{
CloseSteps();
pressedButton.BackColor = GetPrevButColor(pressedButton);
ShowPossibleSteps();
IsMoving = false;
}
else
IsMoving = true;
}
else
{
if (IsMoving)
{
isContunue = false;
if (Math.Abs(pressedButton.Location.X / cellSize - prevButton.Location.X / cellSize) > 1)
{
isContunue = true;
DeleteEaten(pressedButton, prevButton);
}
int temp = map[pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize];
map[pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize] = map[prevButton.Location.Y / cellSize, prevButton.Location.X / cellSize];
map[prevButton.Location.Y / cellSize, prevButton.Location.X / cellSize] = temp;
pressedButton.Image = prevButton.Image;
prevButton.Image = null;
pressedButton.Text = prevButton.Text;
prevButton.Text = "";
SwitchButtonToCheat(pressedButton);
countEatSteps = 0;
IsMoving = false;
CloseSteps();
DeactivateButtons();
if (pressedButton.Text == "D")
ShowSteps(pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize, false);
else ShowSteps(pressedButton.Location.Y / cellSize, pressedButton.Location.X / cellSize);
if (countEatSteps == 0 || !isContunue)
{
CloseSteps();
SwitchPlayer();
ShowPossibleSteps();
isContunue = false;
}
else if (isContunue)
{
pressedButton.BackColor = Color.Red;
pressedButton.Enabled = true;
IsMoving = true;
}
}
}
prevButton = pressedButton;
}