WinForm application ComboBox ReadOnly function to achieve

Today encountered when doing a software problem, recorded after the backup, huh:

There are three property DropDownStyle ComboBox control:
1.Simple TextBox similar appearance, the text portion can be edited, Text control value can be displayed, the items Items rely on keyboard "↑", "↓" is selected.
2.DropDown can edit the text portion, Text control values can be displayed, Items items in the drop-down selection box to select the "▼" click controls appear.
3.DropDownList text part can not be edited, you can not set the control's Text value, the drop-down box to select items Items appearances by "▼" click to select the control.

Originally DropDown is to use a similar style, set the control's Text value at the start As a reminder, but when the user selects the option, you can not let him edit content options, would have thought there would be a ReadOnly property
and then set about it, I did not think of the list of properties one by one read it several times just can not find, I really do not know how to think when MS was designed to control, but to see if I can find information to achieve such a function,

Looking for a long found with windows API to implement relatively simple (and it seems windows API windows API is really very very strong)
reference a namespace:
a using System.Runtime.InteropServices;
API used in the statement:
[DllImport ( "user32. DLL ", the CharSet = CharSet.Auto, ExactSpelling = to true)]
        public static extern IntPtr the GetWindow (IntPtr the hWnd, int uCmd);
        int = the GW_CHILD. 5;
       
[the DllImport (" User32.dll ", the CharSet = CharSet.Auto)]
        public static IntPtr the SendMessage extern (IntPtr the hWnd, int MSG, the wParam int, int the lParam);
        public int const = EM_SETREADONLY 0xCF;

1. Get a handle ComboBox control to be set by GetWindow API.
2. Handle with SendMessage API to obtain the read-only attribute set.


Sample code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
        int GW_CHILD = 5;
       
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
        public const int EM_SETREADONLY = 0xcf;

 

        public Form1()
        {
            InitializeComponent();
            IntPtr editHandle = GetWindow(comboBox1.Handle , GW_CHILD);
            SendMessage(editHandle,EM_SETREADONLY,1,0);
        }        
    }
}


Others used the principle of interpretation:
1.comboBox actually a nested controls (composite controls) in the DropDownList state; he made the drop-down list, and ComboBox itself composed
  when DropDown ComboBox state in more than a TextBox edit is under .net that state is controlled by the input of the edit;
  but this edit is this.comboBox1.Controls.Count not available in .net return 0.
2. obtain control handle with AIP.
3. Set the control to read-only status.
  This is a message you can get the basic (but forget about the news), take a look msdn beginning em_ find news, find EM_SETREADONLY look at the name that is him;
  according to the SDK rules, the message is beginning em_ of the corresponding edit.

Reproduced in: https: //my.oschina.net/secyaher/blog/274470

Guess you like

Origin blog.csdn.net/weixin_33794672/article/details/91967147