0
public partial class Form3 : Form
{
        public Form3()
        {
            InitializeComponent();   
        }

        int port;       // I declared a variable and I wanna use this in another form like
}

// ------------------------------------------------------- //

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
            SagTikMenuOlustur();
        }

        void menu1_Click(object sender, EventArgs e)
        {
            Form2 frq = new Form2();
            frq.Show();

            MessageBox.Show("{0} server is online ",port); //How to I declare ????
        }

}
joe
  • 8,344
  • 9
  • 54
  • 80

3 Answers3

1

Set the field as public

or

Create property for that field.

This is the way you can use

Refer this link: How to access a form control for another form?

Community
  • 1
  • 1
Murugavel
  • 269
  • 1
  • 2
  • 9
0

You have to change port to public.

public partial class Form3 : Form { public Form3() {

        InitializeComponent();

    }

    public int port; <<== Change to public

or public int port {get;set;}

0

Best thing would be to create a property for it.

Try this

public partial class Form3 : Form
{
    int _port;
    public int Port
    {
       get { return _port; }
       set { _port = value; }
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
       InitializeComponent();
    }

    void menu1_Click(object sender, EventArgs e)
    {
        Form2 frq = new Form2();
        frq.Show();
        Form3 frm3 = new Form3();
        frm3.Port = 8080;

        MessageBox.Show("{0} server is online ", frm3.Port);
    }

}
yogi
  • 19,175
  • 13
  • 62
  • 92