C# joystick interface

haha funny how this works :slight_smile: i was messing around with my code before you just posted, and did all of that, but it still doesn't work. it will display the X and Y axis when the form starts, but then it should write the dot to screen after splitting the incoming serial data; no dots are displayed on the screen though. ive tried display.Update(); etc and no luck. then i tried "this.Refresh();" under SetText after writing the new dot for X/Y and it works!!! see pic below.
can i make it so that it gets rid of the previous dots so theirs not dots all over?
also the x any y axis' are flipped(like left is right etc), any tips?

namespace CommunicateWithArduino
{
	public partial class Form1 : Form
	{
		public static System.IO.Ports.SerialPort port;
		delegate void SetTextCallback(string text);

		// This BackgroundWorker is used to demonstrate the 
		// preferred way of performing asynchronous operations.
		private BackgroundWorker hardWorker;

		private Thread readThread = null;

        int[] JoyData = new int[7];

        private static Bitmap bmp = new Bitmap(512, 512);
        Graphics g = Graphics.FromImage(bmp);
        private PictureBox display = new PictureBox();

		public Form1()
		{
			InitializeComponent();
			hardWorker = new BackgroundWorker();
		}

		private void btnConnect_Click(object sender, EventArgs e)
		{
			System.ComponentModel.IContainer components = 
				new System.ComponentModel.Container();
			port = new System.IO.Ports.SerialPort(components);
			port.PortName = comPort.SelectedItem.ToString();
			port.BaudRate = Int32.Parse(baudRate.SelectedItem.ToString());
			port.DtrEnable = true;
			port.ReadTimeout = 5000;
			port.WriteTimeout = 500;
			port.Open();

			readThread = new Thread(new ThreadStart(this.Read));
			readThread.Start();
			this.hardWorker.RunWorkerAsync();

			btnConnect.Text = "<Connected>";

			btnConnect.Enabled = false;
			comPort.Enabled = false;
			sendBtn.Enabled = true;
		}
		private void Form1_Load(object sender, EventArgs e)
		{
			foreach (string s in SerialPort.GetPortNames())
			{
				comPort.Items.Add(s);
			}
			comPort.SelectedIndex = 0;

			baudRate.Items.Add("2400");
			baudRate.Items.Add("4800");
			baudRate.Items.Add("9600");
			baudRate.Items.Add("14400");
			baudRate.Items.Add("19200");
			baudRate.Items.Add("28800");
			baudRate.Items.Add("38400");
			baudRate.Items.Add("57600");
			baudRate.Items.Add("115200");

			baudRate.SelectedIndex = 2;

            //draw axis lines
            g.DrawLine(new Pen(Color.Black, 2), 0, 240, 512, 240);
            g.DrawLine(new Pen(Color.Black, 2), 253, 0, 253, 512);

            PictureBox display = new PictureBox();
            display.Width = 512;
            display.Height = 512;
            this.Controls.Add(display);
            display.Location = new Point(500, 5);
            display.Image = bmp;
		}

		private void sendBtn_Click(object sender, EventArgs e)
		{
			if (port.IsOpen)
			{
				port.Write(sendText.Text);
			}
		}

		private void SetText(string text)
		{
			// InvokeRequired required compares the thread ID of the
			// calling thread to the thread ID of the creating thread.
			// If these threads are different, it returns true.
			if (this.receiveText.InvokeRequired)
			{
				SetTextCallback d = new SetTextCallback(SetText);
				this.Invoke(d, new object[] { text });
			}
			else
			{
				this.receiveText.Text += text;
				this.receiveText.Text += "\n";

                //Handle data recieved; something like: 1023,1023,1023,1,1,1,1,      
                string[] split = text.Split(new Char[] { ',' });

                int cntwrite = 0;
                foreach (string s in split)
                {
                    if (s.Trim() != "")
                    {
                        int c = Int32.Parse(s);
                        JoyData[cntwrite] = c; //adds each new value split(s) from SerRead string to JoyData array
                        ++cntwrite;
                        if (cntwrite > 6) //reset cntwrite for next time serial data is recieved
                        {
                            cntwrite = 0;
                        }
                    }
                }
                //Graph
                int x = JoyData[0] / 2;
                int y = JoyData[1] / 2;        
                // let's draw a coordinate 
                g.DrawString("•", new Font("Calibri", 18), new SolidBrush(Color.Red), x - 12, y - 30);
                this.Refresh();
            }
		}

		public void Read()
		{
			while (port.IsOpen)
			{
				try
				{
					string message = port.ReadLine();
					this.SetText(message);
				}
				catch (TimeoutException) { }
			}
		}

		private void Form1_FormClosed(object sender, FormClosedEventArgs e)
		{
			readThread.Abort();

			port.Close();
		}
	}
}

And really, Thanks alot !!