Error in using dynaming ComboBoxes

Hi,

can anyone help me regarding the following error which is occuring after creating comboboxes dynamically. its happening in WinForms and the combobox is frequently being converted to editable and non editable based on user options.

when we are creating dynamic combobox after deleting one of the combobox we select any of the other combo and make it editable gives error. (Its coming when we change ComboBoxStyle property)

Here is the error which is coming (and surprisingly its not coming in the code but coming in void Main() section)

System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at SThe program '[2084] Ctry2.exe' has exited with code 0 (0x0).
ystem.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at Ctry2.Form1.Main() in d:\try\ctry2\form1.cs:line 233



Answer this question

Error in using dynaming ComboBoxes

  • TwilightsZone

    I am Attaching the code........




    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;

    // Explicitily added Namespaces
    using System.Xml.XPath;
    using System.Data;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Windows.Forms.Design;


    namespace  Ctry2
    {

    /// <summary>
    /// Summary description for ExpressionBuilder.
    /// </summary>
    public class frmExpressionBuilder : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label lblText;
    private System.Windows.Forms.Panel pnlExpressionBuilder;
    private System.Windows.Forms.Button btnDelete;
    private System.Windows.Forms.Button btnAdd;
    private System.Windows.Forms.ComboBox cmbArithmeticOper;
    private System.Windows.Forms.ComboBox cmbCloseParenthesis;
    private System.Windows.Forms.ComboBox cmbValue;
    private System.Windows.Forms.ComboBox cmbOpenParenthesis;
    private System.Windows.Forms.Panel pnlControls;



    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    //private ComboBox cmbVariablesTemp;
    private ComboBox cmbArithmeticOperTemp;


    /// <summary>
    /// WindowsFormsEditorService.
    /// </summary>
    public IWindowsFormsEditorService iWfes = null;





    private int intMaxLines = 0;

    private int intControlsIndex = 0;

    //string strVariable;
    //string strVariableDataType;
    //string strVariableScope;
    static void Main()

    {
    try
    {
    Application.Run(new frmExpressionBuilder());
    }
    catch(Exception ex)
    {
    MessageBox.Show (ex.ToString());
    }
    }
    /// <summary>
    /// Parameterized Constructor for frmExpressionBuilder.
    /// </summary>
    /// <param name="componentPool">Component Pool</param>
    /// <param name="strLanguage">Localization Language</param>
    /// <param name="variable">variable</param>
    /// <param name="variableDataType">DataType of variable </param>
    /// <param name="variableScope">Scope of variable </param>
    public frmExpressionBuilder()
    {
    try
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();






    }
    catch(Exception ex)
    {
    MessageBox.Show (ex.Message,this.Text);
    }


    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing"></param>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }


    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.lblText = new System.Windows.Forms.Label();
    this.pnlExpressionBuilder = new System.Windows.Forms.Panel();
    this.pnlControls = new System.Windows.Forms.Panel();
    this.btnDelete = new System.Windows.Forms.Button();
    this.btnAdd = new System.Windows.Forms.Button();
    this.cmbArithmeticOper = new System.Windows.Forms.ComboBox();
    this.cmbCloseParenthesis = new System.Windows.Forms.ComboBox();
    this.cmbValue = new System.Windows.Forms.ComboBox();
    this.cmbOpenParenthesis = new System.Windows.Forms.ComboBox();
    this.pnlExpressionBuilder.SuspendLayout();
    this.pnlControls.SuspendLayout();
    this.SuspendLayout();
    // 
    // lblText
    // 
    this.lblText.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.lblText.Location = new System.Drawing.Point(16, 8);
    this.lblText.Name = "lblText";
    this.lblText.Size = new System.Drawing.Size(192, 24);
    this.lblText.TabIndex = 0;
    this.lblText.Text = "_Variable MyDate";
    // 
    // pnlExpressionBuilder
    // 
    this.pnlExpressionBuilder.AutoScroll = true;
    this.pnlExpressionBuilder.BackColor = System.Drawing.Color.Transparent;
    this.pnlExpressionBuilder.Controls.Add(this.pnlControls);
    this.pnlExpressionBuilder.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.pnlExpressionBuilder.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.pnlExpressionBuilder.Location = new System.Drawing.Point(9, 35);
    this.pnlExpressionBuilder.Name = "pnlExpressionBuilder";
    this.pnlExpressionBuilder.Size = new System.Drawing.Size(605, 40);
    this.pnlExpressionBuilder.TabIndex = 0;
    // 
    // pnlControls
    // 
    this.pnlControls.Controls.Add(this.btnDelete);
    this.pnlControls.Controls.Add(this.btnAdd);
    this.pnlControls.Controls.Add(this.cmbArithmeticOper);
    this.pnlControls.Controls.Add(this.cmbCloseParenthesis);
    this.pnlControls.Controls.Add(this.cmbValue);
    this.pnlControls.Controls.Add(this.cmbOpenParenthesis);
    this.pnlControls.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.pnlControls.Location = new System.Drawing.Point(8, 9);
    this.pnlControls.Name = "pnlControls";
    this.pnlControls.Size = new System.Drawing.Size(585, 24);
    this.pnlControls.TabIndex = 0;
    // 
    // btnDelete
    // 
    this.btnDelete.BackColor = System.Drawing.Color.LightGray;
    this.btnDelete.Enabled = false;
    this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.btnDelete.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.btnDelete.Location = new System.Drawing.Point(507, 0);
    this.btnDelete.Name = "btnDelete";
    this.btnDelete.TabIndex = 5;
    this.btnDelete.Text = "_Delete";
    this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
    // 
    // btnAdd
    // 
    this.btnAdd.BackColor = System.Drawing.Color.LightGray;
    this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.btnAdd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.btnAdd.Location = new System.Drawing.Point(403, 0);
    this.btnAdd.Name = "btnAdd";
    this.btnAdd.Size = new System.Drawing.Size(96, 23);
    this.btnAdd.TabIndex = 4;
    this.btnAdd.Text = "_Add>>";
    this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
    // 
    // cmbArithmeticOper
    // 
    this.cmbArithmeticOper.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cmbArithmeticOper.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.cmbArithmeticOper.ItemHeight = 13;
    this.cmbArithmeticOper.Location = new System.Drawing.Point(403, 0);
    this.cmbArithmeticOper.Name = "cmbArithmeticOper";
    this.cmbArithmeticOper.Size = new System.Drawing.Size(72, 21);
    this.cmbArithmeticOper.TabIndex = 3;
    this.cmbArithmeticOper.Visible = false;
    // 
    // cmbCloseParenthesis
    // 
    this.cmbCloseParenthesis.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cmbCloseParenthesis.Enabled = false;
    this.cmbCloseParenthesis.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.cmbCloseParenthesis.ItemHeight = 13;
    this.cmbCloseParenthesis.Location = new System.Drawing.Point(336, 0);
    this.cmbCloseParenthesis.Name = "cmbCloseParenthesis";
    this.cmbCloseParenthesis.Size = new System.Drawing.Size(56, 21);
    this.cmbCloseParenthesis.TabIndex = 2;
    // 
    // cmbValue
    // 
    this.cmbValue.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cmbValue.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.cmbValue.ItemHeight = 13;
    this.cmbValue.Items.AddRange(new object[] {
      "Other",
      "Date",
      "Amount"});
    this.cmbValue.Location = new System.Drawing.Point(64, 0);
    this.cmbValue.Name = "cmbValue";
    this.cmbValue.Size = new System.Drawing.Size(264, 21);
    this.cmbValue.TabIndex = 1;
    this.cmbValue.SelectedIndexChanged += new System.EventHandler(this.cmbValue_SelectedIndexChanged);
    // 
    // cmbOpenParenthesis
    // 
    this.cmbOpenParenthesis.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cmbOpenParenthesis.Enabled = false;
    this.cmbOpenParenthesis.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.cmbOpenParenthesis.ItemHeight = 13;
    this.cmbOpenParenthesis.Location = new System.Drawing.Point(2, 0);
    this.cmbOpenParenthesis.Name = "cmbOpenParenthesis";
    this.cmbOpenParenthesis.Size = new System.Drawing.Size(56, 21);
    this.cmbOpenParenthesis.TabIndex = 0;
    // 
    // frmExpressionBuilder
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.BackColor = System.Drawing.Color.WhiteSmoke;
    this.ClientSize = new System.Drawing.Size(626, 125);
    this.Controls.Add(this.pnlExpressionBuilder);
    this.Controls.Add(this.lblText);
    this.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.Name = "frmExpressionBuilder";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
    this.Text = "_Expression Builder";
    this.Load += new System.EventHandler(this.ExpressionBuilder_Load);
    this.Closed += new System.EventHandler(this.frmExpressionBuilder_Closed);
    this.pnlExpressionBuilder.ResumeLayout(false);
    this.pnlControls.ResumeLayout(false);
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// Load the Form with all the controls.
    /// </summary>
    /// <param name="sender"> </param>
    /// <param name="e"></param>
    private void ExpressionBuilder_Load(object sender, System.EventArgs e)
    {

    }




    /// <summary>
    /// To add new line.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnAdd_Click(object sender, System.EventArgs e)
    {



    try
    {
    AddNewSetofControls();
    }
    catch(Exception ex)
    {
    MessageBox.Show (ex.Message,this.Text);
    }

    }
    /// <summary>
    /// To add set of controls in new panel.
    /// </summary>
    private void AddNewSetofControls()
    {
    AddNewSetofControls(false,0,"","",0);
    }

    /// <summary>
    /// To add set of controls in new panel with specified value.
    /// </summary>
    /// <param name="setData">Data to be set in the Controls.</param>
    /// <param name="openBrace">Open Brace.</param>
    /// <param name="closeBrace">Close Brace.</param>
    /// <param name="varValue"> Value for Variable.</param>
    /// <param name="arithOper"></param>
    private void AddNewSetofControls(bool setData,int openBrace,string varValue,string arithOper,int closeBrace)
    {
    int intTop;
    int intLeft;
    int intWidth;
    int intHeight;
    object[] objArray;

    intControlsIndex++;
    cmbArithmeticOper.Visible = true;
    btnAdd.Visible = false;

    intWidth = pnlControls.Width;
    intHeight = pnlControls.Height;
    intLeft = pnlControls.Left;
    pnlControls = (Panel)pnlExpressionBuilder.Controls[pnlExpressionBuilder.Controls.Count-1]; 
    intTop = pnlControls.Top + pnlControls.Height + 8;
    pnlControls = new  Panel();
    pnlControls.Name = "pnlControls" + intControlsIndex.ToString().PadLeft(2,'0');
    pnlControls.Size = new System.Drawing.Size(intWidth , intHeight);
    pnlControls.Location = new System.Drawing.Point(intLeft , intTop);
    pnlExpressionBuilder.Controls.Add(pnlControls);

    intWidth = cmbOpenParenthesis.Width;
    intHeight = cmbOpenParenthesis.Height;
    intLeft = cmbOpenParenthesis.Left;
    ComboBox cmbOpenParenthesisTemp = new ComboBox();
    cmbOpenParenthesisTemp.Name = "cmbOpenParenthesis" + intControlsIndex.ToString().PadLeft(2,'0');
    cmbOpenParenthesisTemp.Size = new System.Drawing.Size(intWidth , intHeight);
    cmbOpenParenthesisTemp.Location = new System.Drawing.Point(intLeft , 0);
    cmbOpenParenthesisTemp.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    cmbOpenParenthesisTemp.Enabled = cmbOpenParenthesis.Enabled;
    objArray = new object [cmbOpenParenthesis.Items.Count];
    cmbOpenParenthesis.Items.CopyTo(objArray,0);
    cmbOpenParenthesisTemp.Items.AddRange(objArray);
    //cmbOpenParenthesisTemp.SelectedIndex=0;

    pnlControls.Controls.Add(cmbOpenParenthesisTemp);
    if (setData == true)
    {
    cmbOpenParenthesisTemp.SelectedIndex = openBrace;
    }


    intWidth = cmbValue.Width;
    intLeft = cmbValue.Left;
    ComboBox cmValueTemp = new ComboBox();
    cmValueTemp.Name = "cmbValue" + intControlsIndex.ToString().PadLeft(2,'0');

    cmValueTemp.SelectedIndexChanged += new System.EventHandler(this.cmbValue_SelectedIndexChanged);
    cmValueTemp.Size = new System.Drawing.Size(intWidth , intHeight);
    cmValueTemp.Location = new System.Drawing.Point(intLeft , 0);
    cmValueTemp.DropDownStyle = ComboBoxStyle.DropDownList;
    objArray = new object [cmbValue.Items.Count];
    cmbValue.Items.CopyTo(objArray,0);
    cmValueTemp.Items.AddRange(objArray);

    pnlControls.Controls.Add(cmValueTemp);
    if (setData == true)
    {
    if(cmValueTemp.Items.IndexOf(varValue) == -1)
    cmValueTemp.DropDownStyle = ComboBoxStyle.DropDown;
    cmValueTemp.Text = varValue;
    }
    else
    cmValueTemp.SelectedIndex = 0;

    intWidth = cmbCloseParenthesis.Width;
    intLeft = cmbCloseParenthesis.Left;
    ComboBox cmbCloseParenthesisTemp = new ComboBox();
    cmbCloseParenthesisTemp.Name = "cmbCloseParenthesis" + intControlsIndex.ToString().PadLeft(2,'0');
    cmbCloseParenthesisTemp.Size = new System.Drawing.Size(intWidth , intHeight);
    cmbCloseParenthesisTemp.Location = new System.Drawing.Point(intLeft , 0);
    cmbCloseParenthesisTemp.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    cmbCloseParenthesisTemp.Enabled = cmbCloseParenthesis.Enabled;
    objArray = new object [cmbCloseParenthesis.Items.Count];
    cmbCloseParenthesis.Items.CopyTo(objArray,0);
    cmbCloseParenthesisTemp.Items.AddRange(objArray);
    //cmbCloseParenthesisTemp.SelectedIndex=0;
    pnlControls.Controls.Add(cmbCloseParenthesisTemp);
    if (setData == true)
    {
    cmbCloseParenthesisTemp.SelectedIndex = closeBrace;
    }

    intWidth = cmbArithmeticOper.Width;
    intLeft = cmbArithmeticOper.Left;

    if (cmbArithmeticOperTemp != null)
    cmbArithmeticOperTemp.Visible = true;
    cmbArithmeticOperTemp = new ComboBox();
    cmbArithmeticOperTemp.Name = "cmbArithmeticOper" + intControlsIndex.ToString().PadLeft(2,'0');
    cmbArithmeticOperTemp.Size = new System.Drawing.Size(intWidth , intHeight);
    cmbArithmeticOperTemp.Location = new System.Drawing.Point(intLeft , 0);
    cmbArithmeticOperTemp.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    cmbArithmeticOperTemp.Visible = false;

    ArrayList arlTempArrayList = new ArrayList ();
    ArrayList arlCloneArraylist;
    arlCloneArraylist = (ArrayList) cmbArithmeticOper.DataSource;
    if(arlCloneArraylist != null)
    {
    arlTempArrayList = (ArrayList) arlCloneArraylist.Clone ();
    cmbArithmeticOperTemp.DataSource = arlTempArrayList;//Arithmetic;
    cmbArithmeticOperTemp.DisplayMember = "DisplayName";
    cmbArithmeticOperTemp.ValueMember = "ValueName";
    }
    pnlControls.Controls.Add(cmbArithmeticOperTemp);
    if (setData == true)
    {
    cmbArithmeticOperTemp.Text = arithOper;
    }


    intWidth = btnAdd.Width;
    intHeight = btnAdd.Height;
    intLeft = btnAdd.Left;
    btnAdd = new Button();
    btnAdd.FlatStyle = FlatStyle.System;
    btnAdd.Name = "btnAdd" + intControlsIndex.ToString().PadLeft(2,'0');
    btnAdd.Text = "ADD";
    btnAdd.Size = new System.Drawing.Size(intWidth , intHeight);
    btnAdd.Location = new System.Drawing.Point(intLeft , 0);
    this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
    pnlControls.Controls.Add(btnAdd);
    btnAdd.BringToFront();

    intWidth = btnDelete.Width;
    intHeight = btnDelete.Height;
    intLeft = btnDelete.Left;
    btnDelete = new Button();
    btnDelete.FlatStyle = FlatStyle.System;
    btnDelete.Name = "btnDelete" + intControlsIndex.ToString().PadLeft(2,'0');
    btnDelete.Text = "Delete";
    btnDelete.Size = new System.Drawing.Size(intWidth , intHeight);
    btnDelete.Location = new System.Drawing.Point(intLeft , 0);
    this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
    pnlControls.Controls.Add(btnDelete);
    btnDelete.Tag = intControlsIndex;

    if (pnlExpressionBuilder.Controls.Count -1  < 10)
    {
    intHeight = pnlExpressionBuilder.Height;
    intTop = (pnlExpressionBuilder.Top + pnlExpressionBuilder.Height);
    pnlExpressionBuilder.Height = pnlExpressionBuilder.Height + pnlControls.Height + 8;
    this.Height = this.Height + (pnlExpressionBuilder.Height - intHeight);

    }
    else
    {
    pnlExpressionBuilder.AutoScrollPosition = new Point(0,pnlExpressionBuilder.Height);
    }
    }

    /// <summary>
    /// To remove current line.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnDelete_Click(object sender, System.EventArgs e)
    {
    try
    {
    Button btnFirstButton;
    btnFirstButton = (sender as Button );
    if (btnFirstButton.Parent.Name.Equals("pnlControls"))return;

    DeleteCurrentSetofControls (btnFirstButton);
    //intControlsIndex--;

    if (pnlExpressionBuilder.Controls.Count  < 10)
    {
    int intHeight = pnlExpressionBuilder.Height;
    int intTop =  (pnlExpressionBuilder.Top + pnlExpressionBuilder.Height);
    pnlExpressionBuilder.Height = pnlExpressionBuilder.Height-pnlControls.Height-8 ;
    this.Height = this.Height - (intHeight-pnlExpressionBuilder.Height);

    }
    if (intMaxLines == pnlExpressionBuilder.Controls.Count )
    btnAdd.Enabled = false;
    else
    btnAdd.Enabled = true;
    if (pnlExpressionBuilder.Controls.Count == 1)
    {
    cmbArithmeticOper.Visible = false;
    cmbArithmeticOperTemp.Visible = false;
    pnlExpressionBuilder.Controls[0].Controls[1].Visible = true;
    btnAdd = (pnlExpressionBuilder.Controls[0].Controls[1] as Button );
    btnDelete = (pnlExpressionBuilder.Controls[0].Controls[0] as Button );

    cmbArithmeticOperTemp = null;
    cmbOpenParenthesis.SelectedIndex = 0;
    cmbCloseParenthesis.SelectedIndex = 0;
    cmbOpenParenthesis.Enabled = false;
    cmbCloseParenthesis.Enabled = false;
    btnDelete.Enabled = false;

    }
    else
    {
    cmbArithmeticOperTemp  = (pnlExpressionBuilder.Controls[pnlExpressionBuilder.Controls.Count-1].Controls[4] as ComboBox);
    pnlExpressionBuilder.Controls[pnlExpressionBuilder.Controls.Count-1].Controls[4].Visible = false;
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message,this.Text);
    }
    }


    /// <summary>
    /// To delete set of controls in specified panel.
    /// </summary>
    /// <param name="currentButton">Button to be Deleted</param>
    private void DeleteCurrentSetofControls(Button currentButton)
    {
    long lngTag;
    lngTag = long.Parse (currentButton.Tag.ToString ());
    string strParentPanel;
    strParentPanel = currentButton.Parent.Name;
    this.pnlExpressionBuilder.SuspendLayout();

    for( int intCounter=0; intCounter<this.pnlExpressionBuilder.Controls.Count; intCounter++ )
    {
    if (this.pnlExpressionBuilder.Controls.Count == 1) break;
    if( this.pnlExpressionBuilder.Controls[intCounter] is Panel )
    {
    Panel  pnlControl = (Panel)this.pnlExpressionBuilder.Controls[intCounter];
    if (pnlControl.Name.Equals(strParentPanel))
    {
    if(intCounter == this.pnlExpressionBuilder.Controls.Count-1)
    {
    if (this.pnlExpressionBuilder.Controls[intCounter-1].Controls[0] is Button)
    btnAdd = (Button) this.pnlExpressionBuilder.Controls[intCounter-1].Controls[0];
    else
    btnAdd =(Button) this.pnlExpressionBuilder.Controls[intCounter-1].Controls[3];
    btnAdd.Visible = true;
    btnAdd.BringToFront();

    }
    this.pnlExpressionBuilder.Controls.RemoveAt(intCounter);
    pnlControl.Dispose();
    if(intCounter == this.pnlExpressionBuilder.Controls.Count )
    pnlControls.Top = this.pnlExpressionBuilder.Controls[intCounter-1].Top;

    intCounter--;

    }
    else
    {

    long lngIndex = 0;
    stringstrSubstring = pnlControl.Name.Substring(11).Trim();
    //if(!strSubstring.Equals(""))
    if(strSubstring.Length != 0)
    lngIndex = long.Parse (strSubstring);
    if(lngIndex>lngTag)
    pnlControl.Top = pnlControl.Top - (pnlControl.Height + 8);

    }
    }
    this.pnlExpressionBuilder.ResumeLayout();
    }
    }


    /// <summary>
    /// To Close the form.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void frmExpressionBuilder_Closed(object sender, System.EventArgs e)
    {

    if(iWfes != null)
    iWfes.CloseDropDown ();
    }

    private void cmbValue_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    GetValue((sender as ComboBox ));
    }

    public void GetValue(ComboBox cmbValue)
    {
    if (cmbValue.Text.Equals ("Other"))
    {
    if(cmbValue.Items.IndexOf("") == -1)
    cmbValue.Items.Add ("");
    cmbValue.Text = "";
    cmbValue.DropDownStyle = ComboBoxStyle.DropDown;

    }
    else
    cmbValue.DropDownStyle = ComboBoxStyle.DropDownList;
    }
    }
    }

  • zepgrirl

    Please genarate a sample that demonstrates this issue and then file a bug via http://lab.msdn.microsoft.com/productfeedback/default.aspx.

    Thanks

  • Ed209

    Thanks for posting your example (there's a space missing on line 573 I think... easy fix)

    I am able to create the bug by doing the following things:
    1. Click ADD 3 times
    2. Click the TOP MOST Active Delete Delete button
    3. Change the middle drop down and select "Other"

    We are having a similar error (very hard to reproduce), and having ANYTHING recreate a similar stack trace might help us with out problem. If it does I'll report back, but anyone else working on this, the above sample code and my steps recreate it everytime on our system.

    M@

  • ntintel

    Here's a block of code that reproduces the problem every time.

    What is interesting - if you REMOVE the left most combo box from the panel, the problem doesn't happen.

    And if you look in the code, where I do the dispose I have a comment that if you skip the dispose the problem doesn't happen. "WinformsUser" above, who posted the original code, this will solve your problem. Instead of disposing the panel, REMOVE IT and you're all set.

    We haven't found the pattern in our application that causes this, but I think I'm on the right road.

    Code to reproduce every time:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;

    namespace AutoCompleteSandBox
    {
    /// <summary>
    /// Summary description for Form2.
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;


    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.ComboBox comboBox2;
    private System.Windows.Forms.Button button1;

    Panel lastPanel;
    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    int panelCount;

    static void Main()
    {
    try
    {
    Application.Run(new Form2());
    }
    catch(Exception ex)
    {
    MessageBox.Show (ex.ToString());
    }
    }

    public Form2()
    {
    InitializeComponent();
    lastPanel = panel1;

    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.panel1 = new System.Windows.Forms.Panel();
    this.comboBox1 = new System.Windows.Forms.ComboBox();
    this.button1 = new System.Windows.Forms.Button();
    this.comboBox2 = new System.Windows.Forms.ComboBox();
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.panel1.SuspendLayout();
    this.SuspendLayout();
    //
    // panel1
    //
    this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.panel1.Controls.Add(this.comboBox1);
    this.panel1.Controls.Add(this.button1);
    this.panel1.Controls.Add(this.comboBox2);
    this.panel1.Location = new System.Drawing.Point(8, 8);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(232, 40);
    this.panel1.TabIndex = 0;
    //
    // comboBox1
    //
    this.comboBox1.Location = new System.Drawing.Point(8, 8);
    this.comboBox1.Name = "comboBox1";
    this.comboBox1.Size = new System.Drawing.Size(40, 21);
    this.comboBox1.TabIndex = 4;
    this.comboBox1.Text = "comboBox1";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(176, 8);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(48, 23);
    this.button1.TabIndex = 3;
    this.button1.Text = "New";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // comboBox2
    //
    this.comboBox2.Items.AddRange(new object[] {
    "DropDown",
    "DropDownList"});
    this.comboBox2.Location = new System.Drawing.Point(56, 8);
    this.comboBox2.Name = "comboBox2";
    this.comboBox2.Size = new System.Drawing.Size(112, 21);
    this.comboBox2.TabIndex = 1;
    this.comboBox2.Text = "comboBox2";
    this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(256, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(152, 23);
    this.label1.TabIndex = 1;
    this.label1.Text = "1. Click \"New\" 4 times";
    //
    // label2
    //
    this.label2.Location = new System.Drawing.Point(256, 24);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(152, 23);
    this.label2.TabIndex = 2;
    this.label2.Text = "1. \"DEL\" all but the last set";
    //
    // label3
    //
    this.label3.Location = new System.Drawing.Point(256, 40);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(168, 32);
    this.label3.TabIndex = 3;
    this.label3.Text = "3. Change the last combo box to \"DropDownList\"";
    //
    // Form2
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(424, 173);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.panel1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.panel1.ResumeLayout(false);
    this.ResumeLayout(false);

    }
    #endregion

    private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    ComboBox box = sender as ComboBox;
    if (box.Text == "DropDownList")
    box.DropDownStyle = ComboBoxStyle.DropDownList;
    else
    box.DropDownStyle = ComboBoxStyle.DropDown;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    Panel newPanel = new Panel();
    newPanel.Size = panel1.Size;
    newPanel.Top = lastPanel.Bottom;
    newPanel.Name = panelCount.ToString();
    newPanel.Left = lastPanel.Left;
    newPanel.BorderStyle = lastPanel.BorderStyle;

    panelCount++;

    foreach (Control control in panel1.Controls)
    {
    if (control is ComboBox)
    {
    ComboBox comboBox = new ComboBox();
    comboBox.Items.Add("DropDown");
    comboBox.Items.Add("DropDownList");

    comboBox.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
    comboBox.Location = control.Location;
    comboBox.Size = control.Size;
    newPanel.Controls.Add(comboBox);
    }
    if (control is Button)
    {
    Button button = new Button();
    button.Text = "Del";
    button.Size = control.Size;
    button.Location = control.Location;
    newPanel.Controls.Add(button);
    button.Click += new EventHandler(deletePanel);

    }
    }
    Height = newPanel.Bottom + newPanel.Height;

    Controls.Add( newPanel);
    lastPanel = newPanel;
    }

    private void deletePanel(object sender, System.EventArgs e)
    {
    Button button = sender as Button;

    string parentName = button.Parent.Name;
    Control deleteMe = panel1;

    foreach (Control control in Controls)
    {
    if (control.Name == parentName)
    deleteMe = control;
    }

    Controls.Remove(deleteMe);
    //If I remove the following Dispose the problem does not happen.
    deleteMe.Dispose();

    }

    }
    }


  • Error in using dynaming ComboBoxes