Witam. Próbuje stworzyć czat, lecz mam problem z dodawaniem wiadomości. Wiadomości maja się pojawiać w panelu jako labele
Wiadomości testowe dodane w klasie dodają się bez problemu, lecz gdy próbuje to zrobić na buttonie to jest problem i nic się nie dzieje.
Kod pojedynczej wiadomości:
class SingleMessage : Panel
{
int margin = 5;
public SingleMessage(Panel _parent, string _text, int loc)
{
Parent = _parent;
Location = new Point(margin, loc);
Width = _parent.ClientSize.Width - 2 * margin;
Height = 16;
BackColor = Color.LightGray;
Label text = new Label();
text.Text = _text;
text.Width = ClientSize.Width;
text.Height = 30;
text.Parent = this;
text.Location = new Point(0, 0);
}
}
Kod całego messageboxa
class MessagesBox : Panel
{
List<SingleMessage> listaWiadomosci = new List<SingleMessage>();
public MessagesBox(Panel _parent)
{
Parent = _parent;
Width = _parent.ClientSize.Width;
Height = _parent.ClientSize.Height;
Location = new Point(0, 0);
BackColor = Color.Gray;
AddMessage("test");
}
public void AddMessage(string _text)
{
int horizontalLocation = GetNextMessageHorizontalLocalization();
SingleMessage m = new SingleMessage(this, _text, horizontalLocation);
listaWiadomosci.Add(m);
}
private int GetNextMessageHorizontalLocalization()
{
int margin = 5;
if (listaWiadomosci.Count <= 0)
{
return margin;
}
else
{
Panel lastMessage = listaWiadomosci[listaWiadomosci.Count - 1];
int lastElementHorizontalLocalization = lastMessage.Location.Y;
int lastElementBottomLocalization = lastElementHorizontalLocalization + lastMessage.Height;
return lastElementBottomLocalization + margin;
}
}
}
Kod Buttona który nie działa
private void button1_Click(object sender, EventArgs e)
{
MessagesBox messagesBox = new MessagesBox(mainPanel);
messagesBox.AddMessage(textBoxM.Text);
if(textBoxM.Text == "")
{
MessageBox.Show("You did not type message");
}
}
Jestem nowy w c# a to pewnie jest coś prostego czego nie dostrzegam.