searched items dont be added to list box - c#

items searched in search function: the ALBUMS dont get added to the list box?
the other fields populate
can you please tell me how i can populate the listbox with the searched albums
albums be looked up using a linked list
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assignment
{
public partial class frmAddArtist : Form
{
AVLTree<Artist> avltree = new AVLTree<Artist>();
LinkedList<Album> temp = new LinkedList<Album>();
Artist artistinst;
Album albuminst;
string noofmembers, artistname;
int artistcount;
public frmAddArtist()
{
InitializeComponent();
}
private void label5_Click(object sender, EventArgs e)
{
}
private void btnAddArtist_Click(object sender, EventArgs e)
{
string tempalbum, date;
tempalbum = txtAlbumName.Text;
date = dtpReleaseDate.Text.ToString();
albuminst = new Album(tempalbum, date);
temp.AddFirst(albuminst);
lbAlbums.Items.Add(tempalbum);
}
private void btnSave_Click(object sender, EventArgs e)
{
artistname = txtArtistName.Text;
noofmembers = txtNoOfMembers.Text;
artistinst = new Artist(artistname, noofmembers, temp);
avltree.InsertItem(artistinst);
artistcount++;
txtArtistName.Clear();
txtNoOfMembers.Clear();
txtAlbumName.Clear();
lbAlbums.Items.Clear();
temp.Clear();
}
private void btnNoOfArtist_Click(object sender, EventArgs e)
{
MessageBox.Show("The No. Artist: " + Convert.ToString(artistcount));
}
private void btnHeight_Click(object sender, EventArgs e)
{
int heightoftree = avltree.Height();
string height = Convert.ToString(heightoftree);
MessageBox.Show("The Height of the Tree: " + height);
}
private void btnSearch_Click(object sender, EventArgs e)
{
Artist temp = new Artist(txtSearch.Text, " ", null);
Artist result = avltree.Search(temp);
if (result != null)
{
if (result.CompareTo(temp) == 0)
{
txtArtistName.Text = result.artistname;
txtNoOfMembers.Text = result.noofmembers;
foreach (Album p in result.Albumslist)
{
lbAlbums.Items.Add(p.Albumname);
}
}
else if(result.CompareTo(temp) <0)
{
MessageBox .Show("No Match Found");
}
}
}
}
}

Put the items into a LinkedList or a List.
Then set lbAlbums.ItemsSource=;

Related

Passing data between two forms

first of all i want you to know that i know that there a lot of results for this question, but i have searched far and wide still haven't come up with a solution for my problem.
i have tried to do the following:
1.constructor
2.objects
3.properties
4.delegates
but none of my implementation of them really did worked as wanted (in this "solution" i have used properties
when i press "back" on the "pop up" screen i dont in the main screen the value i choose from in the "pop up" screen
basically, it's something like, i have main screen and a "pop up"
the main screen
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BakaritCV
{
public partial class FrmProdChoose : MetroFramework.Forms.MetroForm
{
CVFeedUtilities utilities = new CVFeedUtilities();
Mixtures mixture;
public string selectedDefault = " 102";
string t;
public FrmProdChoose(string t)
{
InitializeComponent();
this.t = t;
}
public FrmProdChoose()
{
InitializeComponent();
}
private void btnHome_Click(object sender, EventArgs e)
{
FrmMain frmload = new FrmMain();
utilities.moveBetweenScreens(this, frmload);
}
private void mixtureBtn_Click(object sender, EventArgs e)
{
utilities.loadPopUp(this, mixture);
}
private void FrmProdChoose_Load(object sender, EventArgs e)
{
mixture = new Mixtures(this);
mixtureBtn.Text = selectedDefault;
}
public string Selected
{
get { return selectedDefault; }
set { selectedDefault = value; }
}
}
}
the "pop up"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BakaritCV
{
public partial class Mixtures : MetroFramework.Forms.MetroForm
{
string[] mixture = new string[] { "102", "103", "104", "105" };
MetroFramework.Controls.MetroTile[] tiles;
FrmProdChoose form;
string selectedDefault;
CVFeedUtilities utilities = new CVFeedUtilities();
public Mixtures(FrmProdChoose form)
{
InitializeComponent();
this.form = form;
}
private void btnHome_Click(object sender, EventArgs e)
{
form.Selected = selectedDefault;
utilities.closePopUp(this, form);
}
private void Mixtures_Load(object sender, EventArgs e)
{
tiles = new MetroFramework.Controls.MetroTile[] { tileOne, tileTwo, tileThree, tileFour};
for (int i = 0; i < mixture.Length; i++)
tiles[i].Text = mixture[i];
}
private void tileOne_Click(object sender, EventArgs e)
{
tileOne.BackColor = Color.ForestGreen;
removeBackColor(1);
}
private void tileTwo_Click(object sender, EventArgs e)
{
tileTwo.BackColor = Color.ForestGreen;
removeBackColor(2);
}
private void tileThree_Click(object sender, EventArgs e)
{
tileThree.BackColor = Color.ForestGreen;
removeBackColor(3);
}
private void tileFour_Click(object sender, EventArgs e)
{
tileFour.BackColor = Color.ForestGreen;
removeBackColor(4);
}
private void tileFive_Click(object sender, EventArgs e)
{
tileFive.BackColor = Color.ForestGreen;
removeBackColor(5);
}
public void removeBackColor(int index)
{
for (int i = 0; i < tiles.Length; i++)
{
if (i == index - 1)
{
selectedDefault = tiles[i].Text;
continue;
}
else tiles[i].BackColor = Color.DeepSkyBlue;
}
}
}
}
and the functions loadPopUp and closePopUp
public void loadPopUp(Form from, Form to)
{
to.Tag = from;
to.Show(from);
}
public void closePopUp(Form from, Form to)
{
to.Tag = from;
if (!to.Visible)
to.Show(from);
from.Hide();
}

How can i use my splash screen until all data is loaded using a progressBar?

In the splash screen i'm using now just for the demo a timer.
But i want to use a class that using win32 classes and wait until all loops are finished. While it's looping to make the progressBar move until 100%.
In form1 i didn't make yet anything.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestingHardware
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
In Program.cs i changed the Run:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestingHardware
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Splash_Screen());
}
}
}
Then i added a new form for the splash screen:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestingHardware
{
public partial class Splash_Screen : Form
{
Timer tmr;
public Splash_Screen()
{
InitializeComponent();
}
private void Splash_Screen_Load(object sender, EventArgs e)
{
}
private void Splash_Screen_Shown(object sender, EventArgs e)
{
tmr = new Timer();
//set time interval 3 sec
tmr.Interval = 3000;
//starts the timer
tmr.Start();
tmr.Tick += tmr_Tick;
}
void tmr_Tick(object sender, EventArgs e)
{
//after 3 sec stop the timer
tmr.Stop();
//display mainform
Form1 mf = new Form1();
mf.Show();
//hide this form
this.Hide();
}
}
}
In the splash screen form designer i added a progressBar.
And last the Form that get the hardware info. I want the splash screen and progressBar to shown while it's looping until the end.
using System;
using System.Collections;
using System.Management;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GetHardwareInfo
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
cmbxOption.SelectedItem = "Win32_Processor";
}
private void InsertInfo(string Key, ref ListView lst, bool DontInsertNull)
{
lst.Items.Clear();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);
try
{
foreach (ManagementObject share in searcher.Get())
{
ListViewGroup grp;
try
{
grp = lst.Groups.Add(share["Name"].ToString(), share["Name"].ToString());
}
catch
{
grp = lst.Groups.Add(share.ToString(), share.ToString());
}
if (share.Properties.Count <= 0)
{
MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
foreach (PropertyData PC in share.Properties)
{
ListViewItem item = new ListViewItem(grp);
if (lst.Items.Count % 2 != 0)
item.BackColor = Color.White;
else
item.BackColor = Color.WhiteSmoke;
item.Text = PC.Name;
if (PC.Value != null && PC.Value.ToString() != "")
{
switch (PC.Value.GetType().ToString())
{
case "System.String[]":
string[] str = (string[])PC.Value;
string str2 = "";
foreach (string st in str)
str2 += st + " ";
item.SubItems.Add(str2);
break;
case "System.UInt16[]":
ushort[] shortData = (ushort[])PC.Value;
string tstr2 = "";
foreach (ushort st in shortData)
tstr2 += st.ToString() + " ";
item.SubItems.Add(tstr2);
break;
default:
item.SubItems.Add(PC.Value.ToString());
break;
}
}
else
{
if (!DontInsertNull)
item.SubItems.Add("No Information available");
else
continue;
}
lst.Items.Add(item);
}
}
}
catch (Exception exp)
{
MessageBox.Show("can't get data because of the followeing error \n" + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void RemoveNullValue(ref ListView lst)
{
foreach (ListViewItem item in lst.Items)
if (item.SubItems[1].Text == "No Information available")
item.Remove();
}
#region Control events ...
private void cmbxNetwork_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, chkNetwork.Checked);
}
private void cmbxSystemInfo_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxSystemInfo.SelectedItem.ToString(), ref lstSystemInfo, chkSystemInfo.Checked);
}
private void cmbxUtility_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxUtility.SelectedItem.ToString(), ref lstUtility, chkUtility.Checked);
}
private void cmbxUserAccount_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxUserAccount.SelectedItem.ToString(), ref lstUserAccount, chkUserAccount.Checked);
}
private void cmbxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxStorage.SelectedItem.ToString(), ref lstStorage, chkDataStorage.Checked);
}
private void cmbxDeveloper_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxDeveloper.SelectedItem.ToString(), ref lstDeveloper, chkDeveloper.Checked);
}
private void cmbxMemory_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxMemory.SelectedItem.ToString(), ref lstMemory, chkMemory.Checked);
}
private void chkHardware_CheckedChanged(object sender, EventArgs e)
{
if (chkHardware.Checked)
RemoveNullValue(ref lstDisplayHardware);
else
InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);
}
private void cmbxOption_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);
}
private void chkDataStorage_CheckedChanged(object sender, EventArgs e)
{
if (chkDataStorage.Checked)
RemoveNullValue(ref lstStorage);
else
InsertInfo(cmbxStorage.SelectedItem.ToString(), ref lstStorage, chkDataStorage.Checked);
}
private void chkMemory_CheckedChanged(object sender, EventArgs e)
{
if (chkMemory.Checked)
RemoveNullValue(ref lstMemory);
else
InsertInfo(cmbxMemory.SelectedItem.ToString(), ref lstStorage, false);
}
private void chkSystemInfo_CheckedChanged(object sender, EventArgs e)
{
if (chkSystemInfo.Checked)
RemoveNullValue(ref lstSystemInfo);
else
InsertInfo(cmbxSystemInfo.SelectedItem.ToString(), ref lstSystemInfo, false);
}
private void chkNetwork_CheckedChanged(object sender, EventArgs e)
{
if (chkNetwork.Checked)
RemoveNullValue(ref lstNetwork);
else
InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, false);
}
private void chkUserAccount_CheckedChanged(object sender, EventArgs e)
{
if (chkUserAccount.Checked)
RemoveNullValue(ref lstUserAccount);
else
InsertInfo(cmbxUserAccount.SelectedItem.ToString(), ref lstUserAccount, false);
}
private void chkDeveloper_CheckedChanged(object sender, EventArgs e)
{
if (chkDeveloper.Checked)
RemoveNullValue(ref lstDeveloper);
else
InsertInfo(cmbxDeveloper.SelectedItem.ToString(), ref lstDeveloper, false);
}
private void chkUtility_CheckedChanged(object sender, EventArgs e)
{
if (chkUtility.Checked)
RemoveNullValue(ref lstUtility);
else
InsertInfo(cmbxUtility.SelectedItem.ToString(), ref lstUtility, false);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
linkLabel1.LinkVisited = true;
System.Diagnostics.Process.Start("http://www.ShiraziOnline.net");
}
#endregion
}
}
It's using win32 classes and instead adding it to a ListView i want to add each part to a List. Then i want to use the Lists in form1 when it's finishing all loops and add the Lists to controls. Or maybe i should load it in form1 already ?
Not sure.
If searcher has a Count property, use that for your maximum value. Determine current percentage with:
int count = 0;
try
{
foreach (ManagementObject share in searcher.Get())
{
count++;
progressbar1.Value = count * 100 / Math.Max(1, searcher.Count);
Also, make sure that you are setting your ListView to suspend updates using .SuspendLayout() before adding the items and .ResumeLayout() afterwards as constantly updating your ListView is very slow.
Another recommendation would be to place all your ListViewItems into an List/Array and then just update your ListView once.
Create a List at the start just after the Clear()
lst.SuspendLayout();
lst.Items.Clear()
System.Collections.Generic.List<ListViewItem> listViewItems = new System.Collections.Generic.List<ListViewItem>();
Then where you were adding the items before add them to the List<>
listViewItems.Add(item);
And finally after all records have been processed add the List<> to your ListView and resume updates
try
{
// All your code here
lst.Items.AddRange(listViewItems.ToArray());
}
finally
{
lst.ResumeLayout();
}

Storing a value to a listbox item C#

I wish to know how to store a VALUE to an item in a listbox. I'm currently working out a worksheet for my studies, and I've been wondering how to do this. Basically I need to store values to the Food (such as broccoli, bread. For example: Broccoli has a value of 20 calories).
The user must not see the value, only the program can store it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MCAST_Calorie_Counter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lbAvailable.Items.Clear();
if (comboBox1.Text == "Vegetables")
{
lbAvailable.Items.Add("Broccoli");
lbAvailable.Items.Add("Carrots");
lbAvailable.Items.Add("Lettuce");
lbAvailable.Items.Add("Onions");
lbAvailable.Items.Add("Potatoes");
}
if (comboBox1.Text == "Meat")
{
lbAvailable.Items.Add("Chicken");
lbAvailable.Items.Add("Veal");
lbAvailable.Items.Add("Beef");
lbAvailable.Items.Add("Fish");
}
if (comboBox1.Text == "Legumes")
{
lbAvailable.Items.Add("Bread");
lbAvailable.Items.Add("Peanuts");
lbAvailable.Items.Add("Green Peas");
lbAvailable.Items.Add("Lentils");
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection highlightedItems = lbAvailable.SelectedItems;
foreach (var item in highlightedItems)
{
lbChosen.Items.Add(item);
}
if (lbAvailable.SelectedItems.Count > 0)
{
lbAvailable.Items.Remove(lbAvailable.SelectedItems[0]);
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection highlightedItems = lbChosen.SelectedItems;
foreach (var item in highlightedItems)
{
lbAvailable.Items.Add(item);
}
if (lbChosen.SelectedItems.Count > 0)
{
lbChosen.Items.Remove(lbChosen.SelectedItems[0]);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
lbAvailable.Items.Clear();
lbChosen.Items.Clear();
if (comboBox1.Text == "Vegetables")
{
lbAvailable.Items.Add("Broccoli");
lbAvailable.Items.Add("Carrots");
lbAvailable.Items.Add("Lettuce");
lbAvailable.Items.Add("Onions");
lbAvailable.Items.Add("Potatoes");
}
if (comboBox1.Text == "Meat")
{
lbAvailable.Items.Add("Chicken");
lbAvailable.Items.Add("Veal");
lbAvailable.Items.Add("Beef");
lbAvailable.Items.Add("Fish");
}
if (comboBox1.Text == "Legumes")
{
lbAvailable.Items.Add("Bread");
lbAvailable.Items.Add("Peanuts");
lbAvailable.Items.Add("Green Peas");
lbAvailable.Items.Add("Lentils");
}
}
}
}
Thanks alot for your help!
You can always add custom objects that support ToString() to the ListBox.Items collection:
struct Record
{
int value;
string label;
public override string ToString()
{
return label;
}
}
Adding a custom object:
Record record = new Record();
record.value = 1;
record.label = "This text will appear in the ListBox";
listBox.Items.Add(record);
Retrieving it:
Record selectedRecord = (Record)listBox.SelectedItem;
Console.WriteLine(selectedRecord.value); // => 1

In which part of the program should I put a function that keeps the application running continuosly in c#

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Temporizador
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
/*if (escolherHoraTxt.InvokeRequired == true)
escolherHoraTxt.Invoke((MethodInvoker)delegate { escolherHoraTxt.Text = "Invoke was needed"; });*/
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void escolherFicheiroBtn_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.ShowDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName;
fileName = dlg.FileName;
link.Text = fileName;
//MessageBox.Show(fileName);
}
}
private void escolherHoraTxt_MouseClick(object sender, EventArgs e)
{
if (escolherHoraTxt.Text == "--:--:--")
escolherHoraTxt.Text = " ";
}
private void escolherHoraTxt_TextChanged(object sender, EventArgs e)
{
if (escolherHoraTxt.Text == "--:--:--")
escolherHoraTxt.Text = " ";
}
private void gravarBtn_Click(object sender, EventArgs e)
{
}
private void gravarBtn_Click_1(object sender, EventArgs e)
{
String s = escolherHoraTxt.Text;
Horas hora = new Horas();
hora.IsValidTime(s);
hora.compareTime(s);
}
}
}
Horas.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Temporizador
{
class Horas
{
private String m_timeNow;
public String timeNow
{
get
{
return m_timeNow;
}
set
{
m_timeNow = value;
}
}
public Horas()
{
}
public string getCurrentTime()
{
m_timeNow=DateTime.Now.ToString("HH:mm:ss");
return m_timeNow;
}
public void IsValidTime(String theTime)
{
string[] timeArray = theTime.Split(new[] { ":" }, StringSplitOptions.None);
try{
Convert.ToDateTime(theTime);
}
catch (FormatException e)
{
MessageBox.Show(e.Message);
}
}
public void compareTime(String theTime)
{
string currentTime=getCurrentTime();
if (currentTime == theTime)
MessageBox.Show("SIM");
else
MessageBox.Show("NAO");
}
}
}
/I´m trying to make an application that it is always running and that will close and then open a file in a certain hour choosen by the user. I intend to use something like while(1){...} but I don´t know in what part of the program should I put this. Could someone help me please?/

adding a counter class to winform

I have a project that wants
A method that returns the current count.
A Constructor that sets the count to zero.
I have the first few down but need help with the return count to 0 and then the constructor. I need to do this by adding a counter class but I'm confused about the way to add it.
Can some one help me out?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project10TC
{
public partial class Form1 : Form
{
int zero = 0;
int i = 1;
public Form1()
{
InitializeComponent();
}
private EventHandler myCounter;
// end of Form class
private class myCounter()
{
myCounter = new myCounter( );
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = (zero).ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
public class Counter
{
public int Value { get; private set; }
public void Increment()
{
Value = Value + 1;
}
public void Decrement()
{
if (Value > 0) Value = Value - 1;
}
public Counter()
{
Value = 0;
}
}

Categories

Resources