change text of textfield from another static class - c#

Here is my code.
Main class
namespace winapp
{
public partial class Main_Form : Form
{
//Here is textfield1
public Main_Form()
{
InitializeComponent();
}
}
the other class
namespace winapp
{
public partial class second: Form
{
static Main_Form main_form = new Main_Form();
string sss = "12345";
public second()
{
InitializeComponent();
}
private void but_Click(object sender, EventArgs e)
{
//I want to change the text from here
main_form.textbox1.text = this.sss;
}
Like above, I want to change the text of a textField in the main class with second class.
But my the text of textfiled does not change.
Regurd if anyone can help me.

The issue is that you're creating a brand new Main_Form. Instead you can pass your instance of Main_Form to second:
public partial class Second : Form
{
private readonly Main_Form _mainForm;
public Second(Main_Form mainForm) {
_mainForm = mainForm;
}
}
I would then create a property on MainForm to access the textbox:
public ... Main_Form : Form
{
public string MyText {
get { return textbox1.Text; }
set { textbox1.Text = value; }
}
}
I'm not sure where you create your second form but it would now look like this as long as you're creating it in Main_Form:
Second secondForm = new Second(this);
And then second can access Main_Form's MyText property which will set the textbox.

Related

How pass a class instance from Form1 to UserControl correctly?

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.

Passing Data to Form from another class

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();
}
}

Passing data between two wpf forms as strings

trying to get data from the main form to form 2. The main form has a textbox
and a button. when the button is pressed it opens form 2 which will display the data entered in the main form as a series of text blocks.
However I cant get the data to transfer between the forms. the code is bellow.
can anyone help or suggest anything I can do differently?
WPF 1 main form:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenForm_Click(object sender, RoutedEventArgs e)
{
//btnset: Takes the values contained in the text boxes and updates
//the student class
//properties.
Student.sFname = firstname.Text;
Student.sSname = secondname.Text;
Window1 details = new Window1();
details.Show();
}
WPF 2 code:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void details_Load(object sender, EventArgs e)
{
Fname.Text = Student.sFname;
Sname.Text = Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
There are a number of ways to "pass data" between 2 classes. The easiest way is to expose property or method on Window1 and just set the text you need passed. Another way is to create a constructor on Window1 that takes in the data as parameters. Here is code that demonstrates these approaches.
public class Program
{
public static void Main(string[] args)
{
var c1 = new Class1();
c1.DoStuff();
}
}
public class Class1
{
public void DoStuff()
{
var c = new Class2("stuff");
var c2 = new Class2();
c2.AcceptStuff("stuff2");
c.Print();
c2.Print();
c2.MyData = "stuff3";
c2.Print();
}
}
public class Class2
{
private string _myData;
public Class2()
{
}
public Class2(string myData)
{
_myData = myData;
}
public string MyData
{
set { _myData = value;}
}
public void AcceptStuff(string myData)
{
_myData = myData;
}
public void Print()
{
Console.WriteLine(_myData);
}
}
Prints
stuff
stuff2
stuff3
I assume you have a class in MainWindow like:
`Public class Student
{
public static string sFname;
public static string sSname;
}`
When you click open button you are assigning values to those variable, but if you want to access them in another window mention the window name and then class name.
Check this code if its working?
`public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void details_Load(object sender, EventArgs e)
{
Fname.Text = MainWindow.Student.sFname;
Sname.Text = Mainwindow.Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}`

C# Pass selection from listbox in form1 to textbox in form2

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;
}
}

No errors, label seems like it should change to the indicated text but its not

Can anybody tell me why this is doing nothing? pcNameLabel.Text is supposed to be changing to bob when StatTransfer() is called by FighterButtonClick. According to the debugger everything is working right.
I've taken out some extra variables and stuff unrelated to the problem at hand.
public partial class MainForm : Form
{
public static string VariableLabel1;
public static string Variable2;
Random _r = new Random();
public MainForm()
{
InitializeComponent();
}
void CLoop()
{
while(true)
{
SetInfo();
}
}
public void SetInfo()
{
this.pcNameLabel.Text = VariableLabel1;
}
void ChClassButtClick(object sender, EventArgs e)
{
CharStats form = new CharStats();
form.Show();
}
}
This is a seperate windows form window.
public partial class CharStats : Form
{
public CharStats()
{
InitializeComponent();
}
void StatTransfer()
{
MainForm Mform = new MainForm();
MainForm.VariableLabel1 = "Bob";
Mform.SetInfo();
}
void FighterButtonClick(object sender, EventArgs e)
{
Fighter();
StatTransfer();
}
}
In these lines
void StatTransfer()
{
// This is a new instance of MainForm, not the original one
MainForm Mform = new MainForm();
MainForm.VariableLabel1 = "Bob";
Mform.SetInfo();
}
you create a new instance of MainForm and this instance is never displayed. This hidden instance contains the label that you are trying to change, but you cant't see it.
The simplest workaround to the problem is to pass the calling instance of MainForm to CharStats form when you initialize it
void ChClassButtClick(object sender, EventArgs e)
{
CharStats form = new CharStats(this);
form.Show();
}
Now you should change the constructor of CharStats to receive the passed instance and save it in a global variable inside the CharStats class
public partial class CharStats : Form
{
private MainForm _callingForm;
public CharStats(MainForm callingForm)
{
InitializeComponent();
_callingForm = callingForm;
}
.....
And use this saved instance where you need it
void StatTransfer()
{
_callingForm.VariableLabel1 = "Bob";
callingForm.SetInfo();
}
}
EDIT By the way, you dnn't need to use static variable for this to work. Simply change the method MainForm.SetInfo to receive a string and pass Bob when you call it
public void SetInfo(string newText)
{
this.pcNameLabel.Text = newText;
}
From CharStats
void StatTransfer()
{
callingForm.SetInfo("Bob");
}
MainForm is not set to display anywhere. I believe you want to add it to your CharStats form like so:
void StatTransfer()
{
MainForm Mform = new MainForm();
MainForm.VariableLabel1 = "Bob";
Mform.SetInfo();
this.Controls.Add(Mform);
}

Categories

Resources