|
-
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
-
November 4th, 2009, 02:22 PM
#2
Re: Populating listbox control form a class file
Welcome to the Forum 
The culprit is this line of code Form1 frm = new Form1();. You are actually creating a new instance of Form1 and then populating the List Box on this instance and not on the Original Form from which the class was called. I for one thing don't like populating UI elements directly from a class. Let the class do its own work and Let the UI have the responsibility of populating UI elements. Why couple both things together and create a mess.
Modify the GetVM method to return an array list or a custom data type and then use that custom type to populate the listbox.
An easy work around would also be to pass the Form's instance to GetVM method and then use that to refer to the Form's listbox. But that would again mean that you are coupling two things together which is not a good thing to do when you are implementing Object oriented features.
-
December 21st, 2009, 08:01 PM
#3
Re: Populating listbox control form a class file
Hi thank you so much for your reply, i have started getting back to work on my project and am stuck trying to have my GetVm() method return a List. If i do the following then it tells me not all code paths return a value, but if i put the return NewInfo.Name outside the loop then it grags just the 1st vm in the list and doesnt recognize the rest..
public string GetVM()
//The method to return VM's on the machine.
{
List<VMInfo> VMList = new List<VMInfo>();
VMInfo NewInfo = new VMInfo();
int i = 0;
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();
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
{
NewInfo.ID = i - 1;
NewInfo.VMGuid = m["Name"].ToString();
NewInfo.Name = m["ElementName"].ToString();
VMList.Add(NewInfo);
return NewInfo.Name;
}
}
}
}
}
#endregion
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
|