当前上下文中不存在组合框 [英] combo box does not exist in current context

查看:105
本文介绍了当前上下文中不存在组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Form1有两个控件按钮和组合框

按钮在设计时添加并在运行时组合框,按钮上的Click事件

刷新组合框来了错误(当前上下文中不存在组合框)

============================ =================

Form1.cs内容

============= ===============================

Form1 has two controls button and combo box
Button is added at design time and combo box at run time,Click event on the Button
refrencing the combo Box comes up with the Error (combo box does not exist in current context)
=============================================
Form1.cs Content
============================================

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

namespace combotest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComboBox();
            InitializeComponent();

        }


        private void button1_Click(object sender, System.EventArgs e)
        {
            string selectedItem = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            MessageBox.Show(selectedItem);
        }
    }
}





============ ===========================

表格设计师内容

==== ===================================

private void InitializeComponent()

{



this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout( );

//

// button1

//

this.button1.Location = new System。 Drawing.Point(95,101);

this.button1.Name =button1;

this.button1.Size = new System.Drawing.Size(75, 23);

this.button1.TabIndex = 0;

this.button1.Text =button1;

this.button1.UseVisualStyleBackColor = true;

//

// Form1

//

this.AutoScaleDimension s = new System.Drawing.SizeF(6F,13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System .Drawing.Size(284,262);

this.Controls.Add(this.button1);

this.Name =Form1;

this.Text =Form1;

this.ResumeLayout(false);



}

private void InitializeComboBox()

{

//this.ComboBox1 = new System.Windows.Forms.ComboBox();



System.Windows.Forms.ComboBox comboBox1 = new System.Windows.Forms.ComboBox();



comboBox1.Location = new System.Drawing .Point(20,60);



comboBox1.Name =comboBox1;



comboBox1。 Size = new System.Drawing.Size(245,25);



comboBox1.BackColor = System.Dr awing.Color.Orange;



comboBox1.ForeColor = System.Drawing.Color.Black;



this.Controls.Add(comboBox1);



comboBox1.DropDownHeight = 70;



comboBox1.DropDownWidth = 350;



comboBox1.Items.Add(Mahesh Chand);



comboBox1.Items .Add(Mike Gold);



comboBox1.Items.Add(Praveen Kumar);



comboBox1.Items.Add(Raj Beniwal);

}



私人按钮1;







}



=======================================
Form Designer Content
=======================================
private void InitializeComponent()
{

this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(95, 101);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
private void InitializeComboBox()
{
//this.ComboBox1 = new System.Windows.Forms.ComboBox();

System.Windows.Forms.ComboBox comboBox1 = new System.Windows.Forms.ComboBox();

comboBox1.Location = new System.Drawing.Point(20, 60);

comboBox1.Name = "comboBox1";

comboBox1.Size = new System.Drawing.Size(245, 25);

comboBox1.BackColor = System.Drawing.Color.Orange;

comboBox1.ForeColor = System.Drawing.Color.Black;

this.Controls.Add(comboBox1);

comboBox1.DropDownHeight = 70;

comboBox1.DropDownWidth = 350;

comboBox1.Items.Add("Mahesh Chand");

comboBox1.Items.Add("Mike Gold");

comboBox1.Items.Add("Praveen Kumar");

comboBox1.Items.Add("Raj Beniwal");
}

private Button button1;



}

推荐答案

对象 combobox1 的范围仅限于 InitializeComboBox 。这就是为什么你不能在处理程序中使用它。



另外,请在之后调用 InitializeComboBox 方法 InitializeComponent 方法。
Object combobox1 has a scope limited to your InitializeComboBox. And that''s why you cannot use it in the handler.

Also, please call InitializeComboBox method after InitializeComponent method.


这篇关于当前上下文中不存在组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆