开发者

Re-adding a control to the controls collection issue

开发者 https://www.devze.com 2023-03-18 18:03 出处:网络
Please help me with the following issue: Scenario: If ComboBox is not null: Controls.Remove(ComboBox), ComboBox.Dispose()

Please help me with the following issue:

Scenario:

  1. If ComboBox is not null: Controls.Remove(ComboBox), ComboBox.Dispose()
  2. Create a new ComboBox (with new DataSource,DisplayMember,etc)
  3. Add the new ComboBox to Controls: Controls.Add(ComboBox)
  4. go to 1.

The issue: When I re-adding my ComboBox (with new properties in place) to the Controls collection, properties (exactly ComboBox.Items collection) of the previous ComboBox are assigned (override) to the new one just added.

Can you provide me any suggestion what is (or could) happening here, please?

Thanks

C# Code:

private void DropDownCell(CellIndex cell, object dropDownData, string displayMember) {

        // Remove previous drop-down, if any
        if( DropDownBox != null ) {
            var garbageDropDownBox = DropDownBox;
            DropDownBox = null;
            Controls.Remove(garbageDropDownBox);
            garbageDropDownBox.Dispose();
        }

        // Create up the dropdown:
        var dropDownBox = new ComboBox() {
            DataSource = dropDownData,
            DisplayMember = displayMember,              
            DropDownStyle = ComboBoxStyle.DropDown,
            MaxDropDownItems = 25,
            AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend,
            Margin = new Padding(0, 0, 0, 0),
            Font = new Font(Family, CellFont.Size, CellFont.Style, CellFont.Unit),
            Location = Point.Round(GetViewPoint(GetLogicalCellRect(cell).Location))
        };
        // Set the 开发者_运维技巧background color
        bool editable = true, editableKnown = true;
        var brush = SheetStyle.GetBackBrush(source, source.GetColumn(cell.Column), cell.Row, ref editableKnown, ref editable) as SolidBrush;
        if( brush != null )
            dropDownBox.BackColor = brush.Color;

        // Tie up events to the drop down
        dropDownBox.SelectionChangeCommitted += new EventHandler(delegate(object sender, EventArgs e) {
            if( sender == DropDownBox )
                try {
                    source.GetColumn(cell.Column).SetDropDownItem(cell.Row, dropDownBox.SelectedItem);
                } catch( Exception ex ) {
                    Logger.Debug(ex);
                }
        });
        dropDownBox.DropDownClosed += new EventHandler(delegate(object sender, EventArgs e) {
            if( sender == DropDownBox )
                try {
                    dropDownBox.Visible = false;
                    Focus();
                    //FocusIndex = cell;
                } catch {
                }
        });
        // Add the control and calculate the optimal width based on contents
        Controls.Add(dropDownBox);
        object currentValue = source.GetColumn(cell.Column).GetRawValue(cell.Row);
        using( Graphics g = dropDownBox.CreateGraphics() ) {
            float width = dropDownBox.Width - SystemInformation.VerticalScrollBarWidth;
            foreach( DataRowView item in dropDownBox.Items ) {
                if( object.Equals(item[displayMember], currentValue) )
                    dropDownBox.SelectedItem = item;
                width = Math.Max(width, g.MeasureString(dropDownBox.GetItemText(item), dropDownBox.Font).Width);
            }
            dropDownBox.Width = (int)width + SystemInformation.VerticalScrollBarWidth;
        }
        // Scroll to drop down and open it
        ScrollToCell(cell);
        dropDownBox.DroppedDown = true;
        dropDownBox.Focus();
        DropDownBox = dropDownBox;
    }


In step 2, be sure to not just change the properties of the ComboBox but to really create a new instance:

Controls.Remove(ComboBox);
ComboBox.Dispose();
ComboBox = new ComboBox(); // <<- !! important
ComboBox.DataSource = ...;
Controls.Add(ComboBox);
0

精彩评论

暂无评论...
验证码 换一张
取 消