我想做个winform中类似于属性面板中font中控件,也就是说当焦点没在这个控件的时候,看起来像个textbox,当焦点在这个控件的时候,最后面会有个小的button或者向下的箭头!

解决方案 »

  1.   

    LZ你是想要调整字体的效果,还是要个COMBOX下拉单效果?还是想要WORD里直接下拉单选字体的效果?
      

  2.   

    properygrid
    http://www.codeproject.com/KB/cpp/DynamicDropDownProperties.aspx
      

  3.   

    你用自定义控件,在上面放一个textbox和butten,然后用代码获取到鼠标的焦点,当textbox获取到的时候butten显示,否则的话隐藏,然后其他操作你在butten里操作就可以了。不知道对不对,你可以参考一下,我没有实验。
      

  4.   

    下面的代码基本实现你要的功能,不过这仅仅是个示例。而且只能显示字符串,你参考一下吧思路就是使用了一个UserControl,里面有一个ComboBox和一个Label,当需要编辑时就显示combobox,不需要编辑了就显示label
            public class ComboBoxControl : UserControl
        {
            private Label m_Label;        private ComboBox m_ComboBox;        private void InitializeComponent()
            {
                this.m_ComboBox = new System.Windows.Forms.ComboBox();
                this.m_Label = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // m_ComboBox
                // 
                this.m_ComboBox.Dock = System.Windows.Forms.DockStyle.Fill;
                this.m_ComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.m_ComboBox.FormattingEnabled = true;
                this.m_ComboBox.Location = new System.Drawing.Point(0, 0);
                this.m_ComboBox.Name = "m_ComboBox";
                this.m_ComboBox.Size = new System.Drawing.Size(150, 21);
                this.m_ComboBox.TabIndex = 0;
                this.m_ComboBox.Visible = false;
                this.m_ComboBox.LostFocus += new System.EventHandler(this.m_ComboBox_LostFocus);
                this.m_ComboBox.SelectedIndexChanged += new System.EventHandler(this.m_ComboBox_SelectedIndexChanged);
                // 
                // m_Label
                // 
                this.m_Label.BackColor = System.Drawing.Color.Transparent;
                this.m_Label.Dock = System.Windows.Forms.DockStyle.Fill;
                this.m_Label.Location = new System.Drawing.Point(0, 0);
                this.m_Label.Name = "m_Label";
                this.m_Label.Size = new System.Drawing.Size(150, 21);
                this.m_Label.TabIndex = 1;
                this.m_Label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                this.m_Label.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_Label_MouseDown);
                // 
                // ComboBoxControl
                // 
                this.BackColor = System.Drawing.SystemColors.Window;
                this.Controls.Add(this.m_Label);
                this.Controls.Add(this.m_ComboBox);
                this.Name = "ComboBoxControl";
                this.Size = new System.Drawing.Size(150, 21);
                this.ResumeLayout(false);        }        public ComboBoxControl()
            {
                InitializeComponent();
            }        protected override void OnGotFocus(EventArgs e)
            {
                base.OnGotFocus(e);
                ChangeInnerControlState(true);
            }        public ComboBox.ObjectCollection Items
            {
                get { return m_ComboBox.Items; }
            }        public object SelectedItem
            {
                get { return m_ComboBox.SelectedItem; }
                set { m_ComboBox.SelectedItem = value; }
            }        public int SelectedIndex
            {
                get { return m_ComboBox.SelectedIndex; }
                set { m_ComboBox.SelectedIndex = value; }
            }        private void m_Label_MouseDown(object sender, MouseEventArgs e)
            {
                ChangeInnerControlState(true);
            }        void m_ComboBox_LostFocus(object sender, EventArgs e)
            {
                ChangeInnerControlState(false);
            }        private void ChangeInnerControlState(bool isGotFocus)
            {
                m_ComboBox.Visible = isGotFocus;
                m_Label.Visible = !isGotFocus;
                if (isGotFocus) 
                    m_ComboBox.Focus();
            }        private void m_ComboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                m_Label.Text = m_ComboBox.SelectedItem.ToString();
            }
        }