I would like to create a button with a plus sign, however when I created the plus sign with the text, it is not aligned from the top of the button as in the image below:
The problem occurs when I try to increase the font size. In default size of the text the plus sign is centered. What can I do to center it in my case?
TextAlign (TopLeft) and UseCompatibleTextRendering didn't work. (Winforms C# Visual Studio 2019)
Note: Text align property did not work because my font size is larger than default it is 36+.
You can use an png of + from resource instead of setting directly the text property of the button.
Example
this.button1.Image = NameSpace1.Properties.Resources.Image2.png;
One way is to set the Button's FlatStyle (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.buttonbase.flatstyle?view=net-5.0) to System (System.Windows.Forms.FlatStyle.System), if you are Ok to change the FlatStyle.
I created a test application and the combination of font "Microsoft Sans Serif" in size 36pt with button size 32x32px and FlatStyle = FlatStyle.System (as elimad already mentioned) works quite well for me.
namespace ControlTest
{
public class MainForm : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
private Button button1;
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 32);
this.button1.TabIndex = 0;
this.button1.Text = "+";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(157, 92);
this.Controls.Add(this.button1);
this.Name = "MainForm";
this.Text = "ControlTest";
this.ResumeLayout(false);
}
}
}
You can create custom controls and then render the text yourself
https://www.c-sharpcorner.com/UploadFile/f5a10c/creating-custom-controls-in-C-Sharp/
After compiling it should just show up inside the toolbox
Related
I'm working in a c# application in winform.
I saw that there is no Style for element (unlike WPF). But is there a way to simply set all the labels to a specific design ?
Actually I do :
public partial class myControl : UserControl
{
private Color LabelColor = Color.Indigo;
private Color LabelFont = new System.Drawing.Font("Arial",
18F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
public myControl()
{
InitializeComponent();
//Set design
designLabels();
}
private void designLabels()
{
List<Label> labelsToStyle = new List<Label>();
labelsToStyle.Add(labelName);
labelsToStyle.Add(labelAge);
labelsToStyle.Add(labelSize);
foreach (Label l in labelsToStyle)
{
l.ForeColor = LabelColor;
l.Font = LabelFont;
l.Dock = DockStyle.Fill;
}
}
}
It works but it doesn't display correctly in designer (I have to run application to see my design). And maybe it exists a simplest way ?
As per my comment the easiest is to create a custom control and use that one in your windows.
here how simple it is. Simply override the label and set in constructor the default value you want
public class DesignLabel : Label
{
public DesignLabel()
{
ForeColor = Color.Indigo;
Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
then compile once and go to your design view, open your toolbox and you will see the "DesignLabel", simply drag drop on the window and that's it. If you change default in the class it will be changed all over the place.
If you want to change the style already at design time so that you can see in in the Form.cs[design] you need to edit the Form.Designer.cs file! But this you have to do label for label by hand. I saved in this example the Font and Color in the project properties (sorry for the german version):
in my example I have 3 Labels. In the Form.Designer.cs file you can add the properties:
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 15);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Font = Properties.Settings.Default.LabelFont;
this.label1.ForeColor = Properties.Settings.Default.LabelColor;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 68);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 15);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
this.label2.Font = Properties.Settings.Default.LabelFont;
this.label2.ForeColor = Properties.Settings.Default.LabelColor;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 122);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 15);
this.label3.TabIndex = 2;
this.label3.Text = "label3";
this.label3.Font = Properties.Settings.Default.LabelFont;
this.label3.ForeColor = Properties.Settings.Default.LabelColor;
The result looks like this:
DISCLAIMER
I would not recommend this approach! Editing the Form.Desginer.cs file is never a good idea! I would stick to the run time changes. If you want to change all Labels just filter the this.Controls for it and foreach through the collection like this:
this.Controls.OfType<Label>().ToList().ForEach(lbl =>
{
lbl.Font = LabelFont;
lbl.ForeColor = LabelColor;
//lbl.Dock = DockStyle.Fill; // uncommented, because I could see only 1 Label
});
The result will be the same.
In C# Form i'am trying to make nicely looking Button but i cant get over text "padding" problem.
Problematic Button
Desired Button
I already made my mind of how it should look but i just can't achieve it.
It is supposed to be Flat Button with Black Borders and "Options" text in it (like on second picture).
But some kind of "padding" hides pretty big part of the text.
Changing Font Size kinda helped but i want to perserve Button's ~16px Height and Font that small so it can fit in there is just unreadable.
I've already tried setting Button's Padding property to 0.
I was already thinking about some workaround like overriding OnPaint Event / making multiple controls (like, combine it with label) but i'am worried about performance impact.
Hm I took a shot at it, this is the best I could do.
I used an image with a flat button, with the border providing the grey area beyond the black border. Sort of a workaround. Whatever.
Here is the image for the button background .
And the code for the button:
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.button1.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.button1.FlatAppearance.BorderSize = 4;
this.button1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Silver;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.ForeColor = System.Drawing.Color.White;
this.button1.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
this.button1.Location = new System.Drawing.Point(94, 124);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(61, 28);
this.button1.TabIndex = 0;
this.button1.Text = "Options";
this.button1.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.button1.UseVisualStyleBackColor = false;
So i solved it with
class FixedButton : Button {
public string FixedText;
public Point TextOffset;
/*public PointF FixedTextLocation {
get{return new PointF( (float)(Location.X+TextOffset.X), (float)(Location.Y+TextOffset.Y) );}
}*/
protected override void OnPaint(PaintEventArgs e){
base.OnPaint(e);
if(String.IsNullOrEmpty(Text) && !String.IsNullOrEmpty(FixedText) ){
e.Graphics.DrawString(FixedText, Font, new SolidBrush(ForeColor), TextOffset);
}
}
}
EDIT: I found out i need to use TextOffset instead of FixedTextLocation in DrawString / DrawText (which can also be used instead of DrawString).
I have a system windows forms web browser on top of another control. The web browser has an image set.
On top of the web browser control is an inherited panel where I can control the opacity.
If I make this transparent, it doesn't show the web browser image, it shows the back colour of the control under the web browser.
Ie
Layer1 control. Back colour is blue
Layer2 web browser with image as HTML
Layer3 transparent panel
Layer3 when transparent shows layer1 not layer2
How can I make the web browser opaque so layer3 shows through to layer2 (webbrowser) ?
I have tried setting SetStyles to control opacity on the web browser.
Thanks
Short Answer:
You can't.
Real World Solution:
Have you looked into WPF?
Long Answer:
Opacity is a trick that WinForms does.
When a control is marked to be displayed transparent (or semi transparent), WinForms queries the parent object's visuals to ask "What would you have printed here had my control not existed?". From this result WinForms either displays the pixel of the control, the pixel of the parent, or a combination of the two (if semi transparent).
This becomes really apparent in a simple example:
Create a new winforms project
Create 2 labels, place them over top of each other (slighty offset)
Set both labels backgrounds to Color.Transparent
You'll see immediately that transparency takes a chunk out of the label underneath.
Example Code (All in one file, compile and run):
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication5
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
public class MainForm : Form
{
private Random random = new Random();
private Button btnRandomBackgroundColor;
private Label lblBackgroundLabel;
private Label lblTransparent;
public MainForm()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
BackColor = Color.FromArgb(random.Next(0, 255),
random.Next(0, 255),
random.Next(0, 255));
}
private void InitializeComponent()
{
this.btnRandomBackgroundColor = new System.Windows.Forms.Button();
this.lblBackgroundLabel = new System.Windows.Forms.Label();
this.lblTransparent = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnRandomBackgroundColor
//
this.btnRandomBackgroundColor.Location = new System.Drawing.Point(12, 12);
this.btnRandomBackgroundColor.Name = "btnRandomBackgroundColor";
this.btnRandomBackgroundColor.Size = new System.Drawing.Size(144, 23);
this.btnRandomBackgroundColor.TabIndex = 0;
this.btnRandomBackgroundColor.Text = "Randomize Background Color";
this.btnRandomBackgroundColor.UseVisualStyleBackColor = true;
this.btnRandomBackgroundColor.Click += button_Click;
//
// lblBackgroundLabel
//
this.lblBackgroundLabel.AutoSize = true;
this.lblBackgroundLabel.BackColor = System.Drawing.Color.Transparent;
this.lblBackgroundLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblBackgroundLabel.Location = new System.Drawing.Point(41, 49);
this.lblBackgroundLabel.Name = "lblBackgroundLabel";
this.lblBackgroundLabel.Size = new System.Drawing.Size(184, 33);
this.lblBackgroundLabel.TabIndex = 1;
this.lblBackgroundLabel.Text = "Simple Label";
//
// lblTransparent
//
this.lblTransparent.AutoSize = true;
this.lblTransparent.BackColor = System.Drawing.Color.Transparent;
this.lblTransparent.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblTransparent.Location = new System.Drawing.Point(61, 63);
this.lblTransparent.Name = "lblTransparent";
this.lblTransparent.Size = new System.Drawing.Size(251, 33);
this.lblTransparent.TabIndex = 2;
this.lblTransparent.Text = "Transparent Label";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(341, 114);
Controls.Add(this.lblTransparent);
this.Controls.Add(this.lblBackgroundLabel);
this.Controls.Add(this.btnRandomBackgroundColor);
this.Name = "Form1";
this.Text = "MainForm";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
I am new to WinForms, and I am very used to styling in CSS, so maybe I am not looking at WinForm's Bottom Margin property correctly, but, no matter what element I set an arbitrarily large bottom-margin number to, it seems to have no effect, at all.
What I want is to extend the design of the form to below the initial viewable window (vertical-scroll bar shows up just fine), and set a bottom-margin to these elements so that the very bottom of the element isn't flush with the very bottom of the window (a little space would be nice).
I have tried this on several elements just to see if it was only the one element (or the fact that it was out of the initial visible part of the window) that was giving me problems, but I can't seem to get any effect out of the margin property at all.
Looking here: http://msdn.microsoft.com/en-us/library/ms229627.aspx It seems that this is, indeed, what the margin property should do. Also, I can't find any padding within the GUI controls for any element.
As it stands, I am mostly only coding C# for the event handlers, until I get a better grasp of where Visual Studio puts everything within the two partial classes (and the other .cs files).
If it helps, here is the code for the designer file:
namespace WindowsFormsApplication1
{
partial class IntroForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.WelcomeHeader = new System.Windows.Forms.Label();
this.ActionSelect = new System.Windows.Forms.ComboBox();
this.ProceedBtn = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// WelcomeHeader
//
this.WelcomeHeader.AutoSize = true;
this.WelcomeHeader.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.WelcomeHeader.Location = new System.Drawing.Point(84, 30);
this.WelcomeHeader.Name = "WelcomeHeader";
this.WelcomeHeader.Size = new System.Drawing.Size(367, 25);
this.WelcomeHeader.TabIndex = 0;
this.WelcomeHeader.Text = "Please Select Which Content You";
//
// ActionSelect
//
this.ActionSelect.ForeColor = System.Drawing.SystemColors.WindowFrame;
this.ActionSelect.FormattingEnabled = true;
this.ActionSelect.Items.AddRange(new object[] {
"Events",
"Headline News",
"Images For Slideshow",
"Agendas",
"Job Opportunities",
"Schedule Of Meetings",
"Legal Notices",
"Main Street (Main Link)",
"Tourism (Main Link)",
"Rental Properties",
"Concert In The Park",
"Main Street News Letters"});
this.ActionSelect.Location = new System.Drawing.Point(126, 116);
this.ActionSelect.Name = "ActionSelect";
this.ActionSelect.Size = new System.Drawing.Size(283, 28);
this.ActionSelect.TabIndex = 1;
this.ActionSelect.Text = "Please Select";
//
// ProceedBtn
//
this.ProceedBtn.BackColor = System.Drawing.SystemColors.ButtonFace;
this.ProceedBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.ProceedBtn.ForeColor = System.Drawing.Color.DimGray;
this.ProceedBtn.Location = new System.Drawing.Point(221, 192);
this.ProceedBtn.Name = "ProceedBtn";
this.ProceedBtn.Size = new System.Drawing.Size(93, 34);
this.ProceedBtn.TabIndex = 2;
this.ProceedBtn.Text = "Proceed";
this.ProceedBtn.UseVisualStyleBackColor = false;
this.ProceedBtn.Click += new System.EventHandler(this.ProceedBtn_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(142, 55);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(251, 25);
this.label1.TabIndex = 3;
this.label1.Text = "Would Like To Change";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(147, 268);
this.richTextBox1.Margin = new System.Windows.Forms.Padding(3, 3, 3, 30);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(246, 269);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
//
// IntroForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(102)))), ((int)(((byte)(0)))));
this.ClientSize = new System.Drawing.Size(535, 306);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.ProceedBtn);
this.Controls.Add(this.ActionSelect);
this.Controls.Add(this.WelcomeHeader);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(204)))), ((int)(((byte)(153)))), ((int)(((byte)(0)))));
this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.Name = "IntroForm";
this.Text = "Okmulgee Online Web File Generator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label WelcomeHeader;
private System.Windows.Forms.ComboBox ActionSelect;
private System.Windows.Forms.Button ProceedBtn;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
------------------------------------------UPDATE-------------------------------------------
The element I have set the bottom margin property to within the code above is simply, richTextBox1.
Also, I did find a Padding element to the main form element, but sadly, this doesn't push other elements away from its edges either :(
What do these properties do (margin, padding)?
The Margin property is used by the automatic layout feature built into Winforms. But it does require that you allow the container to grow so it can provide the requested margin. So you must set the form's AutoSize property to True.
Combining AutoSize and AutoScroll is possible, you can set the MaximumSize property to prevent it from growing too much. The scrollbar automatically appears when the layout calculation produces a layout that exceeds the MaximumSize. The default maximum size is the Screen.WorkingArea on which the form is displayed, usually good enough.
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?