50% OFF!!!

Sunday, December 7, 2008

Windows Mobile | Get current displayed window


Find which window is running right now.
Used for determining if OUR window is on background or not.



[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();




Thursday, November 13, 2008

Netbeans 6.0 - working with VSS



Netbeans 6.0 - working with vss (Microsoft Visual SourceSafe).

steps:

1. first, we have to download VSS plugin:

a. Go to Tools -> Plugins -> Setting -> Add

b. name = VSS, url = http://updates.netbeans.org/netbeans/updates/6.0/uc/final/vcsgenerics/catalog.xml.gz

c. this will install vss plugin (netbeans restart may be needed...) (will create Versing tab on Menu bar)

2. Create new project (as desired) inside netbeans. (I will demonstrate -getlatest- for new project)

3. Go to Versioning -> Versioning Manager -> Add.

a. Working directory: name of the new project created (on netbeans)

b. VSS user name: user with privileges for using VSS DB.

c. VSS command: usually: C:\Program Files\Microsoft Visual Studio\VSS\win32\SS.EXE

(sometimes this file is incorrect, try to copy it from VSS folder)

d. VSS project: Name of the project. like: $/projectName

e. VSS Database: the url, like: file://googledb/d$/VSS_FOR_PROJECT/

f. mark -get latest version- checkbox for getting files from server.

g. click on FINISH.

Get latest version will start working and the files will move to the project.

Hope it usefull...



Tuesday, October 7, 2008

AutoCompleteExtender | Run webservice on server side using Reflection



I use this code for running AutoCompleteExtender webservice.
I need to execute on ServerSide for finding the context-key of the selected value.
THis assumes the WebService code include in your project App_Code.

The Web-Service:


[WebMethod]
public string[] GetList(string prefixText, int count)
{
List items = new List();

items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("a","a1"));
items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("b","b1"));
items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("c","c1"));

return items.ToArray();
}



The AutoCompleteExtender on page:


< AjaxControlToolkit:AutoCompleteExtender
runat="server"
ID="AutoCompleteExtender_MAIN"
TargetControlID="txtMAIN"
BehaviorID="behavior_MAIN"
ServicePath="WebService.asmx"
ServiceMethod="GetList"
MinimumPrefixLength="2"
DelimiterCharacters=";,:" >




The code for running the web-service method: (fro finding cotext key!)


object objWs = Activator.CreateInstance("App_Code", AutoCompleteExtender_MAIN.ServicePath.Replace(".asmx", "")).Unwrap();
Type wsType = objWs.GetType();
MethodBase mb = wsType.GetMethod(AutoCompleteExtender_MAIN.ServiceMethod);

string[] results = mb.Invoke(objWs, new object[] { searchValue, 1 }) as string[];
if (results != null && results.Length > 0)
{
string[] splitedText = results[0].Split(',', ':', '{', '}', '"');

// place 5:text, place 11:code
string code= splitedText[11];

// set found code to hidden field & other AutoCompleteExtenders context-keys
hidden_code.Value = code;
AutoCompleteExtender_aaa.ContextKey = code;
AutoCompleteExtender_bbb.ContextKey = code;
}




Monday, September 8, 2008

Single instance of Windows Form application (WinCE)


How to create a single instance application on Win CE?
-----------------------------------------------------------
this example run a single instance of application on WinCE operation systems (devices).

On WindowMobile 6 (Pocket PC) operation systems,
by default, only one instance of the app will run!


here a sample code for Unique application running (starting):



static class Program
{
///
/// The main entry point for the application.
///

[MTAThread]
static void Main()
{
IntPtr eventHandle = IntPtr.Zero;
try
{
String appName = "UniqueAppName_Event";
eventHandle = CreateEvent(IntPtr.Zero, true, false, appName );
bool isFirstInstance = Marshal.GetLastWin32Error() == 0;

if (isFirstInstance == true)
{
Application.Run(new Form1());
}
}
finally
{
if (eventHandle != IntPtr.Zero)
{
CloseHandle(eventHandle);
}
}
}

#region Imports
[DllImport("Coredll.dll", SetLastError = true)]
static extern IntPtr CreateEvent(IntPtr alwaysZero, bool manualReset, bool initialState, string name);

[DllImport("Coredll.dll", SetLastError = true)]
static extern int CloseHandle(IntPtr handle);
#endregion
}



this ensure a single instance of Windows Form application...

Tuesday, September 2, 2008

Compact Framework | Scrolling on control



Scrolling inside a control from the code is very simple.
All you have to do is to give the control the new location (Point: x,y)
and it will scroll to the new point...

My generic method scrolls the item each time by ~66% of its size (2/3)
Note: in my example, the scrolling obect is this!



int changeHeight = this.Height * 2 / 3;
if (p_pressedKey == Keys.Down)
{
this.AutoScrollPosition = new Point(this.AutoScrollPosition.X, -this.AutoScrollPosition.Y + changeHeight);
}
else if (p_pressedKey == Keys.Up)
{
this.AutoScrollPosition = new Point(this.AutoScrollPosition.X, -this.AutoScrollPosition.Y - changeHeight);
}





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