I know ComboBox.Height cannot be set easily. Can be changed with the Font. But I need to know it's final height. It doesn't update before the window and controls are displayed.
How can I calculate it ? When I run this the button is not below but behind the combo box:
// my forms must be disigned by code only (no designer is used)
public class Form1: Form
{
public Form1()
{
ComboBox box = new ComboBox();
box.Font = new Font("Comic Sans MS", 100, FontStyle.Regular);
Controls.Add(box);
Button button = new Button();
button.Text = "hello world";
button.SetBounds(box.Left, box.Bottom, 256, 32);
button.SetBounds(box.Left, box.Height, 256, 32); // doesn't work either
Controls.Add(button);
}
}
The problem is that the ComboBox.Bottom property will not be updated to compensate for the font size until the ComboBox has been drawn.
The solution is to dynamically add your controls in the Form.Load event instead of the constructor:
private void MainForm_Load(object sender, EventArgs e)
{
ComboBox box = new ComboBox();
box.Font = new Font("Comic Sans MS", 100, FontStyle.Regular);
Controls.Add(box);
Button button = new Button();
button.Text = "hello world";
button.SetBounds(box.Left, box.Bottom, 256, 32);
Controls.Add(button);
}
Related
I'm creating a ComboBox control using ToolStripControlHost and ToolStripDropDown that can host any kind of control in the DropDown window. For example, the DropDown window might display a listview or treeview or even another usercontrol.
I'm posting a simplified code below where dropdown host a usercontrol with a listview and a button like this:
The problem occurs when the control is positioned at the bottom of the screen in such a way that the dropdown window will extrapolate the lower boundary of the screen. When this occurs, the dropdown ends up hiding the control.
In this case, I'd like to fix the _dropDown.Show method call to show dropdown window as follows:
To repeat the problem, just run the code below and drag the window to the bottom of the screen and open the dropdown.
using System;
using System.Windows.Forms;
public class CustomComboBox : UserControl
{
ToolStripDropDown _dropDown;
public CustomComboBox()
{
var textbox = new TextBox();
textbox.Location = new System.Drawing.Point(0, 0);
textbox.Size = new System.Drawing.Size(this.Width - 22, 20);
textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
this.Controls.Add(textbox);
var button = new Button();
button.Location = new System.Drawing.Point(this.Width - 22, -1);
button.Size = new System.Drawing.Size(22, 22);
button.Text = "\u2BC6";
button.Anchor = AnchorStyles.Right | AnchorStyles.Top;
button.Click += new System.EventHandler(this.Button_Click);
this.Controls.Add(button);
var dropDownControl = new DropDownControlTest();
var controlHost = new ToolStripControlHost(dropDownControl);
_dropDown = new ToolStripDropDown();
_dropDown.AutoSize = true;
_dropDown.Items.Add(controlHost);
}
void Button_Click(object sender, EventArgs e)
{
_dropDown.Show(this, 0, this.Height);
}
}
public class DropDownControlTest : UserControl
{
public DropDownControlTest()
{
var listview = new ListView();
listview.Location = new System.Drawing.Point(3, 1);
listview.Size = new System.Drawing.Size(400,300);
listview.View = View.Details;
listview.Columns.Add("Col 1",100);
listview.Columns.Add("Col 2",100);
this.Controls.Add(listview);
var button = new Button();
button.Location = new System.Drawing.Point(3, 305);
button.Text = "More...";
this.Controls.Add(button);
}
}
public class Form1 : Form
{
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1 ()
{
CustomComboBox ccBox = new CustomComboBox();
ccBox.Location = new System.Drawing.Point(10, 10);
ccBox.Height = 20;
this.Text = "Test CustomComboBox";
this.Controls.Add(ccBox);
}
}
You can use the ToolStripDropDown.Show Method (Control, Point, ToolStripDropDownDirection) overload to control the drop direction. The code will need to perform bounds checking to decide whether to place the dropdown above or below the textbox.
The following is a simplistic method for doing the bounds checking and was only tested on a single screen configuration.
First, make textbox a class level variable.
private TextBox textbox;
public CustomComboBox()
{
//var textbox = new TextBox();
textbox = new TextBox();
The display logic is as follows.
void Button_Click(object sender, EventArgs e)
{
Point textBoxScreenLocation = textbox.PointToScreen(textbox.Location);
// try to position _dropDown below textbox
Point pt = textBoxScreenLocation;
pt.Offset(0, textbox.Height);
// determine if it will fit on the screen below the textbox
Size dropdownSize = _dropDown.GetPreferredSize(Size.Empty);
Rectangle dropdownBounds = new Rectangle(pt, dropdownSize);
if (dropdownBounds.Bottom <= Screen.GetWorkingArea(dropdownBounds).Bottom)
{ // show below
_dropDown.Show(pt, ToolStripDropDownDirection.BelowRight);
}
else
{ // show above
_dropDown.Show(textBoxScreenLocation, ToolStripDropDownDirection.AboveRight);
}
}
}
I can not comment that is why I am answering your question. You can use the reflection and then re-position you control. I have found a custom combobox control same as you developed. Please check this. At least, you will get some idea what you need to do.
I have a Windows Form containing a DataGridView which binds to a DataSet dynamically. In the form on the Button.Click event I am changing the DataGridView.Font appearance.
I tried the below code but it's not affecting the DataGridView. Only the header part is being changed.
Please recommend what I've done wrong in coding.
My Code
private void Btn_Language_Click(object sender, EventArgs e)
{
if(DGV_View.Font.Name == "Trebuchet MS")
{
DGV_View.Font = new System.Drawing.Font("NILKANTH", 12);
this.DGV_View.DefaultCellStyle.Font = new System.Drawing.Font("NILKANTH", 12);
}
else if(DGV_View.Font.Name == "NILKANTH")
{
DGV_View.Font = new System.Drawing.Font("Trebuchet MS", 11);
}
}
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new Font(dataGridView.Font, FontStyle.Bold);
dataGridView.Rows[0].DefaultCellStyle = style;
Just saw it on the other site.
I am learning C#, and as a part of it I wrote small app that consists of form with two groups of buttons, two buttons each. Button for closing of app is added as well. And everything works ok. Also there is MessageBox, showing that the app is going to be closed. And here is little problem that bugs me: OK button within that MessageBox is NOT centered horizontally. I guess there is a method to align that button, but what puzzles me is why it's is not centered by default? As illustration here are screenshots:
Here is code as well:
using System;
using System.Drawing;
using System.Windows.Forms;
public class myForm : Form
{
private GroupBox gboxGrp1;
private GroupBox gboxGrp2;
private RadioButton butn1a;
private RadioButton butn1b;
private RadioButton butn2a;
private RadioButton butn2b;
private Button btnClose;
public myForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnClose = new Button();
this.gboxGrp1 = new GroupBox();
this.gboxGrp2 = new GroupBox();
this.butn1a = new RadioButton();
this.butn1b = new RadioButton();
this.butn2a = new RadioButton();
this.butn2b = new RadioButton();
//myForm
this.Text = "My Form";
this.StartPosition = FormStartPosition.CenterScreen;
this.Height = 350;
this.Width = 200;
//btnClose
this.Controls.Add(btnClose);
this.btnClose.Text = "Close";
this.btnClose.Location = new Point(60, 260);
this.btnClose.Click += new EventHandler(btnClose_Click);
//gboxgrp1
this.gboxGrp1.Location = new Point(20, 20);
this.gboxGrp1.Text = "Group Box 1";
this.gboxGrp1.Width = 150;
this.gboxGrp1.Height = 100;
//gboxgrp2
this.gboxGrp2.Text = "Group Box 2";
this.gboxGrp2.Location = new Point(20, 130);
this.gboxGrp2.Width = 150;
this.gboxGrp2.Height = 100;
//Radio buttons
this.butn1a.Text = "Radio 1a";
this.butn1a.Location = new Point(30, 30);
this.butn1a.Size = new Size(90, 15);
this.butn1b.Text = "Radio 1b";
this.butn1b.Location = new Point(30, 60);
this.butn1b.Size = new Size(90, 15);
this.butn2a.Text = "Radio 2a";
this.butn2a.Location = new Point(30, 30);
this.butn2a.Size = new Size(90, 15);
this.butn2b.Text = "Radio 2b";
this.butn2b.Location = new Point(30, 70);
this.butn2b.Size = new Size(90, 15);
//Controls
this.Controls.Add(gboxGrp1);
this.Controls.Add(gboxGrp2);
this.gboxGrp1.Controls.Add(butn1a);
this.gboxGrp1.Controls.Add(butn1b);
this.gboxGrp2.Controls.Add(butn2a);
this.gboxGrp2.Controls.Add(butn2b);
}
private void btnClose_Click(object sender, EventArgs e)
{
MessageBox.Show("Closing Application");
Application.Exit();
}
}
public class MyApp
{
public static void Main()
{
Application.Run(new myForm());
}
}
You can't restyle the default MessageBox as that's completely depends on the windows OS theme. However you could create your own message box by simply creating a new form and then call it by using newMessagebox.ShowDialog();
I add a MenuStrip to my form and I would like to add other controls below it like usual Point(0, 0) is the top left corner of empty form space. After I add the menu to my form and add more controls they overlap each other. So I want to take away some height of the client rect for the menu and a button with Location = (0,0) must be RIGHT below the menu.
How do I do that ?
If I assign a MainMenu to Menu property of the form it does that automatically but I really want and need to use MenuStrip.
Edit: this doesn't work:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = 50;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 25, 128, 50);
Controls.Add(b);
While this works like I would like it to do with MenuStrip:
Menu = new MainMenu();
Menu.MenuItems.Add("File");
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
When you SetBounds(0, 25, 128, 50), you are actually setting b.Top to 25 (the second parameter). In order to set the top bound relative to the menu control, use:
b.SetBounds(0, menu.Bottom, 128, 50);
[UPDATE]
Alternatively, you could use:
public partial class Form1 : Form
{
private int menuStripHeight = 50;
public Form1()
{
InitializeComponent();
this.ControlAdded += Form1_ControlAdded;
}
private void Form1_Load(object sender, EventArgs e)
{
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
// note that the position used is 0,0
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
}
void Form1_ControlAdded(object sender, ControlEventArgs e)
{
if (e.Control.GetType().FullName != "System.Windows.Forms.MenuStrip")
e.Control.Top += menuStripHeight;
}
}
[UPDATE 2]
Or you could just use a Panel:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Panel p = new Panel();
p.SetBounds(0, menuStripHeight, this.Width, this.Height);
Controls.Add(p);
Button b = new Button();
b.Text = "hello world";
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
Use DockStyle.Top in both MenuStrip and Panel, but add them in the reverse order. Adding a control with Dock=Top puts this last control closest to the border, that is, on top of all the other controls. So without resorting to private constants and event handlers:
MenuStrip menu = new MenuStrip() {
AutoSize = false,
Dock = DockStyle.Top
};
menu.Items.Add("File");
Panel p = new Panel(){
Dock = DockStyle.Top
};
Controls.Add(p);
Controls.Add(menu);
MainMenuStrip = menu;
Button b = new Button(){
Text = "hello world"
};
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
I have a little demonstration below of a peculiar problem.
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public class TestForm : Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TextBox textBox1;
public TestForm()
{
//Controls
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.textBox1 = new System.Windows.Forms.TextBox();
// tabControl1
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(12, 12);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(260, 240);
this.tabControl1.TabIndex = 0;
this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);
// tabPage1
this.tabPage1.Controls.Add(this.textBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(252, 214);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
// tabPage2
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(192, 74);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
// textBox1
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(6, 38);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 20);
this.textBox1.TabIndex = 0;
// TestForm
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
}
//Tab Selected
private void tabControl1_Selected(object sender, EventArgs e)
{
this.Text = "TextBox Width: " + this.textBox1.Width.ToString();
}
}
//Main
static class Program
{
static void Main()
{
Application.Run(new TestForm());
}
}
}
If you run the above C# code you will have a small form containing a tabcontrol. Within the tabcontrol is a texbox on the first tab. If you follow these steps you will see the problem:
Select tabPage2 (textBox1's width is reported in the form title)
Resize the form
Select tabPage1 (The wrong textBox1 width is reported)
Any ideas what is going on here? The textbox is obviously bigger than what is being reported. If you click again on tabPage2 the correct size is then updated. Obviously there is an event updating the width of textBox1. Can i trigger this when tabPage1 is selected?
Firstly, thanks for the complete program - it made it much easier to work out what was going on!
While the textbox isn't visible, it isn't resized. When you select tabPage1, the Selected event fires before the controls become visible and the textbox gets laid out again.
Now, that's why it's happening - but what's your real situation? If you actually want to capture the size of controls changing, subscribe to their Resize events. If not, could you explain more about what you're trying to achieve?
I'm pretty sure that what's happening is the Selected event is raised slightly before the tab page becomes visible. The text box is not resized until the tab page becomes visible, so you end up checking the value of the text box's size before it is actually resized. When you change tabs again, the text box is already resized, so you get the correct value.
Change the last few lines of your example form to look like this and it will become apparent:
this.textBox1.SizeChanged += TextboxSizeChanged;
}
//Tab Selected
private void tabControl1_Selected(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("tab selected");
this.Text = "TextBox Width: " + this.textBox1.Width.ToString();
}
private void TextboxSizeChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Textbox resized");
}
If you modify your code a little by adding an event handler to the textbox1.Resize event you will see what happens.
The tabPage1.Selected event occurs before the controls in the tab page is resized so when you check the width of the textbox you are checking it before it is resized.
Normally this wouldn't be a problem, for the resizing is done properly afterwards, but I guess that you will be using the size of the textbox for something?
You should be able to write your own TabControl that fixes this problem, but you will have to experiment to see what works here.
Not sure if I understand the problem.
But, you might use textbox's resize event to capture the width change OR form's resize.
In your example, does the select event of tabPage1 fire when you do step 3?