'I am making a chat app in c# wpf and it is doing something strange

So basically i just add a label to a panel once i get a message and im tracking the y axis of the previous message and im adding 35 to it every time a new message is detected and i use that variable as the y axis for each label, it worked fine so far but after i added a scroll bar it is acting wierd, the numbers are as i expected, but the messages are put so far apart its hard to see. This is my code:

using Networking;
using System.Net;
using System.Threading;
using System.ComponentModel;

namespace socket
{
    public partial class Form1 : Form
    {
        string name = string.Empty;
        BackgroundWorker threader = new BackgroundWorker();
        bool connected;
        Connector client;
        int CurrentMsgY;
        public Form1()
        {
            Form prompt = new Form();
            prompt.MaximumSize = new Size(500, 200);
            prompt.MinimumSize = new Size(500, 200);
            prompt.Width = 500;
            prompt.Height = 200;
            prompt.Text = "Enter a nickname";
            Label textLabel = new Label() { Left = 50, Top = 20, Text = "Nickname: " };
            TextBox inputBox = new TextBox() { Left = 50, Top = 40, Width = 400 };
            Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
            confirmation.Click += (sender, e) => { prompt.Close(); };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(inputBox);
            prompt.ShowDialog();
            name = inputBox.Text;
            InitializeComponent();
            connected = false;
            client = new Connector(Dns.GetHostByName(Dns.GetHostName()).AddressList[1]);
            threader.DoWork += RecvMsg;
            threader.WorkerReportsProgress = true;
            threader.ProgressChanged += OnChange;
        }
        void OnChange(object sender, ProgressChangedEventArgs e)
        {
            displayMsg((string)e.UserState);
            groupBox1.VerticalScroll.Value = groupBox1.VerticalScroll.Maximum;
        }
        void RecvMsg(object sender, DoWorkEventArgs e)
        {
            while (connected)
            {
                string msg = client.receive(1024);
                if (msg == null) return;
                Console.WriteLine(msg);
                threader.ReportProgress(percentProgress: 0, userState: msg);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Console.WriteLine("Connecting to host");
            client.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1], 1111);
            client.send("{\"name\": \"" + name + "\"}");
            connected = true;
            CurrentMsgY = 0;
            groupBox1.AutoScroll = false;
            groupBox1.VerticalScroll.Enabled = true;
            groupBox1.VerticalScroll.Visible = true;
            groupBox1.HorizontalScroll.Visible = false;
            groupBox1.HorizontalScroll.Enabled = false;
            groupBox1.AutoScroll = true;
            threader.RunWorkerAsync();
            if (client.receive(1024) == "test")
            {
                Console.WriteLine("connected succesfully");
            }
            
        }
        
        private void label1_Click(object sender, EventArgs e)
        {

        }
        void displayMsg(string msg)
        {

            Label msgLabel = new Label()
            {
                Text = msg,
                Parent = groupBox1,
                Location = new System.Drawing.Point(10, CurrentMsgY),
                //Size = new System.Drawing.Size(new System.Drawing.Point(0, 15)),
                Font = new Font("Segoe UF", 12f, FontStyle.Bold, GraphicsUnit.Point),
                AutoSize = false,
                Size = new Size(groupBox1.Width, 40)
                

            };
            CurrentMsgY += 35;
            label1.Text = Convert.ToString(CurrentMsgY);
            msgLabel.Show();
        }

        private void Send_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == "disconnect__=230") client.send("disconnect__=23"); 
            else if(textBox1.Text != "disconnect__=230") client.send(textBox1.Text);
            
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            client.send("disconnect__=230");
            connected = false;
            threader.WorkerSupportsCancellation = true;
            threader.CancelAsync();
            client.Disconnect();
        }
    }
}

look at the label in the bottom



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source