⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagridviewradiobuttoncell.cs

📁 vs.net2005 中DataGridView中加入RadioButton的源码,把多个RadioButton加入DataGridView的每一行中.
💻 CS
📖 第 1 页 / 共 5 页
字号:
            // Recompute the layout of the cell.
            ComputeLayout(graphics,
                          cellBounds,
                          cellBounds,
                          rowIndex,
                          DataGridViewElementStates.None,
                          null /*formattedValue*/,
                          null /*errorText*/,
                          cellStyle,
                          dataGridViewAdvancedBorderStyleEffective,
                          DataGridViewPaintParts.None,
                          false /*paint*/);

            // Deduce the cell part beneath the mouse pointer.
            Point mousePosition = this.DataGridView.PointToClient(Control.MousePosition);
            Rectangle rect;
            if (this.layout.ScrollingNeeded)
            {
                // Is the mouse over the bottom scroll button?
                rect = new Rectangle(this.layout.DownButtonLocation, this.layout.ScrollButtonsSize);
                if (rect.Contains(mousePosition))
                {
                    return DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationBottomScrollButton;
                }
                // Is the mouse over the upper scroll button?
                rect = new Rectangle(this.layout.UpButtonLocation, this.layout.ScrollButtonsSize);
                if (rect.Contains(mousePosition))
                {
                    return DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationTopScrollButton;
                }
            }
            if (this.layout.DisplayedItemsCount > 0)
            {
                Point radioButtonLocation = this.layout.FirstDisplayedItemLocation;
                int textHeight = cellStyle.Font.Height;
                int itemIndex = this.layout.FirstDisplayedItemIndex;
                Rectangle radioButtonBounds = new Rectangle(radioButtonLocation, this.layout.RadioButtonsSize);
                while (itemIndex < this.Items.Count &&
                       itemIndex < this.layout.FirstDisplayedItemIndex + this.maxDisplayedItems &&
                       itemIndex - this.layout.FirstDisplayedItemIndex < this.layout.DisplayedItemsCount)
                {
                    if (radioButtonBounds.Contains(mousePosition))
                    {
                        // The mouse is over a radio button
                        return itemIndex - this.layout.FirstDisplayedItemIndex;
                    }
                    itemIndex++;
                    radioButtonBounds.Y += textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin;
                }
            }
            return DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationGeneric;
        }

        /// <summary>
        /// Returns a ScrollBarArrowButtonState state given the current mouse location.
        /// </summary>
        private ScrollBarArrowButtonState GetScrollBarArrowButtonState(bool upButton, int mouseLocationCode, bool enabled)
        {
            if (!enabled)
            {
                if (upButton)
                {
                    return ScrollBarArrowButtonState.UpDisabled;
                }
                else
                {
                    return ScrollBarArrowButtonState.DownDisabled;
                }
            }
            if (mouseLocationCode == DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationTopScrollButton)
            {
                // Mouse is over upper button
                if (Control.MouseButtons == MouseButtons.Left)
                {
                    if (upButton)
                    {
                        return ScrollBarArrowButtonState.UpPressed;
                    }
                    else
                    {
                        return ScrollBarArrowButtonState.DownNormal;
                    }
                }
                else
                {
                    if (upButton)
                    {
                        return ScrollBarArrowButtonState.UpHot;
                    }
                    else
                    {
                        return ScrollBarArrowButtonState.DownNormal;
                    }
                }
            }
            else if (mouseLocationCode == DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationBottomScrollButton)
            {
                // Mouse is over bottom button
                if (Control.MouseButtons == MouseButtons.Left)
                {
                    if (upButton)
                    {
                        return ScrollBarArrowButtonState.UpNormal;
                    }
                    else
                    {
                        return ScrollBarArrowButtonState.DownPressed;
                    }
                }
                else
                {
                    if (upButton)
                    {
                        return ScrollBarArrowButtonState.UpNormal;
                    }
                    else
                    {
                        return ScrollBarArrowButtonState.DownHot;
                    }
                }
            }

            else if (upButton)
            {
                return ScrollBarArrowButtonState.UpNormal;
            }
            else
            {
                return ScrollBarArrowButtonState.DownNormal;
            }
        }

        /// <summary>
        /// Custom implementation of the GetPreferredSize method.
        /// </summary>
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (this.DataGridView == null)
            {
                return new Size(-1, -1);
            }
            DataGridViewRadioButtonFreeDimension freeDimension = DataGridViewRadioButtonCell.GetFreeDimensionFromConstraint(constraintSize);
            Rectangle borderWidthsRect = this.StandardBorderWidths;
            int borderAndPaddingWidths = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int borderAndPaddingHeights = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            int preferredHeight = 0, preferredWidth = 0;
            // Assuming here that all radio button states use the same size.
            Size radioButtonGlyphSize = RadioButtonRenderer.GetGlyphSize(graphics, RadioButtonState.CheckedNormal);

            if (freeDimension != DataGridViewRadioButtonFreeDimension.Width)
            {
                preferredHeight = Math.Min(this.Items.Count, this.MaxDisplayedItems) * (Math.Max(cellStyle.Font.Height, radioButtonGlyphSize.Height) + DATAGRIDVIEWRADIOBUTTONCELL_margin) + DATAGRIDVIEWRADIOBUTTONCELL_margin;
                preferredHeight += 2 * DATAGRIDVIEWRADIOBUTTONCELL_margin + borderAndPaddingHeights;
            }
           
            if (freeDimension != DataGridViewRadioButtonFreeDimension.Height)
            {
                TextFormatFlags flags = TextFormatFlags.Top | TextFormatFlags.Left | TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.NoPrefix;

                if (this.Items.Count > 0)
                {
                    // Figure out the width of the longest entry
                    int maxPreferredItemWidth = -1, preferredItemWidth;
                    foreach (object item in this.Items)
                    {
                        string formattedValue = GetFormattedValue(GetItemValue(item), rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize) as string;
                        if (formattedValue != null)
                        {
                            preferredItemWidth = DataGridViewCell.MeasureTextSize(graphics, formattedValue, cellStyle.Font, flags).Width;
                        }
                        else
                        {
                            preferredItemWidth = DataGridViewCell.MeasureTextSize(graphics, " ", cellStyle.Font, flags).Width;
                        }
                        if (preferredItemWidth > maxPreferredItemWidth)
                        {
                            maxPreferredItemWidth = preferredItemWidth;
                        }
                    }
                    preferredWidth = maxPreferredItemWidth;
                }

                if (freeDimension == DataGridViewRadioButtonFreeDimension.Width)
                {
                    Size contentSize = new Size(Int32.MaxValue, constraintSize.Height - borderAndPaddingHeights);
                    if (GetScrollingNeeded(graphics, rowIndex, cellStyle, contentSize))
                    {
                        // Accommodate the scrolling buttons
                        preferredWidth += ScrollBarRenderer.GetSizeBoxSize(graphics, ScrollBarState.Normal).Width;
                    }
                }

                preferredWidth += radioButtonGlyphSize.Width + 5 * DATAGRIDVIEWRADIOBUTTONCELL_margin + borderAndPaddingWidths;
            }

            if (this.DataGridView.ShowCellErrors)
            {
                // Making sure that there is enough room for the potential error icon
                if (freeDimension != DataGridViewRadioButtonFreeDimension.Height)
                {
                    preferredWidth = Math.Max(preferredWidth,
                                              borderAndPaddingWidths + DATAGRIDVIEWRADIOBUTTONCELL_iconMarginWidth * 2 + DATAGRIDVIEWRADIOBUTTONCELL_iconsWidth);
                }
                if (freeDimension != DataGridViewRadioButtonFreeDimension.Width)
                {
                    preferredHeight = Math.Max(preferredHeight,
                                               borderAndPaddingHeights + DATAGRIDVIEWRADIOBUTTONCELL_iconMarginHeight * 2 + DATAGRIDVIEWRADIOBUTTONCELL_iconsHeight);
                }
            }

            return new Size(preferredWidth, preferredHeight);
        }

        /// <summary>
        /// Helper function that determines if scrolling buttons should be displayed
        /// </summary>
        private bool GetScrollingNeeded(Graphics graphics, int rowIndex, DataGridViewCellStyle cellStyle, Size contentSize)
        {
            if (this.Items.Count <= 1)
            {
                return false;
            }

            if (this.MaxDisplayedItems >= this.Items.Count && 
                this.Items.Count * (cellStyle.Font.Height + DATAGRIDVIEWRADIOBUTTONCELL_margin) + DATAGRIDVIEWRADIOBUTTONCELL_margin <= contentSize.Height /*- borderAndPaddingHeights*/)
            {
                // There is enough vertical room to display all the radio buttons
                return false;
            }

            // Is there enough room to display the scroll buttons?
            Size sizeBoxSize = ScrollBarRenderer.GetSizeBoxSize(graphics, ScrollBarState.Normal);
            if (2 * DATAGRIDVIEWRADIOBUTTONCELL_margin + sizeBoxSize.Width > contentSize.Width ||
                2 * sizeBoxSize.Height > contentSize.Height)
            {
                // There isn't enough room to show the scroll buttons.
                return false;
            }

            // Scroll buttons are required and there is enough room for them.
            return true;
        }

        /// <summary>
        /// Helper function that sets the displayMemberProperty member based on the DataSource and the provided displayMember field name
        /// </summary>
        private void InitializeDisplayMemberPropertyDescriptor(string displayMember)
        {
            if (this.DataManager != null)
            {
                if (String.IsNullOrEmpty(displayMember))
                {
                    this.displayMemberProperty = null;
                }
                else
                {
                    BindingMemberInfo displayBindingMember = new BindingMemberInfo(displayMember);
                    // make the DataManager point to the sublist inside this.DataSource
                    this.DataManager = this.DataGridView.BindingContext[this.DataSource, displayBindingMember.BindingPath] as CurrencyManager;

                    PropertyDescriptorCollection props = this.DataManager.GetItemProperties();
                    PropertyDescriptor displayMemberProperty = props.Find(displayBindingMember.BindingField, true);
                    if (displayMemberProperty == null)
                    {
                        throw new ArgumentException("Field called '" + displayMember + "' does not exist.");
                    }
                    else
                    {
                        this.displayMemberProperty = displayMemberProperty;
                    }
                }
            }
        }

        /// <summary>
        /// Helper function that sets the valueMemberProperty member based on the DataSource and the provided valueMember field name
        /// </summary>
        private void InitializeValueMemberPropertyDescriptor(string valueMember)
        {
            if (this.DataManager != null)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -