50% OFF!!!

Showing posts with label Auto size. Show all posts
Showing posts with label Auto size. Show all posts

Tuesday, November 10, 2009

Asp.Net - Make short ids for controls

Here is a code for making your controls names/ids shorten than it is, because asp.net as default make every control name: {parent-id}_{control-id} and it makes html code length larger...


public class BasePage : Page
{
protected override void OnInit(EventArgs p_eventArgs)
{
MakeIdsShorted(this, true);
base.OnInit(p_eventArgs);
}

private int currentId = 0;
private void MakeIdsShorted(Control p_ctrl, bool p_recursive)
{
if (p_ctrl is Login)
{
return; // ignore ASP.Login controls
}
if (string.IsNullOrEmpty(p_ctrl.ID) == false)
{
//this.EnsureID();
p_ctrl.ID = "c" + currentId; // p_ctrl.ID.GetHashCode();
currentId++;
}

if (p_recursive == true)
{
foreach (Control c in p_ctrl.Controls)
{
MakeIdsShorted(c, true);
}
}
}
}



Make sure your created page is inherits from BasePage...
:)

Thursday, August 21, 2008

Auto size ListView [columns]


Good feature for auto sizing the listview control.
this autosizes the columns of the list.
the auto size can be done with 2 ways:

1. adjust the width of the longest item in the column, set the Width property to -1
2. autosize to the width of the column heading, set the Width property to -2


Here is example for autosizing by the larges of the 2 options:



public static void AutoSizeListView(ListView p_listView)
{
int width1, width2;
ColumnHeader colHeader;
ListView.ColumnHeaderCollection colHeaderCollection = p_listView.Columns;

int count = colHeaderCollection.Count;
for (int i = 0; i < count; i++)
{
colHeader = colHeaderCollection[i];

// To adjust the width of the longest item in the column, set the Width property to -1
colHeader.Width = -1;
width1 = colHeader.Width;

// To autosize to the width of the column heading, set the Width property to -2
colHeader.Width = -2;
width2 = colHeader.Width;

colHeader.Width = (width1 <= width2) ? width2 : width1;
}
}





Tuesday, August 12, 2008

Auto Sized Label for cf

Auto Sized Label control for the compact framework:

I noticed that there is no solution for an autosized label (auto width & height)

so i build my example for this control:




public class AutoSizeLabel : Label
{
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
ReCalculateSize();
}
}
public override System.Drawing.Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
ReCalculateSize();
}
}
private void ReCalculateSize()
{
using (Control control = new Control())
{
using (Graphics g = control.CreateGraphics())
{
SizeF size = g.MeasureString(base.Text, base.Font);
base.Width = (int)size.Width + 1;
base.Height = (int)size.Height + 1;
}
}
}
}


i hope it will help you...