|
-
June 21st, 2005, 12:39 PM
#1
Writting to a ListBox control from another class
Hi ...
I have created a ListBox on a Form. I would like to write to the ListBox from other classes using a wrapper method in the Form Class.
I have a public method in the Form1 Class called WriteStatusMessage(). This method will format the string and add the date and time and in the future could write to a file.
I would like to be able to call this method from other classes. In the sample code below I an able to create an instance of the Form1 Class and then call the WriteStatusMessage() method. Stepping through the code I actaully get into the method and to the point where I do the Add() on the ListBox.Item Object.
Any idea why the message will not display on the ListBox? As you can see I have tryed the Update() and Invalidate() method, both to repaint the ListBox?
Thanks,
Chris
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestBlueRayon
{
public class Form1 : System.Windows.Forms.Form
{
private void menuItemVideo_Click(object sender, System.EventArgs e)
{
//string strTemp;
string strMessage;
// Tell the user what is happening
strMessage = "Create Video Object";
WriteStatusMessage(strMessage);
// Create the Video object
Video GreenVideo = new Video();
// Change the contrast
// This call will try to write to the ListBox
GreenVideo.SetVideoContrast();
}
public void WriteStatusMessage(string strMessage)
{
string strDisplayMessage;
DateTime dtCurrent = DateTime.Now;
int nYear = dtCurrent.Year;
int nMonth = dtCurrent.Month;
int nDay = dtCurrent.Day;
int nHour = dtCurrent.Hour;
int nMinute = dtCurrent.Minute;
int nSecond = dtCurrent.Second;
int nMillisecond = dtCurrent.Millisecond;
string strTemp;
strDisplayMessage = String.Format("{0,0:D4}.{1,0:D2}.{2,0:D2} {3,0:D2}:{4,0:D2}:{5,0:D2}.{6,0:D3} {7}",
nYear,
nMonth,
nDay,
nHour,
nMinute,
nSecond,
nMillisecond,
strMessage);
// Shutdown the painting of the ListBox as items are added.
this.ListBoxStatus.BeginUpdate();
this.ListBoxStatus.Items.Add(strDisplayMessage);
this.ListBoxStatus.EndUpdate();
// this.ListBoxStatus.Invalidate();
this.ListBoxStatus.Update();
}
}
}
namespace TestBlueRayon
{
public class Video
{
public Video()
{
// This will not write to the ListBox ??
Form1 NewForm = new Form1();
NewForm.WriteStatusMessage("Hello");
}
// Public attributes
public int nColor;
public int nHue;
public string strComment;
public void SetVideoContrast(int nIndex)
{
// This will NOT write to the ListBox
Form1 NewForm = new Form1();
NewForm.WriteStatusMessage("Hello");
nColor = 12;
nHue = 34;
strComment = "Contrast has been set";
}
}
}
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
|