I want to make an object that stores a reference to another object. I have a code like this:
public partial class Form1 : Form
{
int test = 1;
store st;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
st = new store(test);
}
private void button1_Click(object sender, EventArgs e)
{
test = 7;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = Convert.ToString((int)st.o);
}
}
public class store
{
public object o;
public store(object obj)
{
o = obj;
}
}
If I click button2 - I can see "1" in my label. But if I click button2 and then button1 - I still see "1". How should I alter my code so I'll see "7" in that case?
When you create the store object you're evaluating the value of the test variable, and storing that value rather than the test variable. If you want to have a way of evaluating the variable to its value later, you can use a lambda to close over the variable, since closures in C# close over variables, not values.
public partial class Form1 : Form
{
int test = 1;
Store<string> store;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
store = new Store<string>(() => test.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
test = 7;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = store.GetValue();
}
}
public class Store<T>
{
private Func<T> function;
public Store(Func<T> function)
{
this.function = function;
}
public T GetValue()
{
return function();
}
}
Note that I made a few changes to the names of items to be in line with standard C# conventions, rather than having Store expose the generator function's field publicly, I provide a function that lets you get the value, and I've also made it generic, rather than using object, as this both avoids boxing, and prevents the need to cast the object returned from the store.
With minimal changes, here you go.
Properties and constructor of Form1:
public Form1()
{
st = new store(test);
}
private int testPrivate { get; set; }
public int test
{
get { return testPrivate; }
set
{
testPrivate = value;
st.o = value;
}
}
public store st { get; set; }
The store class:
public class store
{
public object o { get; set; }
public store(object obj)
{
o = obj;
}
}
Related
So I'm making this small program for my assignment at university and I'm finding it hard to add to my list in my form. Here is my code:
public partial class WorkOutBeam : Form
{
Check checkLib;
public BindingList<ListBox> list;
public WorkOutBeam()
{
InitializeComponent();
}
public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName();
forceItem.Show();
}
public void AddToForceList(string name)
{
list.Items.Add(name);
}
}
NewForceName class below:
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName()
{
InitializeComponent();
}
private void OkButton_Click(object sender, EventArgs e)
{
if (NewForceNames.Text != "")
{
ReferToLibs();
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}
private void NewForceName_Load(object sender, EventArgs e)
{
}
}
So I say to my program, "give me a new force." When it does, it initializes a new form of "NewForceName." I type into a text box and click 'Ok', this starts a public method shown below:
The list is a binding list which refers to the listBox as a data source. However the program tells me that the Items part is inaccessible due to its protection but I don't know how to add it as public. I tried looking in the properties of my listBox but to no avail.
Give this a shot:
public partial class WorkOutBeam : Form
{
Check checkLib;
// public BindingList<ListBox> list; // get rid of this for now
public WorkOutBeam()
{
InitializeComponent();
}
/*public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}*/
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName(this); // pass a reference to this
// instance of WorkoutBeam
forceItem.Show();
}
public void AddToForceList(string name)
{
// we should do some more things here, but let's keep it simple for now
listBox1.Items.Add(name);
}
}
And
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName( WorkoutBeam beam ) // we take a WorkoutBeam instance as CTOR param!
{
InitializeComponent();
workoutBeam = beam;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NewForceNames.Text))
{
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
// DO NOT create new WorkoutBeams every time. Use the original.
/*private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}*/
}
Disclaimer: I did not address each and every problem in this code. This is just enough so that it should "work" as intended.
I am trying to fill the class with data and use this data anywhere else on the program, where I will need it.
I created this class:
public class id
{
private string name; // field
public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}
And in form_name I tried to fill the class this way:
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
this.Close();
}
But I don't get any results. Could someone help me to clarify this?
What I have tried:
And in another form I tried to retrieve data like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = new id();
txtrez.Text = idobje.Name;
}
You can use some global area to store and access a common variable.
For example, create a class as a central repository.
public static class Globals {
public object myObj;
}
Then assign your created object to this one on first form.
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
Globals.myObj = IDOBJE;
this.Close();
}
Access that on your second form this way.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = (id)Globals.myObj;
txtrez.Text = idobje.Name;
}
I would like to parse a variable example number from one class to another as show in the code below:
public class Gettemp // is the "public" will affect the passing of variable operation?
{
public void temp(string temp)
{
temp = "123"; // things that i wanted to pass from this class
}
}
public partial class Form1 : Form
{
private void STARTbtn_Click(object sender, EventArgs e)
{
MessageBox.Show() // i wan to show the variables here
}
}
What about making that method return the temp value :
public string temp()
{
string temp = "123";
return temp; // things that i wanted to pass from this class
}
}
private void STARTbtn_Click(object sender, EventArgs e)
{
Gettemp _GetTemp = new Gettemp();
string temp = _GetTemp.temp();
MessageBox.Show(temp);
}
My program compiles and to me it makes sense.
I want to know how to get 'name' to list in my listbox.
I'm trying to use an array of classes so I can add salesmen. A new class will be created every time a person is to be added.
This way the name is a way of calling all the data in that class.
When I execute the program everything looks like its doing what it's suppose to do but it just lists 'form1' in the list box when i press the list names button
This is what i mean:
Where am I going wrong?
SalesmanClass
namespace WindowsFormsApplication1
{
class SalesmanClass
{
private string name;
public string cNum;
public string Email;
public string address;
public string gArea;
public int tSales;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Form 1
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 w2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (w2 == null)
{
w2 = new Form2();
w2.Show();
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Object names;
names = Name;
listBox1.Items.Add(Name);
}
}
}
Form 2
//form2
namespace WindowsFormsApplication1
public partial class Form2 : Form
{
SalesmanClass[] salesman = new SalesmanClass[] { };
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length != 0)
{
for (int i = 0; i > salesman.Length; i++)
{
if (salesman[i] == null)
{
salesman[i].Name = textBox1.Text;
break;
}
}
this.Close();
}
else
{
MessageBox.Show("Please Input a Name");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
In this method:
private void button2_Click(object sender, EventArgs e)
{
Object names;
names = Name; // <--- Using this.Name, i.e. Form.Name, NOT SalesmanClass.Name
listBox1.Items.Add(Name);
}
You have accidentally used the Name property of the Form itself (which naturally is "form1").
You need to have a SalesmanClass object at this point, and use the Name property of that instead.
You don't currently have a list of salesmen in your Form1, so you will need to add one and use that.
Also, if you have a list or array of SalesmanClass objects, you should create a List<string> from them and use that to initialise the listbox, something like:
SalesmanClass[] salesmen = new SalesmanClass[] {};
// ...
List<string> names = new List<string>();
foreach (var salesman in salesmen)
names.Add(salesman.Name);
listBox1.Items.AddRange(names);
You can do this using Linq too, but I don't want to confuse you by introducing that into the mix!
In your button2_Click, you have :
names = Name;
What does this Name belong to ? I suspect it belongs to Form1, that's why it's been displaying "form1". If that's the case, you just need to get your SalesmanClass object and get the Name from it.
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)