简体   繁体   中英

Bring a control to front

I have a user control that uses a textbox and a list box. List box isn't visible, it only becomes visible when user starts typing or click in text box.

I have added the user control to a group box which is on the form.

Now when the listox becomes visible, it stays inside the group box, and can't see the full height. I wan't it float on top so that i can see the full height.

I have looked around, implemented some solutions but nothing worked for me.

Constructor for the user control

namespace YarCustomControl
{
    public partial class YarCustom : TextBox 
    {
        public YarCustom()
        {
            InitializeComponent();

            _code = "";
            _id = -1;

            //list box handling
            listBox = new ListBox();
            listBox.Visible = false;
            listBox.Font = this.Font;
            listBox.Location = this.Location;
            listBox.BorderStyle = BorderStyle.Fixed3D;
            listBox.Resize += new EventHandler(listBox_Resize);
            //listBox.SelectedValueChanged += new EventHandler(listBox_SelectedValueChanged);
            listBox.KeyDown += new KeyEventHandler(listBox_KeyDown);
            listBox.Click += new EventHandler(listBox_Click);

            //test => no affect on listbox
            this.Controls.Add(listBox);

            listBox.Visible = false;

        }
}
}

and the following method makes the listbox visible. Both SetchildIndex (commented and not commented) throw an error

private void makeListBoxVisible()
        {
            Form parentForm = (this.FindForm() as Form);

            //parentForm.Controls.SetChildIndex(listBox, 0);
            this.Controls.SetChildIndex(listBox, 0);
            listBox.Visible = true;
            listBox.BringToFront();
        }

在此处输入图片说明

What is the best approach for handling something like this?

My environment is VS2010 and WinForms.

Now when the listox becomes visible, it stays inside the group box, and can't see the full height. I wan't it float on top so that i can see the full height.

Simply put it directly on the Form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM