Click here to Skip to main content
16,019,983 members
Home / Discussions / C#
   

C#

 
GeneralWinForms: SuspendLayout on Application Pin
troels_sorensen17-Feb-04 2:06
troels_sorensen17-Feb-04 2:06 
GeneralRe: WinForms: SuspendLayout on Application Pin
Heath Stewart17-Feb-04 4:54
protectorHeath Stewart17-Feb-04 4:54 
QuestionResize all items? Pin
thomasa17-Feb-04 1:51
thomasa17-Feb-04 1:51 
AnswerRe: Resize all items? Pin
Heath Stewart17-Feb-04 4:57
protectorHeath Stewart17-Feb-04 4:57 
GeneralRe: Resize all items? Pin
je_gonzalez17-Feb-04 17:28
je_gonzalez17-Feb-04 17:28 
GeneralRe: Resize all items? Pin
Heath Stewart17-Feb-04 19:35
protectorHeath Stewart17-Feb-04 19:35 
GeneralRe: Resize all items? Pin
je_gonzalez18-Feb-04 3:20
je_gonzalez18-Feb-04 3:20 
AnswerRe: Resize all items? Pin
je_gonzalez17-Feb-04 17:21
je_gonzalez17-Feb-04 17:21 
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Workbench
{
/// <summary>
/// A panel that resizes controls automatically.
/// </summary>
public class AutoSizePanel : Panel
{
/// <summary>
/// Holds the control info.
/// </summary>
private Hashtable htInfo = new Hashtable();
/// <summary>
/// Flag to prevent recursive call.
/// </summary>
private bool bAllowResize = true;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

/// <summary>
/// Constructor.
/// </summary>
public AutoSizePanel()
{
InitializeComponent();
}

/// <summary>
/// Adds a control to the list of controls.
/// </summary>
/// <param name="e"></param>
protected override void OnControlAdded(ControlEventArgs e)
{
// Hold the resizing
bAllowResize = false;

// Create the original info
SizingInfo riData = new SizingInfo(e.Control);

// Add to table
htInfo.Add(e.Control, riData);

// BAck to normal
bAllowResize = true;

//
base.OnControlAdded (e);
}

/// <summary>
/// Removes a control from the list of controls.
/// </summary>
/// <param name="e"></param>
protected override void OnControlRemoved(ControlEventArgs e)
{
// Delete the info
htInfo.Remove(e.Control);

//
base.OnControlRemoved (e);
}

/// <summary>
/// Does the magic.
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
// Only once
if (bAllowResize)
{
// Set
bAllowResize = false;

// And apply to each control
foreach (Control ctl in this.Controls)
{
// Get the original info
SizingInfo riCtl = htInfo[ctl] as SizingInfo;

// Info available?
if (riCtl != null)
{
// Resize
riCtl.Resize(ctl);
}
}

// Reset
bAllowResize = true;
}

//
base.OnResize (e);
}

protected override void OnPaint(PaintEventArgs e)
{
// Hold the resizing
bAllowResize = false;

// Collect info
foreach (Control ctl in this.Controls)
{
// Get the original info
SizingInfo riCtl = htInfo[ctl] as SizingInfo;

// Info available?
if (riCtl != null)
{
// Update
riCtl.Update(ctl);
}
}

// Back to normal
bAllowResize = true;

//
base.OnPaint (e);
}


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component 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()
{
//
// AutoSizePanel
//
this.Name = "AutoSizePanel";
this.Size = new System.Drawing.Size(232, 184);

}
#endregion

/// <summary>
/// This holds the original control information.
/// </summary>
private class SizingInfo
{
/// <summary>
/// Info to be kept.
/// </summary>
public int iTop;
public int iLeft;
public int iHeight;
public int iWidth;

public int iParentHeight;
public int iParentWidth;

/// <summary>
/// Constructor
/// </summary>
/// <param name="ctl"></param>
public SizingInfo(Control ctl)
{
// Save
this.Initialize(ctl);
}

/// <summary>
/// Updates the internal info
/// </summary>
/// <param name="ctl"></param>
public void Update(Control ctl)
{
// Compute the change.
double dRatioHeight = (double) ctl.Parent.Height / iParentHeight;
double dRatioWidth = (double) ctl.Parent.Width / iParentWidth;

// And where it would go.
int iWTop = (int) (iTop * dRatioHeight);
int iWHeight = (int) (iHeight * dRatioHeight);
int iWLeft = (int) (iLeft * dRatioWidth);
int iWWidth = (int) (iWidth * dRatioWidth);

// Changes?
if ((iWTop != ctl.Top) || (iWLeft != ctl.Left) || (iWHeight != ctl.Height) || (iWWidth != ctl.Width))
{
// Reset
this.Initialize(ctl);
}
}

/// <summary>
/// Does the magic
/// </summary>
/// <param name="ctl"></param>
public void Resize(Control ctl)
{
// Compute the change.
double dRatioHeight = (double) ctl.Parent.Height / iParentHeight;
double dRatioWidth = (double) ctl.Parent.Width / iParentWidth;

// And shake it
ctl.Top = (int) (iTop * dRatioHeight);
ctl.Height = (int) (iHeight * dRatioHeight);
ctl.Left = (int) (iLeft * dRatioWidth);
ctl.Width = (int) (iWidth * dRatioWidth);
}

/// <summary>
/// Saves the base info
/// </summary>
/// <param name="ctl"></param>
private void Initialize(Control ctl)
{
// Save
iTop = ctl.Top;
iLeft = ctl.Left;
iHeight = ctl.Height;
iWidth = ctl.Width;

iParentHeight = ctl.Parent.Height;
iParentWidth = ctl.Parent.Width;
}
}
}
}


GeneralProblems with thread cancellation. Pin
Chen Pang17-Feb-04 1:21
Chen Pang17-Feb-04 1:21 
GeneralRe: Problems with thread cancellation. Pin
Corinna John17-Feb-04 1:41
Corinna John17-Feb-04 1:41 
GeneralRe: Problems with thread cancellation. Pin
Chen Pang17-Feb-04 1:53
Chen Pang17-Feb-04 1:53 
QuestionException Handling . To throw or not to throw? Pin
bzurer17-Feb-04 1:05
bzurer17-Feb-04 1:05 
AnswerRe: Exception Handling . To throw or not to throw? Pin
Jonathan de Halleux17-Feb-04 1:13
Jonathan de Halleux17-Feb-04 1:13 
GeneralRe: Exception Handling . To throw or not to throw? Pin
bzurer17-Feb-04 1:50
bzurer17-Feb-04 1:50 
GeneralRe: Exception Handling . To throw or not to throw? Pin
Jonathan de Halleux17-Feb-04 2:09
Jonathan de Halleux17-Feb-04 2:09 
GeneralRe: Exception Handling . To throw or not to throw? Pin
bzurer17-Feb-04 1:56
bzurer17-Feb-04 1:56 
AnswerRe: Exception Handling . To throw or not to throw? Pin
Heath Stewart17-Feb-04 5:04
protectorHeath Stewart17-Feb-04 5:04 
GeneralAccessing properties as an array Pin
Omega50116-Feb-04 21:28
Omega50116-Feb-04 21:28 
GeneralRe: Accessing properties as an array Pin
thomasa16-Feb-04 23:32
thomasa16-Feb-04 23:32 
GeneralRe: Accessing properties as an array Pin
Heath Stewart17-Feb-04 5:11
protectorHeath Stewart17-Feb-04 5:11 
GeneralRe: Accessing properties as an array Pin
OmegaSupreme17-Feb-04 0:46
OmegaSupreme17-Feb-04 0:46 
GeneralRe: Accessing properties as an array Pin
Jonathan de Halleux17-Feb-04 1:15
Jonathan de Halleux17-Feb-04 1:15 
GeneralRe: Accessing properties as an array Pin
OmegaSupreme17-Feb-04 1:46
OmegaSupreme17-Feb-04 1:46 
GeneralRe: Accessing properties as an array Pin
Omega50117-Feb-04 9:34
Omega50117-Feb-04 9:34 
GeneralRe: Accessing properties as an array Pin
OmegaSupreme17-Feb-04 12:43
OmegaSupreme17-Feb-04 12:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.