Windows Develop Bookmark and Share   
 index > Windows Forms General > A Class Populating a form
 

A Class Populating a form

Ok, I'm having trouble trying to populate some labels on form1 of my program. I have a class that stores the data but i need the data to be displayed on the (status screen) form1 ill poast what i have


namespace Roller_Tumble
{
class Tumbler
{

private String Part_Number;
private String Shop_Order;
private String Quantity;
//public String Oporation;
public DateTime Time_In;
public DateTime Time_Ft;
public DateTime Time_FtE;
public DateTime Time_FtA;
public DateTime Time_FtL;
public DateTime Time_Pull;
public DateTime Time_PullE;
public DateTime Time_PullA;
public DateTime Time_PullL;

public Tumbler(string Part_Number, string Shop_Order, string Quantity)
{
this.Part_Number = Part_Number;
this.Shop_Order = Shop_Order;
this.Quantity = Quantity;
//this.Oporation = Oporation;
this.Time_In = DateTime.Now;
this.Time_Ft = Time_In.AddHours(1.5);
this.Time_Pull = Time_Ft.AddHours(2);
}
public void Populate()
{
Form1.
}
}


some code is not finished but the part im having trouble with is the populate method


Thanks
Jon
Thelostcircuit  Monday, January 15, 2007 11:59 PM

Hi Jon,

your business objects should not know anything about your GUI, your GUI should know about your business objects but not vice versa. So you do not want a populate method in your business object, what you need to do at some point in your form code is to populate your GUI, tis can be done either by hand coding or by DataBinding. An example of hand coding would be like below, at some point in your GUI you will create one of your business objects, then you will pass your business object into a method where you update your GUI:

using System;

using System.Windows.Forms;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

MyForm f = new MyForm();

f.UserClicksButton();

}

}

class MyForm : Form

{

private Label label1 = new Label();

public void UserClicksButton()

{

Person p = new Person("bob");

UpdateGui(p);

}

private void UpdateGui(Person p)

{

this.label1.Text = p.Name;

}

}

class Person

{

private string name;

public Person(string name)

{

this.name = name;

}

public string Name

{

get

{

return this.name;

}

}

}

}

DataBinding allows you to in effect bind properties of objects i.e. the Name property to controls so that they automatically update, this is a good link to some basic tutorials on that: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/

Hope that helps

Mark.

Mark Dawson  Tuesday, January 16, 2007 1:17 AM

Hi Jon,

your business objects should not know anything about your GUI, your GUI should know about your business objects but not vice versa. So you do not want a populate method in your business object, what you need to do at some point in your form code is to populate your GUI, tis can be done either by hand coding or by DataBinding. An example of hand coding would be like below, at some point in your GUI you will create one of your business objects, then you will pass your business object into a method where you update your GUI:

using System;

using System.Windows.Forms;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

MyForm f = new MyForm();

f.UserClicksButton();

}

}

class MyForm : Form

{

private Label label1 = new Label();

public void UserClicksButton()

{

Person p = new Person("bob");

UpdateGui(p);

}

private void UpdateGui(Person p)

{

this.label1.Text = p.Name;

}

}

class Person

{

private string name;

public Person(string name)

{

this.name = name;

}

public string Name

{

get

{

return this.name;

}

}

}

}

DataBinding allows you to in effect bind properties of objects i.e. the Name property to controls so that they automatically update, this is a good link to some basic tutorials on that: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/

Hope that helps

Mark.

Mark Dawson  Tuesday, January 16, 2007 1:17 AM

You can use google to search for other answers

Custom Search

More Threads

• Customized MessageBox Buttons?
• Hiding the clicked button
• A Control with decimal increments
• C#/VB.NET try-catch (Exception Handling)
• On Form Close, doesn't clear up memory
• how autolaunch Windows Defragmenter at a particular time
• Modifying Objects With User Added Controls.
• DataGridView row count
• How to get ASCII character out of System.Windows.Forms.KeyEventArgs.KeyValue?
• ListView.Items.Find using Invoke and Threads