Accesing child class value from list - c#

There two classes
1) Parent Class has 3 fields *all classes have getters and setters
public class CellPhone
{
private string Iemi;
private string description;
private decimal price;
public CellPhone(){}
public CellPhone(string Iemi, string Description, decimal Price)
{
this.Iemi = Iemi;
this.description = Description;
this.price = Price;
}
2) the Child has 1 field, but inherits all frield from the parent class.
public class InDate : CellPhone
{
private string inDate;
public InDate(){}
public InDate(string Iemi, string Description, string InDate,
decimal Price):base( Iemi, Description, Price)
{
this.inDate = InDate;
}
3) I use manage to write all data, and read date to memory (to a list)
public partial class frmSellCellPhone : Form
{
CellPhoneList newList = new CellPhoneList();
public frmSellCellPhone()
{
InitializeComponent();
}
private void frmSellCellPhone_Load(object sender, EventArgs e)
{
newList.Fill();
}
private void btnSale_Click(object sender, EventArgs e)
{
int i = cmbBox.SelectedIndex;
}
private void SearchItemOnList()
{
foreach (CellPhone c in newList)
{
if (c.IEMI == txtSearchIEMI.Text)
{
txtDesciption.Text = c.Description;
txtInPrice.Text = Convert.ToString(c.Price);
txtInDate.Text = ""; //can't access inDate?
}
}
}
The issue is when i try displaying, the date is not accessible.
I would appreicate if any suggestion is offered.

You did not pasted your CellPhoneList class (or is it just a List ?), but surely it is using CellPhone instead of InDate.
Change your SearchItemOnList() to
private void SearchItemOnList()
{
foreach (InDate c in newList)
{
if (c.IEMI == txtSearchIEMI.Text)
{
txtDesciption.Text = c.Description;
txtInPrice.Text = Convert.ToString(c.Price);
txtInDate.Text = ""; //can't access inDate?
}
}
}

In the following method, you step through a list of CellPhones.
CellPhone doesn't have a inDate property.
private void SearchItemOnList() {
foreach (CellPhone c in newList) {
if (c.IEMI == txtSearchIEMI.Text) {
txtDesciption.Text = c.Description;
txtInPrice.Text = Convert.ToString(c.Price);
txtInDate.Text = ""; //can't access inDate?
}
}
}
You would need to step through a list of InDates to get that value.

Related

Loaded an object into a listbox with 2 variables, listbox show only the name

I have a Class where there a 2 variables int ID and string name. I made a list of several objects and loaded them onto a listbox. The listbox only show the name. Is there a way to retrieve the ID from the listbox?
class Show
{
private int _Id;
private string _Naam;
private string _Genre;
public override string ToString()
{
return Naam;
}
}
from a database i make a list of objects.
private void bttn_zoek_Click(object sender, EventArgs e)
{
foreach (object a in List<show> List)
{
listbox1.Items.Add(a);
}
}
I hope this is enough
Assuming WinForms, here is a super simple example of overriding ToString() to control how the class is displayed in the ListBox, and also how to cast the selected item in the ListBox back to your class type so you can extract values from it. There are other ways to accomplish this task, but you should understand a bare bones example like this first:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SomeClassName sc1 = new SomeClassName();
sc1.ID = 411;
sc1.Name = "Information";
listBox1.Items.Add(sc1);
SomeClassName sc2 = new SomeClassName();
sc2.ID = 911;
sc2.Name = "Emergency";
listBox1.Items.Add(sc2);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
SomeClassName sc = (SomeClassName)listBox1.Items[listBox1.SelectedIndex];
label1.Text = "ID: " + sc.ID.ToString();
label2.Text = "Name: " + sc.Name;
}
}
}
public class SomeClassName
{
public int ID;
public string Name;
public override string ToString()
{
return ID.ToString() + ": " + Name;
}
}
Posting some of your code would be nice. Have you tried listBox.items[index].ID?
Here I'm assuming that index is whatever index you're currently searching for.
You can also try listBox.SelectedItem[index].ID if you're doing something like an event.

C# - Accessing a list of Objects from one class to another

I am having this very strange problem where i am creating a list of some objects in one class then trying to access it in another class but it's coming empty in other class:
My first class where i am populating the list:
namespace dragdrop
{
struct BR
{
private string var;
public string Var
{
get { return var; }
set { var = value; }
}
private string equalsTo;
public string EqualsTo
{
get { return equalsTo; }
set { equalsTo = value; }
}
private string output;
public string Output
{
get { return output; }
set { output = value; }
}
private string els;
public string Els
{
get { return els; }
set { els = value; }
}
private string elsOutput;
public string ElsOutput
{
get { return elsOutput; }
set { elsOutput = value; }
}
}
public partial class Form1 : Form
{
//******************
private List<BR> list = new List<BR>(); //This is the list!
//******************
internal List<BR> List
{
get { return list; }
set { list = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] vars = new string[] { "Name", "Gender", "Age", "Address", "email" };
comboBox1.DataSource = vars;
}
private void button1_Click(object sender, EventArgs e)
{
BR b = new BR();
b.Var = comboBox1.SelectedItem.ToString();
b.EqualsTo = textBox1.Text;
b.Output = textBox2.Text;
list.Add(b);
//*****************
textBox1.Text = List.Count.ToString(); //This gives the correct count value!
//*****************
//this.Close();
}
}
}
I am accessing it in second class like:
namespace dragdrop
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Form1 form = new Form1();
List<BR> l = form.List; ;
//*******************
MessageBox.Show(form.List.Count.ToString()); //This strangely gives count 0!
//*******************
}
}
}
I have even tried making everything public in first class but no matter what i do, im always getting empty list in second class.
The is no relation what-so-ever between Form1 and Ribbon1, how can one then access an instance of the other?
With this:
Form1 form = new Form1(); // new instance of Form1
List<BR> l = form.List; ; // of course the list is empty in a new instance!
you can never access values from another instance of Form1.
Since I have no idea how your classes are connected I cannot give you more advice than have a look at this good overview of OO-relationships. You have to connect them somehow for it to work, I would very much recommend composition // aggregation (same thing, different schools).
All i needed to do was make the list a static member in class one, that solved the issue of having different value when i tried to create a new instance of Form1 in Ribbon1 class.
private static List<BR> list = new List<BR>();

Panel does not contain a constructor that takes 0 arguments

I need to load a User Control in my panel1 inside Form1.cs, the problem is that the UserControl (AudioPlaybackPanel) contains an ImportingConstructor ([ImportMany]IEnumerable<>) and I can't figure out what two arguments I should have in the Form1 AudioPlaybackPanel(????).
The error I get is: 'NAudio.App.AudioPlaybackPanel' does not contain a constructor that takes 0 arguments
Here is the Form1.cs
namespace NAudio.App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
AudioPlaybackPanel myPanel = new AudioPlaybackPanel(????);
panel1.Controls.Add(myPanel);
}
}
}
And this is my User Control Panel (AudioPlaybackPanel.cs):
namespace NAudio.App
{
[Export]
public partial class AudioPlaybackPanel : UserControl
{
private IWavePlayer waveOut;
private string fileName = null;
private WaveStream fileWaveStream;
private Action<float> setVolumeDelegate;
[ImportingConstructor]
public AudioPlaybackPanel([ImportMany]IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
{
InitializeComponent();
LoadOutputDevicePlugins(outputDevicePlugins);
}
[ImportMany(typeof(IInputFileFormatPlugin))]
public IEnumerable<IInputFileFormatPlugin> InputFileFormats { get; set; }
private void LoadOutputDevicePlugins(IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
{
comboBoxOutputDevice.DisplayMember = "Name";
comboBoxOutputDevice.SelectedIndexChanged += new EventHandler(comboBoxOutputDevice_SelectedIndexChanged);
foreach (var outputDevicePlugin in outputDevicePlugins.OrderBy(p => p.Priority))
{
comboBoxOutputDevice.Items.Add(outputDevicePlugin);
}
comboBoxOutputDevice.SelectedIndex = 0;
}
void comboBoxOutputDevice_SelectedIndexChanged(object sender, EventArgs e)
{
panelOutputDeviceSettings.Controls.Clear();
Control settingsPanel;
if (SelectedOutputDevicePlugin.IsAvailable)
{
settingsPanel = SelectedOutputDevicePlugin.CreateSettingsPanel();
}
else
{
settingsPanel = new Label() { Text = "This output device is unavailable on your system", Dock=DockStyle.Fill };
}
panelOutputDeviceSettings.Controls.Add(settingsPanel);
}
private IOutputDevicePlugin SelectedOutputDevicePlugin
{
get { return (IOutputDevicePlugin)comboBoxOutputDevice.SelectedItem; }
}
// The rest of the code continues from here on...
}
}
Here is the Interface:
namespace NAudio.App
{
public interface IOutputDevicePlugin
{
IWavePlayer CreateDevice(int latency);
UserControl CreateSettingsPanel();
string Name { get; }
bool IsAvailable { get; }
int Priority { get; }
}
}
And just in case, here is one of the plugins:
DirectSoundOutPlugin.cs
namespace NAudio.App
{
[Export(typeof(IOutputDevicePlugin))]
class DirectSoundOutPlugin : IOutputDevicePlugin
{
private DirectSoundOutSettingsPanel settingsPanel;
private bool isAvailable;
public DirectSoundOutPlugin()
{
this.isAvailable = DirectSoundOut.Devices.Count() > 0;
}
public IWavePlayer CreateDevice(int latency)
{
return new DirectSoundOut(settingsPanel.SelectedDevice, latency);
}
public UserControl CreateSettingsPanel()
{
this.settingsPanel = new DirectSoundOutSettingsPanel();
return this.settingsPanel;
}
public string Name
{
get { return "DirectSound"; }
}
public bool IsAvailable
{
get { return isAvailable; }
}
public int Priority
{
get { return 3; }
}
}
}
Please help!
The error doesn't say it expects two arguments... it just says it doesn't take 0.
The constructor expects a single parameter - an IEnumerable<IOutputDevicePlugin>:
public AudioPlaybackPanel([ImportMany]IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
{
...
}
You need to find something that implements the IOutputDevicePlugin interface and pass a collection of it, even if it's just an empty collection. (Passing null to the constructor will allow it to compile but will throw a runtime exception when you hit the loop in LoadOutputDevicePlugins.)
Considering the update to your question, something like this will get you up and running (although I doubt it means very much to pass an empty list):
var myPanel = new AudioPlaybackPanel(new List<DirectSoundOutPlugin>());
panel1.Controls.Add(myPanel);
It's worth asking whether you actually need to copy AudioPlaybackPanel.cs from the NAudio demo in its entirety. The reason it has this constructor is that it tries to demonstrate how you can use each and every one of NAudio's IWavePlayer implementations. But in a normal real-world application you would just select the one that was most appropriate for your use. e.g.
this.waveOut = new WaveOut();
waveOut.Init(new AudioFileReader("my file.mp3");
waveOut.Play();
So there's no need to incorporate the plug-in architecture from that particular demo, if all you want is just to play audio files.

Is there any to set the visibility of xtra tree list nodes dynamically

I am using xtratreelist in my application with only first level and I want to make some of the nodes visible but not all. Here is the code, but after that all the not are showing in the list
TreeList tr = new Treelist();
for (int x = 0; x < tr.Nodes.Count; x++)
{
tr.Nodes[x].Visible = false;
}
I suggest you use the NodesIterator, here is an example, and it works for me :
The data class :
public class Service
{
public string Name { get; set; }
public bool Visible { get; set; }
}
And in my form :
private void TreeForm_Load(object sender, EventArgs e)
{
treeList1.DataSource = Service.GetServices();
treeList1.NodesIterator.DoLocalOperation(setNodeVisibility, treeList1.Nodes);
}
private void setNodeVisibility(DevExpress.XtraTreeList.Nodes.TreeListNode node)
{
var service = treeList1.GetDataRecordByNode(node) as Service;
if (service == null)
return;
node.Visible = service.Visible;
}
Go through the following links
FindNodeByID
FindNodeByFieldValue
FindNodeByKeyID

Sending ListView data between forms

Hi, I am a newbie to C# and I have no programming background, but I am interested in it.
I want to send data to a ListView, but the data is in the other form. I already saw all the related posts here. I tried copying code from one of the post and changing it according to my needs, but it doesn't work.
Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public delegate void HandleItemAdded(object sender, ItemAddedEventArgs e);
public struct ItemAddedEventArgs: EventArgs
{
public string PartPrefix;
public string PartStartNumber;
public string AssemblyPrefix;
public string AssemblyStartNumber;
public string Name;
public string Profile;
public string Material;
public string Finish;
public string Class;
public ItemAddedEventArgs(string partprefix, string partstartnumber, string assemblyprefix, string assemblystartnumber, string name, string profile, string material, string finish, string classes)
{
PartPrefix = partprefix;
PartStartNumber = partstartnumber;
AssemblyPrefix = assemblyprefix;
AssemblyStartNumber = assemblystartnumber;
Name = name;
Profile = profile;
Material = material;
Finish = finish;
Class = classes;
}
}
public event HandleItemAdded ItemAdded;
public void RaiseItemAdded(ItemAddedEventArgs e)
{
if (ItemAdded != null)
ItemAdded(this, e);
}
public void AddToList()
{
RaiseItemAdded (new ItemAddedEventArgs (textBox221.Text, textBox222.Text, textBox223.Text, textBox224.Text, textBox225.Text, textBox226.Text, textBox227.Text, textBox228.Text, textBox229.Text));
}
}
Form1:
public void HandleItemAdded(object sender, WindowsFormsApplication1.Form3.ItemAddedEventArgs e)
{
ListViewItem item1 = new ListViewItem(textBox221.Text);
item1.SubItems.Add(textBox222.Text);
item1.SubItems.Add(textBox223.Text);
item1.SubItems.Add(textBox224.Text);
item1.SubItems.Add(textBox225.Text);
item1.SubItems.Add(textBox226.Text);
item1.SubItems.Add(textBox227.Text);
item1.SubItems.Add(textBox228.Text);
item1.SubItems.Add(textBox229.Text);
listView1.Add(item1);
Form3.ItemAdded += Form1.HandleItemAdded; *<-( i dont know if this is the correct place for this.)
}
The error I get is: type EventArgs in interface list is not an interface
Thank you in advance.
What you need to do in Form1 is this:
public void HandleItemAdded(object sender, WindowsFormsApplication1.Form3.ItemAddedEventArgs e)
{
ListViewItem item1 = new ListViewItem(e.PartPrefix);
item1.SubItems.Add(e.PartStartNumber);
item1.SubItems.Add(e.<Member_Name>);
.
.
.
listView1.Add(item1);
}
And for the error I think following should work for you:
public class ItemAddedEventArgs: EventArgs

Categories