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 ????
}
}
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?
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);
}
}
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;}
Related
Hello I hope you can help me, I've been trying like more than 10 days to solve this problem.
I have a Form Application with 1 usercontrol and 1 Class and need use my class instance created in Form1 inside the UserControl1. (With Form1 to Form2 it's works fine)
Class CMensaje:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public class CMensajes
{
private string mensaje;
public CMensajes()
{
}
public string Mensaje { get => mensaje; set => mensaje = value; }
}
}
UserControl1:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class UserControl1 : UserControl
{
CMensajes mensajito;
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = mensa;
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = mensajito.Mensaje;
}
}
}
Form1
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class Form1 : Form
{
CMensajes mensajito = new CMensajes();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
mensajito.Mensaje = textBox1.Text;
UserControl1 usercontrol1 = new UserControl1(mensajito);
}
}
}
The problem is that it's works but intermediately I start to get
The variable mensajito is either undeclared or was never assigned. When I open Form1 Design. When I do the same code with Form1 to Form2 everything perfect!!
Really I need to pass a instance of my class serialport but is the same Here I write only a test code to understand what could I do?
Thanks.
Error Screen when I try to open Form1.designer.cs
For me you should do this inside the User Control constructor:
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = new CMensajes();
mensajito = mensa;
}
Haven't got a machine in front of me to test this but at the risk of sounding silly, try adding a default constructor to your user control.
public UserControl1()
{
InitializeComponent();
}
Or try moving the instantiation of your class inside the Form constructor.
I wanted to pass data to form1 from another class but I fail to do so.
What I have tried is
class other_class {
public a_function() {
Form1 form1 = new form1();
form1.something("Lorem Ipsum");
}
}
While on the form1 is simply
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
other_class a = new other_class();
a.a_function();
}
public void something(string str) {
//Pass to Another generic function
another_function(str);
}
public void another_function(string str) {
//Do update Textbox and RichtextBox Fields
}
}
However, it seemed that the functions within form1 is not callable from another class. What's the best fix or an alternative for this?
I solved my own question thanks to: https://social.msdn.microsoft.com/Forums/vstudio/en...
class other_class {
public Form1 form;
public other_class(Form1 frm) {
form = frm;
}
public void a_function() {
form.something("Lorem Ipsum");
}
}
and
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
other_class a = new other_class(this);
a.a_function();
}
I realized that I don't have to reinitiate the form1 since it's running already
Following code will be helpful to you,
other_class.cs :
class other_class
{
//Create a constructor in order to initialise the class
public other_class()
{
}
public void a_function()
{
Form1 form1 = new Form1();
form1.something("Lorem Ipsum");
form1.Show();
}
}
Form1.cs :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void something(string str)
{
another_function(str);
}
public void another_function(string str)
{
//Added a label to Form1.cs
//Set its text here
label1.Text = str;
}
}
and I have created another form Form2 in order to initialize other_class and call other_class's a_function() method from there as follows,
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//Added a button to Form2.cs
//Set a button click event
private void button1_Click(object sender, EventArgs e)
{
other_class obj = new other_class();
obj.a_function();
}
}
I have two forms in one of my projects. In form1 I have a dataGridView and In form2 I have 4 TextBoxes.
in Form1, I want to get a value in a variable from a datagridview using CellMouseClick event and then pass it to a TextBox in Form2
I have tried this.
form1 # it give me an Error
public form(int id)
{
int x;
x = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
and what iam suppose to do in the form2
With a constructor you can construct a type with the given prerequisites for construction.
If that means an integer, then so be it:
public MyForm(int id) {
SomeIdProperty = id;
}
And instead of var form = new MyForm();, do:
var form = new MyForm(idOfTheRelevantThing);
Then show it.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm2 = new Form2(dataGridView1.Rows[0].Cells[0].Value.ToString());
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2(string s)
{
InitializeComponent();
textBox1.Text = s;
}
}
If you are showing the Form2 from Form1, you can pass the value using the constructor. Something like this:
class Form2 {
public string Value { get; set; }
public Form2(string value) {
Value = value;
}
public void Form2_Load() {
textBox1.Text = Value;
}
}
and do this (inside Form1.cs):
Form2 f = new Form2("the value here");
f.ShowDialog(); //or f.Show();
I've got two forms and a custom class. I have populated a listbox in form1 using my custom class which holds several data types. I want to pass each of those values in the class located in the listbox to individual text boxes in form2. I'm having trouble figuring out how to access the individual values in each listbox instance of my class and then split them among the text boxes in form2. I thought I was on the right track by creating a property on form2 for my first textbox. I only have the one property set up right now because I wasn't sure it would work and was only testing. In form1 I was trying to set it up so I could access my class values from the selected item.
Form 1
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
DialogResult result = editProperties.ShowDialog();
object employeeSelect = lstBoxEmployees.SelectedValue;
editProperties.TextFirstName = Convert.ToString(employeeSelect);
}
form 2
public partial class frmProperties : Form
{
public string TextFirstName
{
get { return txtFirstName.Text; }
set { txtFirstName.Text = value; }
}
public frmProperties()
{
InitializeComponent();
}
}
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}
Form 2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}
I have two forms in one of my projects. In form1 I have a dataGridView and In form2 I have 4 TextBoxes.
in Form1, I want to get a value in a variable from a datagridview using CellMouseClick event and then pass it to a TextBox in Form2
I have tried this.
form1 # it give me an Error
public form(int id)
{
int x;
x = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
and what iam suppose to do in the form2
With a constructor you can construct a type with the given prerequisites for construction.
If that means an integer, then so be it:
public MyForm(int id) {
SomeIdProperty = id;
}
And instead of var form = new MyForm();, do:
var form = new MyForm(idOfTheRelevantThing);
Then show it.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm2 = new Form2(dataGridView1.Rows[0].Cells[0].Value.ToString());
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2(string s)
{
InitializeComponent();
textBox1.Text = s;
}
}
If you are showing the Form2 from Form1, you can pass the value using the constructor. Something like this:
class Form2 {
public string Value { get; set; }
public Form2(string value) {
Value = value;
}
public void Form2_Load() {
textBox1.Text = Value;
}
}
and do this (inside Form1.cs):
Form2 f = new Form2("the value here");
f.ShowDialog(); //or f.Show();