Hi,
hey i need a code to display the text of of runtime generated button in ascending order in windows application c#.

example if i have buttons like
ab
abc
asdfsd
asasasasas
how to get the text in regular order starting from left?

thank,
suhasini

Dani AI

Generated

Two parts to fix this reliably: sort the source before you create controls, and set the correct alignment for the control class you are using. was pointing you in the right direction — sort the data first and don’t rely on manual X/Y math for layout. Using a layout container removes most positioning headaches.

Example approach (conceptual):

  • Sort the data source (either with LINQ over your object collection or with a DataView on the DataTable) by the key you want (text length, alphabetic, case-insensitive, etc.).
  • Create the buttons from that sorted sequence and add them to a FlowLayoutPanel or TableLayoutPanel so controls flow left-to-right and wrap automatically.
  • If you are using DevExpress SimpleButton, set its appearance text options so the caption is left-aligned; for the standard WinForms Button use its TextAlign.

Code snippets you can adapt:

var sorted = visits.OrderBy(v => v.MenuDescription.Length)
                   .ThenBy(v => v.MenuDescription, StringComparer.CurrentCultureIgnoreCase);

foreach (var item in sorted) {
    var btn = new DevExpress.XtraEditors.SimpleButton();
    btn.Text = item.MenuDescription;
    btn.Appearance.Options.UseTextOptions = true;
    btn.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
    btn.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
    flowLayoutPanel1.Controls.Add(btn);
}

If you work from a DataTable you can also use a DataView: set dataTable.DefaultView.Sort = "MenuDescription ASC" before creating controls.

Quick troubleshooting:

  • If you can’t find a TextAlign property, verify the control type. SimpleButton (DevExpress) uses Appearance.TextOptions; System.Windows.Forms.Button uses TextAlign.
  • Use a layout panel (FlowLayoutPanel) to avoid manual Location math and to get predictable left-to-right ordering. See the FlowLayoutPanel docs for behavior and properties: FlowLayoutPanel (WinForms).
  • For LINQ ordering reference: Enumerable.OrderBy.

Recommended Answers

All 7 Replies

Do you mean that the text in your buttons has to be left aligned?

Do you mean that the text in your buttons has to be left aligned?

hey hi,
yes the text on the run time generated buttons should be left aligned or the buttons have 3 charactes shud be displayed at the top and as the lengh goes on incresing they shud be displayed next.

Thank,
suhasini.

The TextAlign property is default set to MiddleCenter, set it to MiddleLeft.
I should make a List of all the text to put into the buttons. Sort the List and then assign every item in the List to the Text property of the buttons.

The TextAlign property is default set to MiddleCenter, set it to MiddleLeft.
I should make a List of all the text to put into the buttons. Sort the List and then assign every item in the List to the Text property of the buttons.

but my buttons are binded with database and are generated at runtime
so im not getting any properties such as middleleft.

Let us see some code.

but my buttons are binded with database and are generated at runtime
so im not getting any properties such as middleleft.

my method is

private void LoadRegVisit()
        {
            int i = 0;
            int j = 0;
            BuzySoft.Domain.RegVisit reg = new BuzySoft.Domain.RegVisit();
            reg.Formid = 27;
            BuzySoft.Domain.RegVisits regVisits = BusinessServices.RegVisitServices.GetRegVisitID(reg);
            DataSet ds = regVisits.DsRegVisits;
            int count = 1;
            foreach (Domain.RegVisit rgn in regVisits)
            {
                btnRun = new SimpleButton();
                btnRun.Tag = rgn.DestinationFormId;
                btnRun.Text = rgn.MenuDescription;
                TextBox.

                // btnRun.AutoSize = true;
                btnRun.Location = new System.Drawing.Point(15, 65 + 30 * i);
                btnRun.Name = rgn.MenuDescription;
               
                btnRun.BackColor = System.Drawing.Color.Black;
                btnRun.Size = new System.Drawing.Size(190, 30);

                this.Controls.Add(btnRun);
                if (count == 2)
                {
                    btnRun.Location = new System.Drawing.Point(200, 65 + 60 * j);
                    j++;
                    count = 0;
                }
                count++;
                // groupBox1.Controls.Add(newlabel);
                //.Size = new System.Drawing.Size(100, 25);
                //this.Controls.Add(btnRun);ds.Tables[0].Rows.Count
                btnRun.Click += new EventHandler(abc);
                i++;

                
            }

so i need some property to set the text in ascending order.

Please use code tags when you post code. What would be wrong if instead of this

btnRun = new SimpleButton();
btnRun.Tag = rgn.DestinationFormId;

you did this

btnRun = new SimpleButton();
btnRun.TextAlign = MiddleLeft;
btnRun.Tag = rgn.DestinationFormId;

Now sort rgn.MenuDescription first and then add it to the Text property.

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.