Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Writting to a ListBox from another class

Writting to a ListBox from another class

Scheduled Pinned Locked Moved C#
questiongraphicsannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cmacgowan
    wrote on last edited by
    #1

    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 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,

    S I 2 Replies Last reply
    0
    • C cmacgowan

      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 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,

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Try taking off the BeginUpdate and EndUpdate calls sandwiching the Add method. I had the same problem once and taking off BeginUpdate and EndUpdate solved it, although I still don't know know why/how. Maybe it's a bug in the .NET framework. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • C cmacgowan

        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 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,

        I Offline
        I Offline
        IamJunk
        wrote on last edited by
        #3

        The problem I can see with your code is... When you are trying to add elements to the listbox, you are creating a new instance of your form object. this will never point to the current form being displayed. So even if you call add(), it will add to the listbox on the form which is never displayed. Instead, you have to get the reference to the existing form and then call add method. Hope this helps!!!


        Regards, Sidhu IF YOU DON'T HAVE ANYTHING TO SAY, WELCOME TO THE CLUB Dotnet @ Work Where Dotnet works... My Blog

        C 1 Reply Last reply
        0
        • I IamJunk

          The problem I can see with your code is... When you are trying to add elements to the listbox, you are creating a new instance of your form object. this will never point to the current form being displayed. So even if you call add(), it will add to the listbox on the form which is never displayed. Instead, you have to get the reference to the existing form and then call add method. Hope this helps!!!


          Regards, Sidhu IF YOU DON'T HAVE ANYTHING TO SAY, WELCOME TO THE CLUB Dotnet @ Work Where Dotnet works... My Blog

          C Offline
          C Offline
          cmacgowan
          wrote on last edited by
          #4

          Thanks IAMJUNK ... The following is the solution to the ListBox Issue Thanks also to Andy Tacker (CodeGuru) Note that we added a reference to the form in the Video Class (m_ParentForm1). Then when we create the Video Object in the Form1 Object we will set the Video.m_ParentForm1 attribute to be to set the reference back to the Form1 Object. Then when we want to use the Form1::WriteStatusMessage() method we can used the reference to Form1 from m_ParentForm1. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace TestBlueRayon { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MenuItem menuItemVideo; private System.Windows.Forms.ListBox ListBoxStatus; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); WriteStatusMessage("TestBlueRayon Application Started."); WriteStatusMessage("Good to have you with us. "); } #region Windows Form Designer generated code private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); // took out code this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } 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(); // Set the parent object GreenVideo.m_ParentForm1 = this; // Set video contrast GreenVideo.SetVideoContrast(0); } public void WriteStatusMessage(string strMessage) { // You have pressed ??? string strDisplayMessage; DateTime dtCurrent = DateTime.Now; int nYear = dtCurrent.Year; int nMonth = dtCurrent.Month; int nDay = dtCurrent.Day; int nHour

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups