Click here to Skip to main content
15,881,248 members
Home / Discussions / C#
   

C#

 
Questionhow can i save this? Pin
Peter Reiter16-Jan-04 23:03
Peter Reiter16-Jan-04 23:03 
AnswerRe: how can i save this? Pin
Mazdak17-Jan-04 0:29
Mazdak17-Jan-04 0:29 
AnswerRe: how can i save this? Pin
Corinna John17-Jan-04 6:34
Corinna John17-Jan-04 6:34 
GeneralWindows Application Screen Resolution problem Pin
koosala16-Jan-04 18:13
koosala16-Jan-04 18:13 
GeneralRe: Windows Application Screen Resolution problem Pin
Mazdak16-Jan-04 19:27
Mazdak16-Jan-04 19:27 
GeneralRe: Windows Application Screen Resolution problem Pin
Heath Stewart16-Jan-04 19:40
protectorHeath Stewart16-Jan-04 19:40 
GeneralRe: Windows Application Screen Resolution problem Pin
leppie17-Jan-04 7:05
leppie17-Jan-04 7:05 
GeneralRe: Windows Application Screen Resolution problem Pin
je_gonzalez17-Jan-04 8:40
je_gonzalez17-Jan-04 8:40 
The problem with anchors is that theyonly anchor to the parent control/form, and when the control/form shrinks the controls overlap.

The problem with docking is that the change in size is only shown in the fill'd control, and there is only one of these.

What you want is to have the control container resize the child controls, for that you need to create an "AutoSizePanel", have that dock'd fill and then set your controls on the panel.

Here is the code for the "AutoSizePanel":

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;
}
}
}
}

This code is being made available to all freely.... Have fun, but do not copyright.
GeneralRe: Windows Application Screen Resolution problem Pin
leppie17-Jan-04 9:26
leppie17-Jan-04 9:26 
GeneralRe: Windows Application Screen Resolution problem Pin
je_gonzalez17-Jan-04 9:33
je_gonzalez17-Jan-04 9:33 
QuestionWho can help me about this question? Pin
Feelyn200816-Jan-04 15:24
Feelyn200816-Jan-04 15:24 
AnswerRe: Who can help me about this question? Pin
Colin Angus Mackay16-Jan-04 15:53
Colin Angus Mackay16-Jan-04 15:53 
GeneralRe: Who can help me about this question? Pin
je_gonzalez16-Jan-04 17:02
je_gonzalez16-Jan-04 17:02 
GeneralRe: Who can help me about this question? Pin
Forrest Feather17-Jan-04 9:43
Forrest Feather17-Jan-04 9:43 
AnswerRe: Who can help me about this question? Pin
je_gonzalez16-Jan-04 17:18
je_gonzalez16-Jan-04 17:18 
GeneralRe: Who can help me about this question? Pin
Daniel Turini17-Jan-04 5:03
Daniel Turini17-Jan-04 5:03 
GeneralRe: Who can help me about this question? Pin
je_gonzalez17-Jan-04 8:57
je_gonzalez17-Jan-04 8:57 
GeneralDatagrid and c# Pin
caheo16-Jan-04 14:08
caheo16-Jan-04 14:08 
GeneralRe: Datagrid and c# Pin
Heath Stewart16-Jan-04 19:29
protectorHeath Stewart16-Jan-04 19:29 
QuestionEditboxes show unknown chars as ?? Pin
Anonymous16-Jan-04 14:02
Anonymous16-Jan-04 14:02 
AnswerRe: Editboxes show unknown chars as ?? Pin
LiquidKnight16-Jan-04 14:06
LiquidKnight16-Jan-04 14:06 
AnswerRe: Editboxes show unknown chars as ?? Pin
Heath Stewart16-Jan-04 19:22
protectorHeath Stewart16-Jan-04 19:22 
GeneralGive name to table from stored procedure Pin
laphijia16-Jan-04 8:29
laphijia16-Jan-04 8:29 
GeneralRe: Give name to table from stored procedure Pin
Rocky Moore16-Jan-04 8:44
Rocky Moore16-Jan-04 8:44 
GeneralRe: Give name to table from stored procedure Pin
Guillermo Rivero16-Jan-04 12:51
Guillermo Rivero16-Jan-04 12:51 

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.