50% OFF!!!

Monday, August 25, 2008

Picture with transparent label


A picturebox object that supports a label (transparent).
this allow to create a piture button and add text it.
This control tested and working :)

use it with wisdom... or not:
have fun:



public class PictureText : PictureBox, ISupportInitialize
{
#region #region Private Data-Members

private string _textDisplayed = string.Empty;

private Font _textFont = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold);

private Color _textForeColor = Color.Black;

#endregion

public string TextDisplayed
{
get { return _textDisplayed; }
set
{
_textDisplayed = value;
this.Invalidate();
}
}
public Font TextFont
{
get { return _textFont; }
set
{
_textFont = value;
this.Invalidate();
}
}
public Color TextForeColor
{
get { return _textForeColor; }
set
{
_textForeColor = value;
this.Invalidate();
}
}

//protected override void OnPaintBackground(PaintEventArgs e)
//{
// base.OnPaintBackground(e);

// //HorizontalAlignment.
// if (string.IsNullOrEmpty(TextDisplayed) == false)
// {
// e.Graphics.DrawString(this.TextDisplayed, this.TextFont, new SolidBrush(this.TextForeColor), this.Location.X, this.Location.Y);
// }
//}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

if (string.IsNullOrEmpty(TextDisplayed) == false)
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(this.TextDisplayed, this.TextFont, new SolidBrush(this.TextForeColor), this.Width / 2, this.Height / 2, sf);
}
}
}

#region ISupportInitialize Members
public void BeginInit()
{
}
public void EndInit()
{
}
#endregion
}




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





Remote Data Access (RDA) example


I build a RDA example and it is very usefull...
[Remote Data Access Synchronization with SQL Server 2005]


Pull operation sample:


private void btnRDAPull_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;

// upgrade for using SQL CE 3.5
//try
//{
// SqlCeEngine sqe = new SqlCeEngine(CONNECTIONSTRING_MOBILE);
// sqe.Upgrade();
//}
//catch { }

try
{
// Initialize the RDA object.
using (SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess())
{
//rda.InternetLogin = "XXXXXXXXXX";
//rda.InternetPassword = "XXXXXXXXXXX";

rda.InternetUrl = "http://XXXXXXXXXXXX/samimobiletest/sqlcesa35.dll";

//rda.LocalConnectionString = "Data Source=\\DB.sdf;Password =XXXXX;";
rda.LocalConnectionString = CONNECTIONSTRING_MOBILE;
rda.Pull(
"Users",
//"Select UserId,UserName,Password from Users",
"Select * from Users",
CONNECTIONSTRING_SERVER,
RdaTrackOption.TrackingOnWithIndexes);
}



MessageBox.Show("Pull OK :)");


}
catch (SqlCeException scEx)
{
//Use your own error handling routine.
MessageBox.Show(scEx.Message);
//ShowErrors(e)
}
finally
{
Cursor.Current = Cursors.Default;
}
}



Push operation sample:


private void btnRDAPush_Click(object sender, EventArgs e)
{
try
{


// Initialize the RDA object.
using (SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess())
{
//rda.InternetLogin = "XXXXX";
//rda.InternetPassword = "XXXXXXXX";

rda.InternetUrl = "http://XXXXXXXXXX/samimobiletest/sqlcesa35.dll";


rda.LocalConnectionString = CONNECTIONSTRING_MOBILE;
rda.Push("Users", CONNECTIONSTRING_SERVER, RdaBatchOption.BatchingOff);
}

// show message
MessageBox.Show("Push OK :)");
}
catch (SqlCeException scEx)
{
// Use your own error handling routine.
MessageBox.Show(scEx.Message);
}
}


here is a link for download:
Download full example

Tuesday, August 19, 2008

Validation Israel ID



try
{
string txtId = p_text.PadLeft(9, '0');

int sum = 0;
int[] arrIdDigits = new int[9];
for (int i = 0; i < arrIdDigits.Length; i++)
{
arrIdDigits[i] = int.Parse(p_text[i].ToString());

if (i % 2 != 0)
{
arrIdDigits[i] *= 2;
if (arrIdDigits[i] > 9)
{
arrIdDigits[i] = (arrIdDigits[i] % 10) + 1;
}
}

sum += arrIdDigits[i];
}

sum = (sum % 10);
if (sum == 0)
{
return true;
}
}
catch (Exception ex)
{
}

return false;

Making control RTL or LTR

As many people asked me for making rtl or ltr controls on the compactframework.
My best solution for this question is:

public const int GWL_EXSTYLE = (-20);
public const int WS_EX_LAYOUTRTL = 0x400000;
public static void SetControlDirection(Control c, bool p_isRTL)
{
int style = GetWindowLong(c.Handle, GWL_EXSTYLE);

// set default to ltr (clear rtl bit)
style &= ~WS_EX_LAYOUTRTL;

if (p_isRTL == true)
{
// rtl
style = WS_EX_LAYOUTRTL;
}

SetWindowLong(c.Handle, GWL_EXSTYLE, style);
c.Invalidate();
}


[DllImport("coredll.dll")]
static extern int GetWindowLong(IntPtr hWnd, int cmd);

[DllImport("coredll.dll")]
static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);





CE OS supported:

I tried on "Windows Mobile 6 Classic" emulator and it worked. (CE OS = 5.2)

On "Windows Mobile 5.0 Pocket PC" emulator, also worked. (CE OS = 5.1)


On "Pocket PC 2003 SE" emulator, it did NOT worked! (CE OS = 4.21)


Free Image Hosting at www.ImageShack.us

Download sample test





I hope it will be helpful for you... :)

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...