3 Replies - 826 Views - Last Post: 02 May 2009 - 09:17 PM Rate Topic: -----

#1 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Passing data?

Post icon  Posted 01 May 2009 - 12:20 PM

Hi everybody!

I'm new to a C# and also to Visual Studio .NET, so please bear with me. I'm having a problem, despite reading this tutorial.
I have two forms in main form (Form1) I open a serial port for which data I want to get from the second form (Form2). In this form I select between COM1 and COM2 with a Radio button. So basically what I need to do Is pass string from Form2 to Form1 in to the method init_serial_port(string PORT) which is located in Form1. Also when I open a Form2 the right Radio button has to be set to true.
What I already coded:


Form1 Code:


		public Form1()
		{
			InitializeComponent();
		}

		public void init_serial_port(string PORT)
		{
			if (!(serialPort1.IsOpen))
			{
				serialPort1.PortName = PORT;
				serialPort1.BaudRate = 115200;
				serialPort1.Parity = System.IO.Ports.Parity.None;
				serialPort1.Handshake = System.IO.Ports.Handshake.None;
				serialPort1.DataBits = 8;
				serialPort1.StopBits = System.IO.Ports.StopBits.One;
				serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
				serialPort1.Open();
			}
		}

		private void selectCOMPortToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Form2 COMS = new Form2();
			COMS.ShowDialog();
			COMS.Dispose();
		}



Form2 Code:

 
	public partial class Form2 : Form
	{
		string PORT = string.Empty;
		public Form2()
		{
			InitializeComponent();
		}

		private void ok_Click(object sender, EventArgs e)
		{
			if (radioButton1.Checked)
			{
				PORT = "COM1";
			}
			else
			{
				PORT = "COM2";
			}
			Form1 glavni = new Form1(PORT);
			this.Close();
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Passing data?

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Passing data?

Posted 01 May 2009 - 12:59 PM

*EDITTED*

What you will want to do is make PORT a public variable in Form2 that will hold which port you selected

Then delete this from Form2

Form1 glavni = new Form1(PORT)


After you return from Form2 you can read the variable from Form2 in Form1 using:

yourVariable = Form2.PORT;


*added*

but do this before you dispose the form. ;)

This post has been edited by SixOfEleven: 01 May 2009 - 01:03 PM

Was This Post Helpful? 1
  • +
  • -

#3 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: Passing data?

Posted 01 May 2009 - 01:40 PM

Thenks it works great. I also had to add static before
string PORT

Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Passing data?

Posted 02 May 2009 - 09:17 PM

View Postwuu, on 1 May, 2009 - 12:40 PM, said:

Thenks it works great. I also had to add static before
string PORT


I know why you had to add static. I should have said:

youVariable = form2Name.PORT;



If you make something public static in a class, I believe you can access it the other way, using the class name. :)

This post has been edited by SixOfEleven: 02 May 2009 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1