tags:

views:

1036

answers:

7

Can we place C# textBox controls in a C# dropdown combobox?

That is, when the combo is dropped down, each of its items will show a textbox.

+1  A: 

Not in Windows Forms, but in WPF you can put anything in a ComboBox...

Thomas Levesque
+1  A: 

Yes, if you are using WPF.

Scott Whitlock
+6  A: 

yes, example in WPF:

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
     <ComboBox Margin="49,61,75,0" Height="25" VerticalAlignment="Top">
      <ComboBox.Items>    
       <ComboBoxItem>
        <TextBox>TextBox</TextBox>
       </ComboBoxItem>
       <ComboBoxItem>
        <TextBlock>TextBlock</TextBlock>
       </ComboBoxItem>
       <ComboBoxItem>
        <Button>Button</Button>
       </ComboBoxItem>
      </ComboBox.Items>
     </ComboBox>
    </Grid>
</Window>

in Windows Forms combobox could be a hassle.

Andrija
That's awesome!
lb
<3 WPF, stuff doing this in WinForms
Nathan W
+1  A: 

I may be stating the obvious, but with Silverlight as well.

Hunter
A: 

If you're talking about ASP.NET then the answer is No if you're using the standard ASP.NET controls. However, if you create a ComboBox style control using HTML/JavaScript, then Yes you can.

Chris Pietschmann
A: 

I believe JMSA is talking about Windows Forms.

I'm not a .NET expert, but you could possibly create your own OwnerDrawn combo box. There are methods such as OnDrawItem() and MeasureItem() that ListBoxs and other item holding controls have; which can be overriden and you could have total control.

CheckBoxRenderer, etc. are classes that can be found online which take a graphic object and can draw windows forms.

This of course is a very long process: I would recommend looking for a simpler way of accomplishing your task. Me too sometimes I have a crazy client who wants flashy super comboboxes, well .. take the challenge!

lb
+3  A: 

In windows forms, it's not exactly possible, but you can intercept the window message that causes the combobox to drop down and show a panel or form instead.

As a place to start:

public class UserControlComboBox : ComboBox, IMessageFilter
{
    public readonly MyControlClass UserControl = new MyControlClass();

    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == 0x0201) || (m.Msg == 0x0203))
        {
            if (DroppedDown)
                HideUserControl();
            else
                ShowUserControl();
       }
        else
        {
            base.WndProc(ref m);
        }
    }

    public bool PreFilterMessage(ref Message m)
    {
        // intercept mouse events
        if ((m.Msg >= 0x0200) && (m.Msg <= 0x020A))
        {
            if (this.UserControl.RectangleToScreen(this.UserControl.DisplayRectangle).Contains(Cursor.Position))
            {
                // clicks inside the user control, handle normally
                return false;
            }
            else
            {
                // clicks outside the user controlcollapse it.
                if ((m.Msg == 0x0201) || (m.Msg == 0x0203))
                    this.HideUserControl();
                return true;
            }
        }
        else return false;
    }

    public new bool DroppedDown 
    {
        get { return this.UserControl.Visible; } 
    }

    protected void ShowUserControl()
    {
        if (!this.Visible)
            return;

        this.UserControl.Anchor = this.Anchor;
        this.UserControl.BackColor = this.BackColor;
        this.UserControl.Font = this.Font;
        this.UserControl.ForeColor = this.ForeColor;

        // you can be cleverer than this if you need to
        this.UserControl.Top = this.Bottom;
        this.UserControl.Left = this.Left;
        this.UserControl.Width = Math.Max(this.UserControl.Width, this.Width);

        this.Parent.Controls.Add(this.UserControl);
        this.UserControl.Visible = true;
        this.UserControl.BringToFront();

        base.OnDropDown(EventArgs.Empty);

        // start listening for clicks
        Application.AddMessageFilter(this);
    }

    protected void HideUserControl()
    {
        Application.RemoveMessageFilter(this);

        base.OnDropDownClosed(EventArgs.Empty);
        this.UserControl.Visible = false;
        this.Parent.Controls.Remove(this.UserControl);

        // you probably want to replace this with something more sensible
        this.Text = this.UserControl.Text;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.UserControl.Dispose();
        }

        base.Dispose(disposing);
    }
}
Simon
Wow, great info on how to do a WinForms ComboBox with a custom dropdown. Thank you very much for your feedback!
John Conrad