|
-
November 3rd, 2009, 06:17 PM
#1
Populating listbox control form a class file
Hello i am trying to populate a listbox on Form1 from values in a seperate class file called vmgetter.cs, so far everything seems to be working fine in the class file i just cant get the info to populate into the listbox on Form1 i have set the listbox to public also. Below is my Code.
vmtgetter.cs
----------------------------------------------
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Collections;
namespace BackerUpper4.Engine
{
/* public struct VMInfo
{
public string _name;
public int _id;
public string _vmguid;
}*/
class vmgetter
{
public List<vmgetter> VMList;
private string _name;
private int _id;
private string _vmguid;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Vmguid
{
get { return _vmguid; }
set { _vmguid = value; }
}
public void Getvm()
{
try
{
ManagementScope scope = new ManagementScope("\\\\.\\root\\virtualization");
scope.Connect();
ObjectQuery query = new ObjectQuery(
"SELECT Name,ElementName FROM Msvm_ComputerSystem");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
VMList = new List<vmgetter>();
int i = 0;
foreach (ManagementObject m in queryCollection)
{
//skip the first entry because it is the local machine name, not a vm.
if (i == 0)
{
i++;
continue;
}
else
{
vmgetter NewInfo = new vmgetter();
NewInfo._id = i - 1;
NewInfo._vmguid = m["Name"].ToString();
NewInfo._name = m["ElementName"].ToString();
Form1 frm = new Form1();
frm.listBox1.Items.Add(NewInfo.Vmguid + " " + NewInfo.Name);
//MessageBox.Show(NewInfo.Name);
//MessageBox.Show(NewInfo.Vmguid);
VMList.Add(NewInfo);
}
}
}
catch (Exception e)
{
}
}
}
}
Form1.cs Code:
-----------------------------------------------
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.Collections;
using System.Management;
using System.IO;
using BackerUpper4.Engine;
namespace BackerUpper4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
vmgetter vm = new vmgetter();
vm.Getvm();
}
}
}
Last edited by Shuja Ali; November 4th, 2009 at 02:08 PM.
Reason: Added code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|