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);
}
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 am trying to do a big coding project however I hit a wall.
I need to show the name and score once the data has been entered in.
I tried using youtube tutorials, classes for the code. But no such luck.
Any help would be great!
form1:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.Show();
SudentBox.Items.Clear();
}
form2:
public object StudentBox { get; private set; }
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
try
{
string name = txtName.Text;
int score = Convert.ToInt32(txtScore.Text);
txtStoreScores.Text += score.ToString() + " ";
}
catch (Exception x)
{
MessageBox.Show("Please enter a number");
}
}
private void bClearScores_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtScore.Text = "";
txtStoreScores.Text = "";
}
Examples of what the forms should look like with the final result.
If I'm correct, you are trying to code a form of DialogBox.
Say, you want to get a name from the dialog (e.g. from a TextBox in Form2), you can have a model like this (in Form2 of course).
public string Name
{
//where myTextBox is the design name of your textbox
get => myTextBox.Text;
set => myTextBox.Text=value;
}
Simple Ok Button
public void OkBtnClick(object sender, EventArgs e)
{
this.Close();
}
Now, you need to actually get this info to display in your Form1. That is easy.
Just like you have started above:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.ShowDialog();
//Note that you won't be able to access form1.
SudentBox.Items.Clear();
//You can now get the name
string _nameResult=link.Name;
NameTextbox.Text=_nameResult;
}
I hope this gets you started!
You do this by using the property.
Add public static property on Form 2 and set the values of the text to the property respectively and then access them on the Form 1.
On Form 2 in the Ok button click event do this
public static string Name { get; set; }
public static string Scores { get; set; }
private void bOk_Click(object sender, EventArgs e)
{
Name = txtName.Text;
Scores = txtStoreScores.TextBox;
}
Then in the Form 1 OnLoad event access those properties and display them in the TextBox
private Form1_Load (object sender, EventArgs e)
{
StudentBox.Items.Add(string.Format("{0} {1}", Form2.Name, Form2.Scores);
}
I have a problem with my code. Its working fine and there is no error but a logical one i think so. I have used a method PassValue(int id) to get value from another from. I have tested it and the forms are exchanging the values correctly but the problem comes when I use the value which i have received from other form as a "textbox.text" or a "label.text"
Here is my code:
namespace MyProgram
{
public partial class UserProfile : Form
{
public string empidstr;
public UserProfile()
{
InitializeComponent();
}
public void PassValue(int id)
{
string idstring = Convert.ToString(id);
// empidlabel.Text = idstring;
empidstr = idstring;
}
private void button2_Click(object sender, EventArgs e)
{
empidlabel.Text = empidstr;
}
private void UserProfile_Load(object sender, EventArgs e)
{
}
}}
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.