Cos Callis
Cos Callis

Reputation: 5084

Display Format for a .NET Winforms Combobox to Upper Case

I have an array of objects of type Process I want to display this list in a combobox, alphabetized and in all caps.

The Process object property "ProcessName" is the 'DisplayMember'; it is a readonly property.

        private void Form1_Load(object sender, EventArgs e)
    {
        //get the running processes
        Process[] procs = Process.GetProcesses();
        //alphabetize the list.
        var orderedprocs = from p in procs orderby p.ProcessName select p;
        //set the datasource to the alphabetized list
        comboBox1.DataSource = orderedprocs.ToArray<Process>();
        comboBox1.DisplayMember = "ProcessName";

        // Make the display member render as UPPER CASE???
        //comboBox1.FormatString


    }

I suspect that the answer lies in the FormatString

Upvotes: 2

Views: 2206

Answers (1)

keyboardP
keyboardP

Reputation: 69372

You could format each item when they're added by subscribing to the Format event.

comboBox1.Format += (s, e) =>
    { 
         e.Value = e.Value.ToString().ToUpperInvariant();
    };

But note that when you do this the first item will be selected so your SelectedValueChanged event will fire unless you attach the Format event handler before attaching the SelectedValueChanged event handler.

Upvotes: 2

Related Questions