private void UpdateSelectedIndex(int newValue) { MgRadioButton radioButton = null; if (SelectedIndex != newValue) { Control.ControlCollection c = (Control.ControlCollection)(Controls); if (c.Count > 0) { //unregister & register the CheckedChanged event of radioButton & unCheck the old selected radioButton. if (SelectedIndex > -1) { radioButton = (MgRadioButton)Controls[SelectedIndex]; radioButton.CheckedChanged -= MgRadioButton_CheckedChanged; radioButton.Checked = false; radioButton.CheckedChanged += MgRadioButton_CheckedChanged; } //unregister & register the CheckedChanged event of radioButton & Check the new selected radioButton. if (newValue > -1) { radioButton = (MgRadioButton)Controls[newValue]; radioButton.CheckedChanged -= MgRadioButton_CheckedChanged; radioButton.Checked = true; radioButton.CheckedChanged += MgRadioButton_CheckedChanged; } } selectedIndex = newValue; OnSelectedIndexChanged(EventArgs.Empty); } }
/// <summary> /// </summary> /// <param name = "container"></param> /// <param name = "layoutEventArgs"></param> /// <returns></returns> public override bool Layout(object container, LayoutEventArgs layoutEventArgs) { Control parent = container as Control; if (parent.Controls.Count == 0) { return(false); } Size clientRect = ((Panel)container).Size; int radioLineSeperator = 0; int radioColSeperator = 0; MgRadioButton radioButton = (MgRadioButton)parent.Controls[0]; Boolean isAppearanceButton = (radioButton.Appearance == Appearance.Button); int radionInColumn = getCountOfButtonsInColumn(parent.Controls.Count, isAppearanceButton); Rectangle radioRect = new Rectangle(0, 0, 0, 0); if (parent.Controls.Count == 1) { radioButton.SetBounds(0, 0, clientRect.Width, clientRect.Height); } else { int i = 0; int width = 0; int height = 0; int numColumns = _numColumns; if (isAppearanceButton) { numColumns = Math.Min(numColumns, parent.Controls.Count); width = radioRect.Width = clientRect.Width / numColumns; height = radioRect.Height = clientRect.Height / radionInColumn; // Fixed defect #:137271: radioColSeperator & radioLineSeperator will be 0 for button radioColSeperator = 0; radioLineSeperator = 0; radioRect.X = radioLineSeperator; radioRect.Y = radioColSeperator; } else { MgRadioPanel mgRadioPanel = container as MgRadioPanel; if (radioButton.Image != null) { // Measure the radio button image size. width = radioButton.Image.Width; height = radioButton.Image.Height; } else { //find the maximun string size in the composite foreach (Control c in parent.Controls) { MgRadioButton mgRadioButton = (MgRadioButton)c; Size textExt = Utils.GetTextExt(mgRadioPanel.Font, mgRadioButton.TextToDisplay, c); width = Math.Max(width, textExt.Width); height = Math.Max(height, textExt.Height); } } // add extras width = (int)((float)(RADIO_RECT_DX + RADIO_TEXT_SPACE + width + 1) * Utils.GetDpiScaleRatioX(parent)); height = (int)((float)(Math.Max(RADIO_RECT_DY, height + 2) * Utils.GetDpiScaleRatioY(parent))); PointF fm = Utils.GetFontMetricsByFont(mgRadioPanel, (mgRadioPanel).Font); radioLineSeperator = (int)fm.Y / 2; height = Math.Max(height, (clientRect.Height - (radionInColumn * radioLineSeperator)) / radionInColumn); radioColSeperator = (int)Math.Max((clientRect.Width - (numColumns * width)) / (numColumns + 1), fm.X / 2); // Use DisplayRectangle so that parent.Padding is honored. #if !PocketPC Rectangle parentDisplayRectangle = parent.DisplayRectangle; #else Rectangle parentDisplayRectangle = parent.Bounds; parentDisplayRectangle.X = parentDisplayRectangle.Y = 0; #endif Point nextControlLocation = parentDisplayRectangle.Location; // calculate the bounds of the first button in the first column radioRect.Y = radioLineSeperator; radioRect.Height = height; // TODO: Hebrew if (mgRadioPanel.RightToLeft == RightToLeft.Yes) { radioRect.X = mgRadioPanel.Width - radioColSeperator - width; } else { radioRect.X = radioColSeperator; } radioRect.Width = width; } foreach (Control c in parent.Controls) { radioButton = (MgRadioButton)c; { // checks if skip to another column is needed if (numColumns > 1 && i > 0 && (i % radionInColumn) == 0) { // TODO: Hebrew MgRadioPanel mgRadioPanel = container as MgRadioPanel; if (mgRadioPanel.RightToLeft == RightToLeft.Yes) { radioRect.X -= width + radioColSeperator; } else { radioRect.X += width + radioColSeperator; } radioRect.Y = radioLineSeperator; } } if (radioRect.X + radioRect.Width > clientRect.Width) { radioRect.Width = clientRect.Width - radioRect.X; } if (radioRect.Y + radioRect.Height > clientRect.Height) { radioRect.Height = clientRect.Height - radioRect.Y; } // set the radio bounds radioButton.SetBounds(radioRect.X, radioRect.Y, radioRect.Width, radioRect.Height); // sets the next radio location radioRect.Y += radioLineSeperator + height; i++; } } return(false); }