Hi,my name is yawar i am a student learning c#.I have combobox,when i select an item in combobox there comes data which i want to display on labels,but my problem is there comes many data,i want to display only 3 labels when only 3 data rows comes from database and when 4 data rows comes i want to diplay 4 labels.please help me.

Recommended Answers

All 11 Replies

>creating labels at runtime.

Instantiate the control class and add this object into form collection.

...
 Label sam1=new Label();
 sam1.Text="something.";
 sam1.Location=new Point(30,30);
 this.Controls.Add(sam1);
...

>creating labels at runtime.

Instantiate the control class and add this object into form collection.

...
 Label sam1=new Label();
 sam1.Text="something.";
 sam1.Location=new Point(30,30);
 this.Controls.Add(sam1);
...

Hi,i have tried it but i want to control the generation of labels,i have tried it in (if,else)statement,for e.g if data coming from database are 3 names only 3 labels will be generated to display 3 names and if 4 then 4 labels will be generated to display names,plz help me.

Could you please specify what exactly it is you want, because adatapost has given you the code to create a label at runtime. If it can be done once it can be done 3 or 4 times or more also.

Could you please specify what exactly it is you want, because adatapost has given you the code to create a label at runtime. If it can be done once it can be done 3 or 4 times or more also.

i want to say that i want to display names from the database table names in which i have a column naming nick_names,i have a combobox,when a user click a item on the combobox for e.g user clicks on nick names item it should dispay all the nick names in that column,my problem is that,when i click the combobox item i don't know how many names will come from database table for that purpose i want to generate labels according to the names coming from database,if 3 names come then only 3 labels will generate to display those 3 names if 4 then 4,i hope now you will understand my problem.

You just repeated your original question. Call me dumb, but I still do not understand what exactly it is you want.

You just repeated your original question. Call me dumb, but I still do not understand what exactly it is you want.

i want to display names coming from database when a user click on a item from combo box it,the names should display below that combo box on the same form.

Perhaps showing some code would help. Untill now I think you mean something like this:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // if we want to make 3 labels we could do this:
            Label Label1 = new Label();             //create a label
            Label1.Location = new Point(30, 30);    //put it somewhere on the form
            Label1.Text = "firstitem from DB";      //assign data from DB to label
            this.Controls.Add(Label1);              //add label to the form control collection

            Label Label2 = new Label();
            Label2.Location = new Point(30, 60);
            Label2.Text = "seconditem from DB";
            this.Controls.Add(Label2);

            Label Label3 = new Label();
            Label3.Location = new Point(30, 90);
            Label3.Text = "thirthitem from DB";
            this.Controls.Add(Label3);
        }
    }

Hi
it would be good if you see this code
i am creating a label for each item in tha combo box

public partial class Form1 : Form
    {
        ComboBox cb ;
        Label lb;

        public Form1()
        {
            InitializeComponent();
            cb = new ComboBox();
            cb.Location = new Point(20, 20);
            cb.Items.Add("José");
            cb.Items.Add("Ivan");
            cb.Items.Add("JuancaBicho");
            cb.Items.Add("Jhonatan");
            cb.Items.Add("Victor");
            this.Controls.Add(cb);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in cb.Items)
            {
                lb = new Label();
                lb.MouseDown += new MouseEventHandler(this.lb_MouseDown);
                lb.MouseMove += new MouseEventHandler(this.lb_MouseMove);
                lb.Name = item.ToString();
                lb.Text = item.ToString();
                lb.Visible = true;
                this.Controls.Add(lb);
            }
        }

        private int x = 0;
        private int y = 0;

        void lb_MouseDown(object Sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
        }

        void lb_MouseMove(object Sender, MouseEventArgs e)
        {
            Label lb = (Label)Sender;
            if (e.Button == MouseButtons.Left)
            {
                lb.Left = (lb.Left + e.X) - x;
                lb.Top = (lb.Top + e.Y) - y;
            }
        }
    }

Regards from Peru
by jMosquera
Net Cell Continental

Hi,
I'm santhosh,
Above answer is more helpful for me and i have one question,
How to assign the label values into picturebox ? I creat the Piechart on the picturebox at RunTime and i want to Shown the label values into the piechart..Plz help me..

Please don't resurrect old threads.
Start a new thread with your question and perhaps refer to this 6 years old thread.
Be as specific as possible with your question. Perhaps add in a picture. Show some of your relevant code.
More chance of getting an asnwer!

I found the answer ..
Thanks for the responce...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.