-1

Hi Guys okay so i have a database for this we will use a simple database.txt as reference

Text Database For Population 
Peter,Pan
James,Dean
Hal,lo
Calc,exe
Mad,Hatter
Patterson,Matthiew

I want to be able to add a button containing for example

 [Peter] , [James], ext ext

For Each Read Out Of The Database How Would I Pressure the population in this instance considering i have to create a button for each new name that is read from the database

Thanks In Advance

P.S. Initially i Used a dropdown with the following code

        List<MDB.Userdb> packObjects = new System.Collections.Generic.List<MDB.Userdb>();
        string colname = "UserRef";
        try
        {
            packObjects = MDB.User.RetrieveList();


            if (!dgvData.Columns.Contains(colname))
            {
                DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
                cb.DataPropertyName = colname;
                cb.Name = colname;
                cb.HeaderText = colname;
                cb.DataSource = packObjects;
                cb.ValueMember = "Idx";
                cb.DisplayMember = "UserName";
                dgvData.Columns.Add(cb);
            }
            else
            {
                //refresh data in drop down
                DataGridViewComboBoxColumn ocb = (DataGridViewComboBoxColumn)dgvData.Columns[colname];
                ocb.DataPropertyName = colname;
                ocb.Name = colname;
                ocb.HeaderText = colname;
                ocb.DataSource = packObjects;
                ocb.ValueMember = "Idx";
                ocb.DisplayMember = "UserName";
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to Retrieve User Detials " + ex.Message, "Retrieve User Detials", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Notes: WinForms c# .Net4.0

10
  • What I got from it is : You want to create button for all the person present in you text database right? Previously you have done it with Drop down. Commented Apr 10, 2015 at 6:40
  • @SandeepKushwah you are correct this is what im attempting to do Commented Apr 10, 2015 at 6:45
  • What all you tried? googling? any links you referred? please paste them here. Commented Apr 10, 2015 at 6:50
  • I have tried googeling but im usure on how to approach this this is why I asked the question should I create an event with the buttons header size and parameters and just read the names out of a database into anlit and add them to the button one by one ? Commented Apr 10, 2015 at 6:52
  • Any links you visited please paste here. Commented Apr 10, 2015 at 6:53

2 Answers 2

0

You can try the below code :

for (int i = 0; i < 5; i++)
        {
            Button newPanelButton = new Button();
            newPanelButton.Name = "txtRuntime" + i;
            newPanelButton.Text = "Runtime"+i;
            newPanelButton.Location = System.Drawing.Point.Add(new Point(4 + i * 307, 4), new Size(20, 20));// here make use of your logic.

            this.Controls.Add(newPanelButton);
        }
Sign up to request clarification or add additional context in comments.

Comments

0
StreamReader menu = new StreamReader("database.txt");
        int repetition = 0;

        while (!menu.EndOfStream)
        {
            Button dynamicbutton = new Button();
            dynamicbutton.Click += new System.EventHandler(menuItem_Click);
            dynamicbutton.AutoSize = true;
            dynamicbutton.Text = menu.ReadLine();
            dynamicbutton.Visible = true;
            dynamicbutton.Location = new Point(4 + repetition * 307, 4);
            dynamicbutton.Height = 24;
            dynamicbutton.Width = dynamicbutton.Text.Length * 20;
            dynamicbutton.BackColor = Color.FromArgb(0, 0, 0);
            dynamicbutton.ForeColor = Color.White;
            dynamicbutton.Font = new Font("Arial", 7);
            dynamicbutton.Show();
            splitContainer1.Panel1.Controls.Add(dynamicbutton);
            repetition++;
        }
        menu.Close();

This Does The Trick With A Few Extra Things In The MenuItem_Click Event i Can Also Retrieve The Name Of The Button And So Forth

3 Comments

I have cheked this solution but it not working properly.. there is problem on new point line. So replace that line by what I have suggested.
@SandeepKushwah i did add the point you suggested and it came up as this peter {EnterTab] james{enterTab] where this one does not doe this but please do post your answer as an answer so i can mark this as resolved
I am happy that you finally got what you were looking for. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.