Thread: Multi-Column Listbox :: C#

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Multi-Column Listbox :: C#

    Hi.

    I am a C++ and MFC programmer. I have been forced to code a simple C# program at school. Frankly, I feel threaten. Secondly, I find C# confused. Everything is "Win Form." Where the heck are all the code behind the program?

    Anyways, I am desperately in need of help. Here is the basic program.

    Buttons = 2
    Edit boxes = 3
    Listbox = 1

    The idea is to enter information in one the three edit boxes and then click a button to add it to a list box.

    Right now I want to add multicolumns to the listbox.

    Please post if you have experience with C# and can point out all the shortcuts. This is a very simple introductory program.

    Thanks,
    Kuphryn

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    C# isn't that different from C++/MFC. Do you have Visual Studio ? It creates code for you if you select columns. This is the code Visual Studio creates for a simple ListView ( can't have a listbox with multiple columns ) adds two columns and the first line.
    You will notice that the basics are the same:
    createlistview
    add column
    add column
    add item
    set subitem for second column of item

    PHP Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace 
    WindowsApplication2
    {
        public class 
    Form1 System.Windows.Forms.Form
        
    {
            private 
    System.Windows.Forms.ColumnHeader m_ColumnHeaderTwo;
            private 
    System.Windows.Forms.ColumnHeader m_ColumnHeaderOne;
            private 
    System.Windows.Forms.ListView m_ListView;
            private 
    System.ComponentModel.Container components null;

            public 
    Form1()
            {
                
    InitializeComponent();
            }

            
    /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            
    protected override void Disposebool disposing )
            {
                if( 
    disposing )
                {
                    if (
    components != null
                    {
                        
    components.Dispose();
                    }
                }
                
    base.Disposedisposing );
            }

            
    #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            
    private void InitializeComponent()
            {
                
    System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
                                                                                                                         
    "Test Text in Column One",
                                                                                                                         
    "Test Text in Column2 "}, -1System.Drawing.SystemColors.WindowTextSystem.Drawing.SystemColors.Window, new System.Drawing.Font("Microsoft Sans Serif"8.25FSystem.Drawing.FontStyle.RegularSystem.Drawing.GraphicsUnit.Point, ((System.Byte)(0))));
                
    this.m_ListView = new System.Windows.Forms.ListView();
                
    this.m_ColumnHeaderTwo = new System.Windows.Forms.ColumnHeader();
                
    this.m_ColumnHeaderOne = new System.Windows.Forms.ColumnHeader();
                
    this.SuspendLayout();
                
    // 
                // m_ListView
                // 
                
    this.m_ListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                             
    this.m_ColumnHeaderOne,
                                                                                             
    this.m_ColumnHeaderTwo});
                
    this.m_ListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
                                                                                           
    listViewItem1});
                
    this.m_ListView.Location = new System.Drawing.Point(8080);
                
    this.m_ListView.Name "m_ListView";
                
    this.m_ListView.Size = new System.Drawing.Size(16097);
                
    this.m_ListView.TabIndex 0;
                
    this.m_ListView.View System.Windows.Forms.View.Details;
                
    // 
                // m_ColumnHeaderTwo
                // 
                
    this.m_ColumnHeaderTwo.Text "Second Column";
                
    this.m_ColumnHeaderTwo.Width 72;
                
    // 
                // m_ColumnHeaderOne
                // 
                
    this.m_ColumnHeaderOne.Text "First Column";
                
    // 
                // Form1
                // 
                
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
                
    this.ClientSize = new System.Drawing.Size(292273);
                
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                              
    this.m_ListView});
                
    this.Name "Form1";
                
    this.Text "Form1";
                
    this.ResumeLayout(false);

            }
            
    #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            
    [STAThread]
            static 
    void Main() 
            {
                
    Application.Run(new Form1());
            }
        }

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Oh, and if it's still WinForms with your version, you are still using Beta 1 ! Tell your teacher to get a non-beta Version from MS, the Framework and commandline compiler are free.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. Retrieving Multiple inputs with fgets?
    By Axel in forum C Programming
    Replies: 25
    Last Post: 09-13-2005, 04:04 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Multi column Listbox
    By Gravedigga in forum Windows Programming
    Replies: 1
    Last Post: 10-12-2003, 12:20 PM
  5. Listbox multi collum
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 01-02-2002, 08:06 AM