如何让组合框不接受Excel-Vba中的用户输入并允许vba.value更改?

时间:2016-06-09 20:28:32

标签: excel-vba combobox vba excel

我的用户表单上有一个组合框,应该由vba代码更改,但我不希望任何用户能够键入值,只有vb应具有此功能,用户应该只能选择值。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

根据Chris的建议,添加项目然后设置框的默认值,但是,不要设置锁定,通过在此框中选择组合框选择黄色选项。 enter image description here

答案 1 :(得分:0)

只需将“已锁定”属性更改为True即可。它将阻止用户更改值:但是VBA仍然可以。例如,以下代码锁定ComboBox;然后添加两个值并选择一个:

    MySql.Data.MySqlClient.MySqlConnection connection;
        string server = "localhost";
        string database = "weather";
        string uid = "db1";
        string password = "db12121";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

    try
        {
            connection.Open();
            if (connection.State == ConnectionState.Open)
            {
                MySqlCommand cmd = new MySqlCommand("insert into forecast (country,city,temperature) values(@Country,@City,@Temperature)", connection);
                cmd.Parameters.AddWithValue("@Country", country1);
                cmd.Parameters.AddWithValue("@City", city1);
                cmd.Parameters.AddWithValue("@Temperature", temperature);
                cmd.ExecuteNonQuery();
            }
            else
            {
                DisplayMessage.Text = "Database connection failed.";
            }


        }
        catch (Exception ex)
        {

        }

        connection.Close();
}