21 May 2015

Battery drainage issue on Android

WiFi is always on:

22 October 2010

Problem in KeyBinding constructor in WPF

Basically Implementation#01 and Implementation#02 should be same. But Implementation sometimes gets exception if modifiers = ModifierKeys.None.


//Implementation # 01:
KeyBinding k = new KeyBinding(command, key, modifiers);

//Implementation # 02:
KeyBinding keyBinding = new KeyBinding();
keyBinding.Command = command;
keyBinding.Key = key;
keyBinding.Modifiers = modifiers;


So, use the 2nd way of implementation if required.

07 July 2010

Code : Strip HTML (Remove HTML Tags)

string StripHTML(string htmlString)
{

    //This pattern Matches everything found inside html tags;
    //(.|\n) - > Look for any character or a new line
    // *?  -> 0 or more occurences, and make a non-greedy search meaning
    //That the match will stop at the 1st available '>' it sees, and not at the last one
    //(if it stopped at the last one we could have overlooked
    //nested HTML tags inside a bigger HTML tag..)
   
    string pattern = @"<(.|\n)*?>";
    return Regex.Replace(htmlString, pattern, string.Empty);
   
}

Beginner's Unit Testing in C# using NUnit

C# .NET 2.0 Test Driven Development


The above website is helpful for a beginner though some steps could have been clearer.

Code : Download a web page

/// Returns the content of a given web adress as string.
///
///
URL of the webpage
/// Website content
public static string DownloadWebPage(string Url)
{
    // Open a connection
    HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);

    // You can also specify additional header values like
    // the user agent or the referer:
    WebRequestObject.UserAgent = ".NET Framework/2.0";
    WebRequestObject.Referer = "http://www.example.com/";

    // Request response:
    WebResponse Response = WebRequestObject.GetResponse();

    // Open data stream:
    Stream WebStream = Response.GetResponseStream();

    // Create reader object:
    StreamReader Reader = new StreamReader(WebStream);

    // Read the entire stream content:
    string PageContent = Reader.ReadToEnd();

    // Cleanup
    Reader.Close();
    WebStream.Close();
    Response.Close();

    return PageContent;
}

26 May 2010

CSS for printing


I'm feeling excited knowing that we can have different css styles for normal web-view and for printing. Say, in your website there is an image which you don't want to be available for printing. You can do so simply adding some css style (display:none) for printing (media="print"). Here is the example

<html>
<head>
<style type="text/css" media="print">
.noPrint{
display: none;
}
</style>
</head>

<body>
Here is the image: <img class="noPrint" src="myPicture.jpg" />
</body>
</html>



Inline css is like below

@media print {
.noPrint{
display:none;
}
}

02 May 2010

How to integrate SVN with Visual Studio using AnkhSVN

Download




Enable AnkhSVN for source control

  • In Visual Studio, select Tools > Options.
  • In the Options window, select "Plug-in Selection" under "Source Control".
  • Select "AnkhSVN - Subversion Support for Visual Studio" for "Current source control plug-in".



Connect a project to AnkhSVN

  • Open the project in Solution Explorer.
  • From the File menu in Visual Studio, select Subversion > Change Source Control .
  • In the Change Source Control window, select the row containing your project or solution, and click Connect.
  • Click Ok



Add a solution to a repository

  • Open your solution in the Solution Explorer
  • Right-click your solution and select "Add Solution to Subversion". To add specific projects rather than the containing solution, you can select "Add Selected Projects to Subversion" instead. The "Add to Subversion" dialog appears.
  • Specify the URL to your Subversion repository.
  • To add the solution to a new subdirectory, select the "Create Folder" option, and enter a directory name and log message.
  • Select File > Subversion > Pending Changes. select the files you want to commit, write log message and click on Commit.



For more info: http://help.collab.net/index.jsp?topic=/com.collabnet.doc.anksvn_001/action/ankh_getting_started.html

15 March 2010

Google Groups Bug: Set a banned member to manager

I have found a critical bug in Google Groups!

In google groups, if you set a banned member's membership to manager, then it seems that that user becomes a non-member manager. Then it becomes impossible to change his/her membership type. You cannot even delete / invite that person again. It's a bug and it's from Google!!

There is a remedy though. You have to use firebug.

  • In management task, select 'manage members'. Navigate to the user. You'll find that the checkbox beside that user is disabled.
  • Using firebug, find the html of that checkbox and remove the disabled attribute [disabled=""]
  • Then check the checkbox.
  • Set membership to "regular member" and click corresponding ok.

06 March 2010

How to extract rar file in ubuntu?

sudo apt-get install unrar

26 December 2009

Problem in adjusting brightness in HP laptop using ubuntu

I have a HP laptop and I use ubuntu 9.04. I was facing problem to adjust the brightness of the screen through fn+F7 or fn+F8 keys. I found the solution at http://ubuntuforums.org/showthread.php?t=673946. It should be noted here that it worked after restarting my laptop.

21 December 2009

Problem with audio callback at skype in ubuntu

Solution:

  • Go to "Options" (Ctrl+O)
  • Click on "Sound Devices"
  • Select "pulse" for both "Sound Out" and "Ringing"
  • For "Sound In", you have to choose your appropriate option. Change the value for "Sound In" and test the sound by clicking on "Make a test call" and following the directions.
More info at https://help.ubuntu.com/community/Skype

20 December 2009

How to shrink image size in ubuntu?

* Use imagemagick (sudo apt-get install imagemagick)

* If it is just one directory worth of images use

mogrify -resize 1024x1024 *.jpg

or

mogrify -resize 25% *.JPG

For more information, http://ubuntuforums.org/archive/index.php/t-518662.html

08 October 2009

C# : Dynamically adding control with docking works in reverse way

To create the list of labels (like the following image) dynamically, firstly I wrote the code below the image (very much expected).

for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text = "Label" + (i + 1);
l.Dock = DockStyle.Top;
panel1.Controls.Add(l);
}


Surprisingly, I discovered that it is working just in reverse way of my expectation! (like below)


The fact is the first control is being drawn at the bottom. So, I solved it with the help of Controls.SetChildIndex() function in the following way.

Solution:

for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text =
"Label" + (i + 1);
l.Dock =
DockStyle.Top;
panel1.Controls.Add(l);
panel1.Controls.SetChildIndex(l, 0);
}

Improved Solution:

In the given solution, what actually happens is that the new control is added at the top (for the style Dock.Top), then it is moved to the bottom. But this change in position may cause unwanted blinking of UI. To avoid this unwanted situation, follow the following steps

  • Add the new control with size (0,0)
  • Change its index to 0 (upper solution)
  • Resize again the control to actual size
Code:

for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text = "Label" + (i + 1);
l.Dock = DockStyle.Top;
addControlInContainer(panel1, l);
}


public static void addControlInContainer(Control container, Control control)
{
// save actual size
Size tempSize = control.Size;

// Add the new control with size (0,0)
control.Size = new Size(0, 0);
container.Controls.Add(control);

// Change its index to 0
container.Controls.SetChildIndex(control, 0);

// Resize the control to actual size
control.Size = tempSize;
}



31 August 2009

Flash for beginners

Few days back, I started learning Flash for game developing. At the very beginning I was not quite sure where to start. After traversing some way, now I can show the beginners a way.

Just install Adobe Flash CS3. And follow the tutorials below.

  1. Beginning Game Programming with Flash (Lakshmi Prayaga & Hamsa Suri) : If you are a stranger to CS3, if you cannot even draw object in CS3, then you'll find it useful.

  2. ActionScript 3.0 Game Programming University (Gary Rosenzweig) : It is a very nice tutorial, specially for programmers.

25 August 2009

Big Integer Arithmetics Library in JavaScript

Few days ago, I needed arithmetic functionalities of big integers (alternatively strings) in JavaScript. I was confident that it would get plenty of such libraries by googling, I might be in sweet trouble of choosing one. But unfortunately I did not find satisfactory library with at least 4 functionalities - add, subtract, multiply & divide. I was very surprised. May be my searching keywords were poor. Whatever, I have developed a small library with 6 basic functions (add, subtract, multiply, divide, remainder, isALessThanB) of non-negative big integer.

Big Integer Arithmetic Functions
  1. BigInt.add(n1, n2) returns n1+n2
  2. BigInt.subtract(n1, n2) returns n1-n2 [n1 must be greater than n2]
  3. BigInt.multiply(n1, n2) returns n1xn2
  4. BigInt.dividefunction(n1, n2) returns n1/n2 (integer part only)
  5. BigInt.remainder(n1,n2) returns n1%n2
  6. BigInt.isALessThanB(a,b) returns a <>

Big Integer Arithmetic Library

var BigInt =

{

add : function(n1, n2)

{

if(!this.isValidBigInt(n1) || !this.isValidBigInt(n2))

throw "Not a big integer.";

// make all input to string

n1 = n1 + "";

n2 = n2 + "";

var i;

var sum = "";

// reverse them for comfortable indexing

n1 = this.reverse( this.removeLeading0s(n1));

n2 = this.reverse(this.removeLeading0s(n2));

// make both of same length

var large = n1;

var small = n2;

if(n1.length<>

{

large = n2;

small = n1;

}

// pad with 0's

for(i=small.length; i

small += "0";

// start adding

var carry = 0;

for(i=0; i

{

var subSum = this.digitAt(small, i) + this.digitAt(large, i) + carry;

if(subSum <>

{

sum += (subSum + "");

carry = 0;

}

else

{

sum += (subSum - 10) + "";

carry = 1;

}

}

if(carry == 1)

sum += "1";

return this.removeLeading0s(this.reverse(sum));

},

subtract : function (n1, n2)

{

// n1: larger number

// n2: smalelr number

// returns n1 - n2

if(!this.isValidBigInt(n1) || !this.isValidBigInt(n2))

throw "Not a big integer.";

// make all input to string

n1 = n1 + "";

n2 = n2 + "";

var i;

var large = n1;

var small = n2;

large = this.reverse(large);

small = this.reverse(small);

// pad with 0's

for(i=small.length; i

small += "0";

var carry = 0;

var result = "";

for(i=0; i

{

var upDigit = this.digitAt(large,i);

var downDigit = this.digitAt(small, i);

var diff = upDigit - downDigit - carry;

if(diff >= 0)

{

result += (diff + "");

carry = 0;

}

else

{

result += (diff + 10 + "");

carry = 1;

}

}

return this.removeLeading0s( this.reverse(result) );

},

multiply : function(n1, n2)

{

// returns n1 * n2

// make all input to string

n1 = n1 + "";

n2 = n2 + "";

if(!this.isValidBigInt(n1) || !this.isValidBigInt(n2))

throw "Not a big integer.";

var mul = "";

var extra0s = "";

for(var i=n2.length-1; i>=0; i--)

{

var d = this.digitAt(n2,i);

var subMul = this.multiplyByOneDigit(n1, d);

if(i==n2.length-1)

{

mul = subMul;

}

else

{

extra0s += "0";

subMul += extra0s;

mul = this.add(mul, subMul);

}

}

return this.removeLeading0s(mul);

},

divide : function(n1, n2)

{

// returns Math.floor(n1/n2)

// make all input to string

n1 = n1 + "";

n2 = n2 + "";

if(!this.isValidBigInt(n1) || !this.isValidBigInt(n2))

throw "Not a big integer.";

n1 = this.removeLeading0s(n1);

n2 = this.removeLeading0s(n2);

var i;

var divisor = n2;

var result = "";

var rem = "";

for(i=0; i

{

rem += (n1[i]+"");

var iRem = rem;

if(this.isALessThanB(iRem, divisor))

{

result += "0";

}

else

{

var subDiv = this.shortDivide(iRem, divisor) + "";

result += (subDiv + "");

rem = this.subtract(iRem, this.multiply(subDiv, divisor));

}

}

return this.removeLeading0s(result);

},

isALessThanB : function (a,b)

{

if(!this.isValidBigInt(a) || !this.isValidBigInt(b))

throw "Not a big integer.";

// make all input to string

a = a + "";

b = b + "";

a = this.removeLeading0s(a);

b = this.removeLeading0s(b);

if(a.length != b.length)

return a.length <>

for(var i=0; i

{

var dA = this.digitAt(a,i);

var dB = this.digitAt(b,i);

if(dA != dB)

return dA <>

}

return false;

},

remainder : function(n1,n2)

{

// returns n1 % n2

// make all input to string

n1 = n1 + "";

n2 = n2 + "";

var d = this.divide(n1,n2);

return this.subtract(n1, this.multiply(d,n2));

},

shortDivide : function(a, b)

{

// a is less than 10b

var i=0;

while(!this.isALessThanB(a,b))

{

a = this.subtract(a,b);

i++;

}

return i;

},

multiplyByOneDigit : function (n, d)

{

// n: string

// d: number (0-9)

var n = this.reverse(n);

var carry = 0;

var result = "";

for(var i=0; i

{

var m = this.digitAt(n, i) * d + carry;

result += (m%10 + "");

carry = Math.floor(m/10);

}

if(carry > 0)

result += (carry + "");

return this.reverse(result);

},

reverse : function(s)

{

var iLen = s.length;

var strRev = "";

for(var i=iLen-1; i>=0; i--)

strRev += s.charAt(i);

return strRev;

},

digitAt : function(s, i)

{

var c = s[i];

return parseInt(c);

},

removeLeading0s : function(n)

{

n = n + "";

var result = "";

var i=0;

while(n[i] == '0')

i++;

if(i

{

result = n.substr(i);

}

if(result == "")

result = "0";

return result;

},

isValidBigInt : function(n)

{

if(typeof n != "string" && typeof n != "number")

return false;

// make it string (from both number & string)

n = n+"";

if(n.length == 0)

return false;

// check all digits

for(var i=0; i

if(n[i] < '0' || n[i] > '9')

return false;

return true;

}

};



Sample code of using this library

var num1 = "26093683160935360286936021";

var num2 = "4864306873315646901285";

var resAdd = BigInt.add(num1, num2);

var resSub = BigInt.subtract(num1, num2);

var resMul = BigInt.multiply(num1, num2);

var resDiv = BigInt.divide(num1, num2);

var resRem = BigInt.remainder(num1, num2);

var resLess = BigInt.isALessThanB(num1, num2);


 

© 2007 t!ps n tr!cks



Template unik dari rohman


---[[ Skip to top ]]---