472,096 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Listbox in c#

How do I display a listbox on another windows form?

here's what I have, I want to display a new windows form after a button is clicked , and on this windows form. I want to display a listbox with a collection of items on the new created windows form. How do I go about doing this?

I tried adding a windows form as a new item. But I still don't know how to take the listbox1(with collection of items) from form 1 and display it on form 2 to the user?

Any help would be appreciated
Jul 18 '07 #1
9 9154
Atran
319 100+
How do I display a listbox on another windows form?

here's what I have, I want to display a new windows form after a button is clicked , and on this windows form. I want to display a listbox with a collection of items on the new created windows form. How do I go about doing this?

I tried adding a windows form as a new item. But I still don't know how to take the listbox1(with collection of items) from form 1 and display it on form 2 to the user?

Any help would be appreciated
Hello:
Use this code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsApplication1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void button1_Click(object sender, EventArgs e)
  15.         {
  16.             //Form2 is a new form.
  17.             Form Form2 = new Form();
  18.             //Form2 text.
  19.             Form2.Text = "Form2";
  20.             //Show the Form2.
  21.             Form2.Show();
  22.             //
  23.             //Creating a ListBox.
  24.             ListBox ListBox1 = new ListBox();
  25.             //Add items to Listbox1.
  26.             ListBox1.Items.Add("Hello C#");
  27.             ListBox1.Items.Add("Hello VB");
  28.             //Location of Listbox1.
  29.             ListBox1.Location = new Point(12, 12);
  30.             //Add listbox1 to Form2 controls.
  31.             Form2.Controls.Add(ListBox1);
  32.         }
  33.  
  34.     }
  35. }
  36.  
Hope I helped you.
Jul 18 '07 #2
Thanks, for the quick response. That did create a new form and a new listbox. but I'm still having a minor problem. What I am trying to do is populate a list of sql databases in that listbox and all the sql tables in another listbox on the new form. I'm able to get all the sql databases when I tested my code, and I believe my code gets all the sql tables. Can you please explain to me, why I am getting incorrect results in the listbox when I try and insert the databases and tables into the listbox's in the new form.

//get databases
ListBox listbox1 = new ListBox();
listbox1.Size = new System.Drawing.Size(200, 100);
listbox1.Location = new System.Drawing.Point(10, 10);
Form form = new Form();
DataTable database = myConnection.GetSchema("Databases");
foreach (DataRow row in database.Rows)
listbox1.Items.Add(myConnection.GetSchema("Databas es"));
form.Controls.Add(listbox1);
form.Show();
//MessageBox.Show("Database: " + ["database_name"]);
// get tables
DataTable tables = new DataTable("Tables");
SqlCommand command = myConnection.CreateCommand();
command.CommandText = "SELECT table_name as Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE='BASE TABLE'";
tables.Load(command.ExecuteReader());


Thanks
Jul 18 '07 #3
Plater
7,872 Expert 4TB
This seems a bit suspect:
Expand|Select|Wrap|Line Numbers
  1. DataTable database = myConnection.GetSchema("Databases");
  2. foreach (DataRow row in database.Rows)
  3.    listbox1.Items.Add(myConnection.GetSchema("Databases"));
  4.  
Shouldn't you be adding in some value from row, and not the whole collection again?
Maybe like
Expand|Select|Wrap|Line Numbers
  1. DataTable database = myConnection.GetSchema("Databases");
  2. foreach (DataRow row in database.Rows)
  3.    listbox1.Items.Add(row.Cells[3].Value.ToString());
  4.  
(There are probably typos in there, but you should see what I mean)

Expand|Select|Wrap|Line Numbers
  1. //get databases
  2. ListBox listbox1 = new ListBox();
  3. listbox1.Size = new System.Drawing.Size(200, 100);
  4. listbox1.Location = new System.Drawing.Point(10, 10);
  5. Form form = new Form();   
  6. DataTable database = myConnection.GetSchema("Databases");
  7. foreach (DataRow row in database.Rows)
  8.    listbox1.Items.Add(myConnection.GetSchema("Databases"));
  9. form.Controls.Add(listbox1);
  10. form.Show();
  11. //MessageBox.Show("Database: " + ["database_name"]);
  12. // get tables
  13. DataTable tables = new DataTable("Tables");
  14. SqlCommand command = myConnection.CreateCommand();
  15. command.CommandText = "SELECT table_name as Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE='BASE TABLE'";
  16. tables.Load(command.ExecuteReader());
  17.  
Jul 18 '07 #4
Thanks for the help, I was able to figure out how to insert the new items into the listbox and display them on a new windows form. I have another question though, My listbox on the new form is full of database names, So how do I display the sql tables when the user selects a database from the listbox on the new form and display the sql tables in another listbox on the same form since everything is being created dynamically?

Can someone please help
This is what I tried
//get databases
Label labels = new Label();
labels.Location = new System.Drawing.Point(10, 35);
labels.Text = "Select a database from the list";
ListBox listbox1 = new ListBox();
listbox1.Size = new System.Drawing.Size(100, 100);
listbox1.Location = new System.Drawing.Point(10, 60);
Form form = new Form();
DataTable database = myConnection.GetSchema("Databases");
foreach (DataRow row in database.Rows)
listbox1.Items.Add(row["database_name"]);
form.Controls.Add(listbox1);
form.Controls.Add(labels);
string selectedString = listbox1.SelectedIndex.ToString();
// get tables
ListBox listbox2 = new ListBox();
listbox2.Size = new System.Drawing.Size(100, 100);
listbox2.Location = new System.Drawing.Point(120, 70);
Label label = new Label();
label.Location = new System.Drawing.Point(40, 35);
labels.Text = "Select a table from list";
DataTable tables = new DataTable("Tables");
SqlCommand command = myConnection.CreateCommand();
command.CommandText = "SELECT table_name as Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE='BASE TABLE'";
tables.Load(command.ExecuteReader());
foreach (DataRow table in database.Rows)
listbox2.Items.Add(tables);
form.Controls.Add(listbox2);
form.Controls.Add(label);
form.Show();

This seems a bit suspect:
Expand|Select|Wrap|Line Numbers
  1. DataTable database = myConnection.GetSchema("Databases");
  2. foreach (DataRow row in database.Rows)
  3.    listbox1.Items.Add(myConnection.GetSchema("Databases"));
  4.  
Shouldn't you be adding in some value from row, and not the whole collection again?
Maybe like
Expand|Select|Wrap|Line Numbers
  1. DataTable database = myConnection.GetSchema("Databases");
  2. foreach (DataRow row in database.Rows)
  3.    listbox1.Items.Add(row.Cells[3].Value.ToString());
  4.  
(There are probably typos in there, but you should see what I mean)
Jul 19 '07 #5
Atran
319 100+
Hello:
Try this code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         Form form;
  11.         ListBox listbox;
  12.         DataTable table;
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.             form = new Form();
  21.             listbox = new ListBox();
  22.             table = new DataTable();
  23.             listbox.Location = new Point(12, 12);
  24.             listbox.Size = new Size(200, 150);
  25.             form.Controls.Add(listbox);
  26.             form.Show();
  27.  
  28.             DataTableReader reader = new DataTableReader(dataTable());
  29.             table.Load(reader);
  30.             foreach (DataRow row in table.Rows)
  31.             {
  32.                 for (int i = 0; i < table.Columns.Count; i++)
  33.                 {
  34.                     listbox.Items.Add(row[i] + " ");
  35.  
  36.                 }
  37.             }
  38.         }
  39.  
  40.         private static DataTable dataTable()
  41.         {
  42.             DataTable table = new DataTable();
  43.             DataColumn idColumn = table.Columns.Add("ID", typeof(int));
  44.             table.Columns.Add("Name", typeof(string));
  45.             table.PrimaryKey = new DataColumn[] { idColumn };
  46.             table.Rows.Add(new object[] { 1, "TheScripts" });
  47.             table.Rows.Add(new object[] { 2, ".Net" });
  48.             table.Rows.Add(new object[] { 3, "Programmingr" });
  49.             table.Rows.Add(new object[] { 4, "Fun" });
  50.             table.AcceptChanges();
  51.             return table;
  52.         }
  53.     }
  54. }
  55.  
Atran.
Jul 20 '07 #6
Thanks for the help Atran. I have been working on this problem the past few days and I'm still stuck. I wrote a function that returns the datatable, but the problem is that I don't know what to write in my listbox double click event to load the tables.

Any Suggestions????
Here's what I have:
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4. get_db_tables();
  5. }
  6.  
  7. public DataTable get_db_tables()
  8.         {
  9.             //get databases
  10.             Label labels = new Label();
  11.             labels.Location = new System.Drawing.Point(10, 35);
  12.             labels.Text = "Select a database";
  13.             ListBox listbox1 = new ListBox();
  14.             listbox1.Size = new System.Drawing.Size(100, 100);
  15.             listbox1.Location = new System.Drawing.Point(10, 60);
  16.             listbox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
  17.             DataTable database = dbConnect("HPMS3", "Databases");//myConnection.GetSchema("Databases");
  18.             foreach (DataRow row in database.Rows)
  19.             {                
  20.                 // if (row.ItemArray[0].ToString() == "HPMS3")
  21.                 // {
  22.                 listbox1.Items.Add(row["database_name"]);
  23.                 //}
  24.             }
  25.             Form form = new Form();
  26.             form.Controls.Add(listbox1);
  27.             form.Controls.Add(labels);
  28.             form.Show();
  29.             // get tables
  30.             ListBox listbox2 = new ListBox();
  31.             listbox2.Size = new System.Drawing.Size(140, 100);
  32.             listbox2.Location = new System.Drawing.Point(150, 60);
  33.             listbox2.Visible = false;
  34.             // 
  35.             //   string[] restrictions = new string[4];
  36.             //  restrictions[0] = "HPMS3";
  37.  
  38.             DataTable tables = dbConnect("HPMS3", "Tables"); //myConnection.GetSchema("Tables", restrictions);
  39.             foreach (DataRow rowTable in tables.Rows)
  40.             {
  41.                 for (int i = 0; i < tables.Columns.Count; i++)
  42.                 {
  43.                     listbox2.Items.Add(rowTable[i].ToString());
  44.                 }
  45.             }
  46.                 Label label2 = new Label();
  47.             label2.Location = new System.Drawing.Point(140, 35);
  48.             label2.Text = "Select table";
  49.             form.Controls.Add(listbox2);
  50.             form.Controls.Add(label2);
  51.             form.Show();
  52.            return tables;
  53.         }
  54.  
  55. private void listBox1_DoubleClick(object sender, EventArgs e)
  56.         {
  57.  
  58.           //DON'T KNOW WHAT CODE TO INCLUDE HERE TO SHOW 
  59. THE TABLES AFTER THE LISTBOX WITH DATABASES IS CLICKED
  60.  
  61.         }
OUTPUT:
Everything works fine, I am able to connect to the server, show the new form, with a listbox of all the databases. My problem is I don't know how to show the tables in another listbox after a database is selected from the listbox on the same form

Can anyone help?

Hello:
Try this code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         Form form;
  11.         ListBox listbox;
  12.         DataTable table;
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.             form = new Form();
  21.             listbox = new ListBox();
  22.             table = new DataTable();
  23.             listbox.Location = new Point(12, 12);
  24.             listbox.Size = new Size(200, 150);
  25.             form.Controls.Add(listbox);
  26.             form.Show();
  27.  
  28.             DataTableReader reader = new DataTableReader(dataTable());
  29.             table.Load(reader);
  30.             foreach (DataRow row in table.Rows)
  31.             {
  32.                 for (int i = 0; i < table.Columns.Count; i++)
  33.                 {
  34.                     listbox.Items.Add(row[i] + " ");
  35.  
  36.                 }
  37.             }
  38.         }
  39.  
  40.         private static DataTable dataTable()
  41.         {
  42.             DataTable table = new DataTable();
  43.             DataColumn idColumn = table.Columns.Add("ID", typeof(int));
  44.             table.Columns.Add("Name", typeof(string));
  45.             table.PrimaryKey = new DataColumn[] { idColumn };
  46.             table.Rows.Add(new object[] { 1, "TheScripts" });
  47.             table.Rows.Add(new object[] { 2, ".Net" });
  48.             table.Rows.Add(new object[] { 3, "Programmingr" });
  49.             table.Rows.Add(new object[] { 4, "Fun" });
  50.             table.AcceptChanges();
  51.             return table;
  52.         }
  53.     }
  54. }
  55.  
Atran.
Jul 26 '07 #7
Atran
319 100+
Thanks for the help Atran. I have been working on this problem the past few days and I'm still stuck. I wrote a function that returns the datatable, but the problem is that I don't know what to write in my listbox double click event to load the tables...........
Can anyone help?
Hello:
Try this code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsApplication
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         DataTable table;
  11.         ListBox listbox1;
  12.         Form form;
  13.         DataTableReader reader;
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             form = new Form();
  18.             form.Show();
  19.  
  20.             listbox1 = new ListBox();
  21.             listbox1.Size = new System.Drawing.Size(100, 100);
  22.             listbox1.Location = new System.Drawing.Point(10, 60);
  23.             listbox1.DoubleClick += new EventHandler(listbox1_DoubleClick);
  24.             form.Controls.Add(listbox1);
  25.         }
  26.  
  27.         void listbox1_DoubleClick(object sender, EventArgs e)
  28.         {
  29.             DoIt();
  30.         }
  31.         public void DoIt()
  32.         {
  33.             table = new DataTable();
  34.             reader = new DataTableReader(dataTable());
  35.             table.Load(reader);
  36.             foreach (DataRow row in table.Rows)
  37.             {
  38.                 for (int i = 0; i < table.Columns.Count; i++)
  39.                 {
  40.                     listbox1.Items.Add(row[i] + " ");
  41.                 }
  42.             }
  43.         }
  44.  
  45.         private static DataTable dataTable()
  46.         {
  47.             DataTable table = new DataTable();
  48.             DataColumn idColumn = table.Columns.Add("ID", typeof(int));
  49.  
  50.             table.Columns.Add("Name", typeof(string));
  51.             table.PrimaryKey = new DataColumn[] { idColumn };
  52.             table.Rows.Add(new object[] { 1, "TheScripts" });
  53.             table.Rows.Add(new object[] { 2, ".Net" });
  54.             table.Rows.Add(new object[] { 3, "Programmingr" });
  55.             table.Rows.Add(new object[] { 4, "Fun" });
  56.             table.AcceptChanges();
  57.             return table;
  58.         }
  59.     }
  60. }
  61.  
Hope it helped you.
Jul 26 '07 #8
Hi, Atran
I tried to declare the Datatable and the form globally so that I would be able to use it in my application to dynamically create and display the listbox. The problem i'm currently having is by doing so the tables are null, which means they don't return anything or have no value. I'm still having problems on the listbox1 doubleclick event. My application still doesn't display the 2nd listbox with the tables, my code just breaks at a particular line. Any suggestions? I think I'm taking the right approach, I'm just missing something very subtle in my code.

here's my code:
Expand|Select|Wrap|Line Numbers
  1. namespace Project_3
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         ListBox listbox1;
  6.         ListBox listbox2;  
  7.         Form form=new Form();
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.  
  13.         private void label1_Click(object sender, EventArgs e)
  14.         {
  15.  
  16.         }
  17.  
  18.         private void textBox1_TextChanged(object sender, EventArgs e)
  19.         {
  20.  
  21.         }
  22.  
  23.  private void button1_Click(object sender, EventArgs e)
  24.         {  
  25. get_db_tables();
  26. }
  27. public DataTable get_db_tables()
  28.         {
  29.             //get databases
  30.             Label labels = new Label();
  31.             labels.Location = new System.Drawing.Point(10, 35);
  32.             labels.Text = "Select a database";
  33.              listbox1 = new ListBox();
  34.             listbox1.Size = new System.Drawing.Size(100, 100);
  35.             listbox1.Location = new System.Drawing.Point(10, 60);
  36.             listbox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
  37.            DataTable database = dbConnect("HPMS3 ", "Databases");//myConnection.GetSchema("Databases");
  38.             foreach (DataRow row in database.Rows)
  39.             {             
  40.                 // if (row.ItemArray[0].ToString() == "HPMS3")
  41.                 // {
  42.                // for(int count=0; count<
  43.                 listbox1.Items.Add(row["database_name"]);
  44.                 //}
  45.             }
  46.             //Form form = new Form();
  47.             form.Controls.Add(listbox1);
  48.             form.Controls.Add(labels);
  49.             form.Show();           
  50.             return database;
  51.         }
  52.  
  53. private void listBox1_DoubleClick(object sender, EventArgs e)
  54.         {
  55.  
  56.             Tables();
  57.         }
  58.         public void Tables()
  59.         {
  60.             // get tables
  61.             listbox2 = new ListBox();
  62.             listbox2.Size = new System.Drawing.Size(140, 100);
  63.             listbox2.Location = new System.Drawing.Point(150, 60);
  64.             listbox2.Visible = false;
  65.             //   string[] restrictions = new string[4];
  66.             //  restrictions[0] = "HPMS3";
  67.             DataTable database=new DataTable();
  68.            DataTable tables=new DataTable();
  69.            tables = dbConnect("", "Tables"); //myConnection.GetSchema("Tables", restrictions);
  70.             foreach (DataRow rowTable in tables.Rows)
  71.             {
  72.                 for (int i = 0; i < database.Columns.Count; i++)
  73.                 {
  74.                     listbox2.Items.Add(rowTable[i].ToString());
  75.                 }
  76.             }
  77.             Label label2 = new Label();
  78.             label2.Location = new System.Drawing.Point(140, 35);
  79.             label2.Text = "Select table";
  80.            form.Controls.Add(listbox2);
  81.             form.Controls.Add(label2);            
  82.             form.Show(listbox2);
  83.             listbox2.Visible = true;
  84.         }
  85.         private DataTable dbConnect(string dbase, string schema)
  86.         {
  87.             SqlConnection myConnection = new SqlConnection();
  88.             DataTable table = null;
  89.  
  90.             myConnection.ConnectionString = "Data Source=ipaddress, 1433; Network library=DBMSSOCN; Initial Catalog=" + dbase + ";User=user;Password=password ";
  91.             SqlCommand thisCommand = myConnection.CreateCommand();
  92.             try
  93.             {
  94.                 myConnection.Open();
  95.                 textBox1.Text = "Connected!";
  96.                 table = myConnection.GetSchema(schema);
  97.             }
  98.             catch (Exception ex)
  99.             {
  100.                 MessageBox.Show(ex.Message);
  101.                 MessageBox.Show(ex.StackTrace);
  102.                 MessageBox.Show("Failed to connect!");
  103.                 MessageBox.Show("Could not connect to database, please try again!");
  104.                 Application.Restart();
  105.                 //throw(ex);
  106.  
  107.                 // create dataset to hold rows, columns, etc
  108.                 // DataSet dataset = new DataSet();
  109.             }
  110.             finally
  111.             {
  112.                 if (myConnection != null)
  113.                     myConnection.Close();
  114.             }
  115.             return table; 
Jul 27 '07 #9
hi, i saw your codings. And i wish to ask about one thing.

Is it possbile to put the connection data inside web method rather than putting here.

can help ?
Nov 27 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by amber | last post: by
3 posts views Thread by Paul T. Rong | last post: by
8 posts views Thread by Oddball | last post: by
6 posts views Thread by Chris Leuty | last post: by
7 posts views Thread by Dave | last post: by
3 posts views Thread by Ali Chambers | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.