CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    Saint Paul, Minnesota, US
    Posts
    91

    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";
                
                
            }
    
            }
    }

  2. #2
    Join Date
    May 2004
    Posts
    37

    Re: Writting to a ListBox control from another class

    In your code examples, the Video code is instantiating a Form1 but not Showing it. Was that just meant to simplify your explanation? How are you binding the existing Form1 to the call in Video?

    I reused your code and added another class from which main() was called...

    Code:
    public class AnotherClass
    {
    	public void Write(string s, Form1 f) 
    	{
    		f.WriteStatusMessage( s );
    	}
    	
    	[STAThread]
    	static void Main() 
    	{
    		Form1 form = new Form1();
    		form.Show();
    		AnotherClass a = new AnotherClass();
    		a.Write( "Write Successful",form);
    		MessageBox.Show(" Look at Form1 to see if it worked.");
    	}
    }
    Looked ok to me.

  3. #3
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: Writting to a ListBox control from another class

    check this out!
    Code:
    namespace TestBlueRayon
    {
        
        public class Form1 : System.Windows.Forms.Form
        {
            private void menuItemVideo_Click(object sender, System.EventArgs e)
            {
                // Create the Video object
                Video GreenVideo = new Video();
                GreenVideo.m_Parentform1 = this;
    
                // Change the contrast
                // This call will try to write to the ListBox
                GreenVideo.UpdateParentFormListBox(); 
            }
        }
            public class Video
            {
                    public Form1 m_Parentform1;
                    public Video()
                    {
                    }
    
                    public void UpdateParentFormListBox()
                    {
                         m_Parentform1.WriteStatusMessage("Hello"); 
                    }
            }
    }
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured