|
Programming -
.Net and C# Articles
|
|
If you have ever wanted a Label or TextBox in Windows Forms that performs a little more like on the web, then you've probably figured out that there's no intuitive way to make a Label or TextBox automatically adjust its height to fit the text it contains. While it may not be intuitive, it's definitely not impossible. In this example, I'll use a TextBox (you could just as easily use a Label) that is docked to the top of a form.To use this, add aTextBox called MyTextBox to the form, and set Dock to DockStyle.Top. Wire up the Resize event of the TextBox to this event handler. private void MyTextBox_Resize( object sender, EventArgs e )
{
// Grab a reference to the TextBox
TextBox tb = sender as TextBox;
// Figure out how much space is used for borders, etc.
int chromeHeight = tb.Height - tb.ClientSize.Height;
// Create a proposed size that is very tall, but exact in width.
Size proposedSize = new Size( tb.ClientSize.Width, int.MaxValue );
// Measure the text using the TextRenderer
Size textSize = TextRenderer.MeasureText( tb.Text, tb.Font,
proposedSize, TextFormatFlags.TextBoxControl
| TextFormatFlags.WordBreak );
// Adjust the height to include the text height, chrome height,
// and vertical margin
tb.Height = chromeHeight + textSize.Height
+ tb.Margin.Vertical;
} If you want to resize the a Label or TextBox that is not docked (for example, one that is in a FlowLayoutPanel or other Panel, or just placed on the form), then you can handle the Form's Resize even instead, and just modify the Control's properties directly. |
|
Last Updated ( Tuesday, 17 March 2009 16:54 )
|
|
Blogs -
CodeCreations Blog
|
|
Media: 7000.0.081212-1400_client_en-us_Ultimate-GB1CULFRE_EN_DVD.iso Microsoft today hopefully enters the first public BETA phase for its upcoming Windows 7 operating system, a successor to the two+ year old Windows Vista. I myself have been using graphical operating systems since at least 1986's Amiga computers and almost all of Microsoft's Window's for PC's since Windows 3.1. I've used the OS for two days so far and here are my impressions. Booting my computer with the Windows 7 BETA DVD, I chose the custom install option and I selected an empty 40GB partition on my hard drive. It did not ask me to review and confirm my choice, which would have been nice. The Windows 7 BETA installer recognized the Vista multiboot menu (which had XP/Vista/Fedora 9) and added a new default boot entry for Windows 7 (32bit). Installation was much quicker than Vista's and went without a problem. |
|
Last Updated ( Saturday, 10 January 2009 21:13 )
|
|
Read more...
|
|
|
Blogs -
CodeCreations Blog
|
|
You can create a ToolStrip CheckBox control by simply extending ToolStripControlHost, but in order to get it to show ToolTip help like all the standard ToolStrip controls, you need to jump through the hoop of adding a MouseHover event handler to bubble up the event. In this example, I create a FlowLayoutPanel to hold the new CheckBox so I can add more widgets to the same ToolStrip control later if necessary -- like an image to display a validation error or some other indicator. I create the ToolStripCheckBox by first calling the base contructor (ToolStripControlHost) with a new FlowLayoutPanel. Then I can set some panel properties and add the CheckBox control. Finally, I need to add an event handler for the CheckBox's MouseHover event, since this is the event that will cause the ToolTip to display. The event handler simply calls the ToolStripCheckBox's OnMouseHover() method, effectively making it appear that the ToolStripControlHost is raising the event. Show tool tips for this control, and you should see them when you mouse over the CheckBox. public partial class ToolStripCheckBox
: ToolStripControlHost
{
private FlowLayoutPanel panel;
private CheckBox chk = new CheckBox();
public ToolStripCheckBox()
: base ( new FlowLayoutPanel() )
{
panel = (FlowLayoutPanel)base.Control;
panel.BackColor = Color.Transparent;
panel.Controls.Add( chk );
chk.MouseHover += new EventHandler( chk_MouseHover );
}
void chk_MouseHover( object sender, EventArgs e )
{
this.OnMouseHover( e );
}
}Update 12/30/08: Vladimir Ilic suggests the following to make the ToolStripCheckBox available through the designer and to show the text: (Thanks, Vladimir!) ToolStripItemDesignerAvailability is in System.Windows.Forms.Design. [ToolStripItemDesignerAvailability(
ToolStripItemDesignerAvailability.ToolStrip )]
[ToolboxBitmap( "..\..\Images\CheckBox.bmp" )]
public class ToolStripCheckBox
: ToolStripControlHost
{
...
protected override void OnTextChanged( EventArgs e )
{
base.OnTextChanged( e );
chk.Text = this.Text;
}
} |
|
Last Updated ( Tuesday, 30 December 2008 02:53 )
|
|
Programming -
.Net and C# Articles
|
|
Type safety basically means using strongly-typed data to protect against nasty bugs caused by type errors. A “strong type” is a programming construct that imposes certain rules or restrictions on specific data and how the data can be used. A “type error” is a bug that only appears at runtime (as opposed to compile time), and can have a considerably undesirable impact on data – often it only appears when you discover that something has gone terribly wrong. So, we should always strive to write type-safe code. Here is an example. The following method call would not be considered type safe: |
|
Last Updated ( Saturday, 26 July 2008 20:16 )
|
|
Read more...
|
|
|
Programming -
.Net and C# Articles
|
|
In this series, I’ll take a stab at explaining the different types of objects used in every day applications, how they are held in memory, and how they are eventually cleaned up. Along the way, I’ll hopefully clear up some of the haze that generally surrounds the stack, the heap, object finalization, memory leaks, “unmanaged resources,” the GC (garbage collector), and some other things. This is Part 1 of 4, covering managed and unmanaged code, and the stack. Part 2 will discuss the heap. Part 3 will cover boxing and unboxing, stucts and classes, nondeterministic finalization, IDisposable, and garbage collection. Part 4 will discuss buffer overruns, stack overflows, how to hack and Xbox, and a few best practices for memory-management-aware programming in .Net. |
|
Last Updated ( Sunday, 03 August 2008 22:27 )
|
|
Read more...
|
|
Software -
Applications
|
|
Amazon's affiliate program can be big business, and many web sites rely solely on Amazon affiliate program referral frees for their income. It's a good business model for Amazon.com because it helps drive traffic and sales to their site, and it's a good deal for the affiliates because they get a small piece of the pie by creating links to Amazon.com products that are encoded with their affiliate IDs. But because affiliate information can be encoded into product links, there are also a lot of people out there who try to "hijack" affiliate revenue on web sites by posting links to products that use their own affiliate tags or IDs. This article will describe a couple of different methods for creating Amazon.com product links with encoded affiliate IDs, and will introduce a tool that site owners or content publishers can use to help thwart affiliate revenue hijackers by making it very easy for users to create appropriately tagged Amazon.com product links. |
|
Last Updated ( Monday, 04 August 2008 23:34 )
|
|
Read more...
|
|
|
|
|
<< Start < Prev 1 2 3 4 Next > End >>
|
|
Page 1 of 4 |