I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)
Related
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();
}
}`
There are only (3) things I need to know in here.
Firstly, there is only (1) button (close_button) in this form. Here's my frm_main.cs code:
public partial class frm_main : Form
{
public_class pc = new public_class();
public frm_main()
{
InitializeComponent();
this.Load += new System.EventHandler(frm_main_Load);
}
private void close_button_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_main_Load(object sender, EventArgs e)
{
pc.screen_adjust(close_button);
}
}
And here's my public_class.cs code:
public partial class public_class
{
public int cl_b;
public void screen_adjust(Button b)
{
cl_b = frm_main.ActiveForm.Width;
frm_main.ActiveForm.Width = Screen.PrimaryScreen.Bounds.Width;
frm_main.ActiveForm.Height = Screen.PrimaryScreen.Bounds.Height;
b.Left += frm_main.ActiveForm.Width - cl_b;
}
}
The aim of this borderless program is to auto-stretch the form to the whole screen. Now, what I'd like to learn is:
Did I do a correct "VB.net module" in C# properly?
How do I call the methods in public_class.cs without using the 'public_class pc = new public_class();' and 'pc.screen_adjust(close_button);'?
In the public_class.cs, for example, if I want to change the close_button's text, how should I do it? I can't do frm_main.close_button afterall...
Thanks!
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Authenticator at = new Authenticator();
at.validate();
}
}
public class Authenticator
{
private int num;
public bool validate()
{
if (textBox1.Text == num.ToString()) // problem #1
{
ListBox.Items.Add("Valid"); // problem #2
}
}
}
}
Hello everyone.
As you can see from the above code I am writing an application that requires a user defined class to be able to access the winforms I am working with like in the above simplified example. I'm very new to C# so please forgive my ignorance.
I need the authenticator class to be able to access the data in a textbox and then compare it and if both strings are equal then update the listbox. Is there a simple way to do this?
Probably best to keep the gui separate from the logic:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Authenticator at = new Authenticator();
if (at.validate(textBox1.Text)) {
ListBox.Items.Add("Valid");
}
}
}
public class Authenticator
{
private int num;
public bool validate(string s)
{
if (s == num.ToString())
{
return true;
}
return false;
}
}
How about something like this?
public bool validate(string text)
{
return (text == num.ToString());
}
And calling it in your code like this. You should try to make code as reusable as possible. Which means that references to specific instances of controls on a Form is not always the best design.
if (at.validate(textBox1.Text))
{
ListBox.Items.Add("Valid");
}
Sorry I can't comment on your post, I don't have enough reputation.
I've had similar trouble once before getting a form to access a string from another form.
In my case I just had to make the string static. Try doing the same with your textbox.
public static TextBox textBox1;
public Form1()
There is another question that is very similar to mine however after reading it i still cannot get it to work.
I have two forms , MainForm and SecondForm and a few other classes, i need an instance of my AVLtree and be able to access it through my other forms.
This is what ive done so far
MainForm
public partial class MainForm : Form
{
AddArtist secondForm = new AddArtist();
public static AVLTree<Artist> treeAVL { get; set; }
public MainForm()
{
InitializeComponent();
}
private void butAdd_Click(object sender, EventArgs e)
{
secondForm.Show();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
SecondForm
public partial class AddArtist : Form
{
String Name1 = "No Name";
int Members = 0;
public AVLTreetreeAVL = new AVLTree();
public AddArtist()
{
InitializeComponent();
treeAVL = MainForm.treeAVL;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void butAdd_Click(object sender, EventArgs e)
{
Name1 = tBName.Text;
Members = (Convert.ToInt32(tBMem.Text));
Artist newArtist = new Artist(Name1,Members);
try
{
treeAVL.InsertItem(newArtist);
}
catch (Exception )
{
MessageBox.Show("No Data Entered", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
tBName.Text = "";
tBMem.Text = " ";
}
}
}
Any help would be greatly appreciated pointing out where im going wrong or how to solve it.
It now compiles however it gives an error of Object reference not set to an instance of an object. i hope ive gone about coding this is the right way.
What is the access modifier of AVLTree class? Check if it is private or internal, since your code needs it to be public.
Set public on your parametrized type
public class Artist
{
..
}
I have Form and Class like that :
namespace ALTER_Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.ALTER();
}
}
public class ALTER
{
public Form1 F;
public void ALTER()
{
F.TextBox1.Text="I Altered That";
}
}
}
So i try to call ALTER() to change the textbox1 value in Form1 but i get that error :
object reference not set to an instance of an object
That happens only if i am accessing or modifying the Form1 Controls.
And by the way i set textbox1 modifier to public
So , finally i`d like to change the control value without getting that error.
You need to assign the reference to the form. Like this:
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
A.F = this;
A.ALTER();
}
Why does your ALTER class (which isn't a great class name either) have to know about your form?
private void button1_Click(object sender, EventArgs e)
{
ALTER A = new ALTER();
this.TextBox1.Text = A.ALTER();
}
}
[...]
public class ALTER
{
public String ALTER()
{
// Do your thing
return "I Altered That";
}
}
Use these lines of code:
ALTER A = new ALTER();
A.F = this ;
A.ALTER();