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;
}
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'm trying to code a playlist class for my media player I'm working on. I want to store the file name location and a display name for a list box. The class code i have is:
class Playlist
{
private string filename;
private string displayname;
public Playlist(string strfilename, string strdisplayname)
{
this.filename = strfilename;
this.displayname = strdisplayname;
}
public string FileName
{
get
{
return this.filename;
}
set
{
this.filename = value;
}
}
public string DisplayName
{
get
{
return this.displayname;
}
set
{
this.displayname = value;
}
}
}
Inside the form i have two buttons. One to add a file and another to get the file name location of it.
private void button_Click(object sender, RoutedEventArgs e)
{
Playlist playlistdata = new Playlist(#"c:\filenametest","displayname");
lstMain.Items.Add(playlistdata.DisplayName);
}
This one works fine. Now when i went to try to get info from the class from another button I'm having issues figuring out what to do. I want to try to do something like this but not sure how to go about it. Any help would be much appreciated.
private void button1_Click(object sender, RoutedEventArgs e)
{
Playlist playlistdata = lstMain.SelectedItem;
MessageBox.Show(playlistdata.FileName);
}
Try to implement the ToString() method from Playlist
class Playlist
{
...
public string ToString()
{
return this.DisplayName;
}
...
}
Instead of adding the DisplayName you can now add the Playlist object to the list:
private void button_Click(object sender, RoutedEventArgs e)
{
Playlist playlistdata = new Playlist(#"c:\filenametest","displayname");
lstMain.Items.Add(playlistdata);
}
Now you can access the data:
private void button1_Click(object sender, RoutedEventArgs e)
{
Playlist playlistdata = (Playlist)lstMain.SelectedItem;
MessageBox.Show(playlistdata.FileName);
}
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;
}
}
I'm trying to pass value from one form to another in winforms.
On my main form I have btnAddNewRecord and dataOptions combobox.
User should first select from combobox(dataOptions) and than click on btnAddNewRecord.
I want to pass this user selected value from dataoptions combobox to new form, so I tried like this
MainForm
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
var formAddRecord = new FormNewRecord();
formAddRecord.ShowDialog();
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
var formAddRecord = new FormNewRecord();
formAddRecord.SelectedDataOptions = data.ToString();
}
FormNewRecord.cs
public string SelectedDataOptions {get; set;}
private void FormNewRecord_Load(,,,,,)
{
txtSelectedDataOptions.Text = SelectedDataOptions;
}
no error on build, but on debugging txtSelectedDataOptions is not populated with passed value. What I'm doing wrong here?
Thanks
You are creating two different instances of FormNewRecord. Make formAddRecord as private field and show it on button click.
FormNewRecord formAddRecord = new FormNewRecord();
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
formAddRecord.ShowDialog();
}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
formAddRecord.SelectedDataOptions = data.ToString();
}
Well, formAddRecord should be a private field of your class, not a var redeclared in each method !
(Method btnAddNewRecord_Click has no ideas of variables declared in Method dataOptions_SelectedIndexChanged, by the way you create different instances).
So
private FormNewRecord formNewRecord_ = new FormNewRecord();
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
formNewRecord_ .ShowDialog();
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
formNewRecord_.SelectedDataOptions = data.ToString();
}
I don't think new a form instance in another form is a good way, a better way you can set the data you want to pass as public in the parent form, and when you show the child form, set the parent form as child's owner, then you can get and use the data in the child form.
set the data as a public property in the parent form, like this: Main Form:
public string passData = "";
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
var formAddRecord = new FormNewRecord();
formAddRecord.ShowDialog(this); //important
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
passData = data.ToString(); //store the selected value to passData
}
2.get passed data from child's owner:
FormNewRecord.cs
private void FormNewRecord_Load(,,,,,)
{
if(this.Owner != null)
{
MainForm mf = (MainForm)this.Owner;
txtSelectedDataOptions.Text = mf.passData;
}
}
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.