20.9.11

Android Recursively change fonts ?

I think it's time to start blogging about android :)

I just got my Samsung Galaxy S2 a week ago and it's been so fun playing with !

So today i'm gonna show how to use custom fonts, There is a nice artical about Styling TextViews in here: Customize Android Fonts

You can see there that they are accessing a TextView using it's ID and then changing the font of the spacific TextView.
TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font);  
Now that can be a pain in the a** if you have alot of TextViews in your page...
So iv'e created a Recursive that changes all TextView's & Button's fonts on the same page :)
void UpdateFonts(ViewGroup parent, Typeface font) {
  for (int i = 0; i < parent.getChildCount(); i++) {
   View child = parent.getChildAt(i);
   if (child instanceof ViewGroup) {
    UpdateFonts((ViewGroup) child, font);
   } else if (child != null) {
    if (child.getClass() == TextView.class) {
     ((TextView) child).setTypeface(font);
    } else if (child.getClass() == Button.class) {
     ((Button) child).setTypeface(font);
    }
   }
  }
 }
This is an eay recursive that loops through all children of a parent element and changes all of his children's fonts.
You can call it like that if you want all page:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/choco.ttf");
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content).getRootView();
UpdateFonts(rootView, font);
Hope you enjoyed my first Android article :)
See you next time ;)

2.9.11

Fliping lots of text lines... ?


Environment:

I'm using Windows 7 64bit with Notepad++ 5.9.3, Visual Studio 2010 Ultimate, C# .NET 2.0.


Problem:

So i face a really annoying problem today,
I had an array with 400 lines written in C#.
The problem is that, the list was reversed from the order i wanted.


Example:

The array i have:










The array i need:









Like i said it's a 400 lines array...

Research:


I really needed this reversed, i tried Notepad++ with several plugins like TextFX without any success, i actually found out that it's possible to do it by recording a Macro in the Notepad++, but it's like not my thing.

So i used my best friend! C# :)

I'll make a quick explanation of what i'm doing and then just paste the code.



Solution:

So i made a Console App that gets a file path from the application arguments.
It reads all the lines and pushs them into a Stack then writes it to a new file using Pop :)

Heres the code: (Just compile and it works)

class Program
{
 static void Main(string[] args)
 {
  if (args == null)
  {
   Console.WriteLine("args is null"); // Check for null array
  }
  else
  {
   if (args.Length == 0)
   {
    Console.WriteLine("Please enter a file path to reverse in the console arguments.");
   }
   else
   {
    string p = args[0];
    string o = Path.GetDirectoryName(p) + Path.PathSeparator + Path.GetFileNameWithoutExtension(p) + "Reversed" + Path.GetExtension(p);
    Console.WriteLine(o);
    try
    {
     String line;
     Stack< string > lines = new Stack< string >();
     // Create an instance of StreamReader to read from a file.
     // The using statement also closes the StreamReader.
     using (StreamReader sr = new StreamReader(p))
     {
      // Read and display lines from the file until the end of
      // the file is reached.
      while ((line = sr.ReadLine()) != null)
       lines.Push(line);
     }
     // create a writer and open the file
     // The using statement also closes the StreamWriter.
     using (TextWriter tw = new StreamWriter(o))
     {
      // write a line of text to the file
      while (lines.Count > 0)
       tw.WriteLine(lines.Pop());
     }
    }
    catch (Exception e)
    {
     // Let the user know what went wrong.
     Console.WriteLine("The file could not be read/written:");
     Console.WriteLine(e.Message);
    }
    Console.WriteLine("Reversed.");
   }
  }
  Console.ReadLine();
 }
}

28.8.11

Simple cross drawing :)

Hey, 

Here is a simple cross drawing
using System.Drawing;
//Create graphics object to handle drawings
Graphics gfx = picBox.CreateGraphics(); 
//Get client retangle from out PictureBox that we want the cross in
Rectangle rc = picBox.ClientRectangle; 
//Create a new pen to draw the cross
Pen RedPen = new Pen(Color.Red);
int Size = 10
//Draw the actual cross 
gfx.DrawLine(RedPen, rc.Width / 2, rc.Height / 2 + Size, rc.Width / 2, rc.Height / 2 - Size);
gfx.DrawLine(RedPen, rc.Width / 2 + Size, rc.Height / 2, rc.Width / 2 - Size, rc.Height / 2);

28.6.11

Run application on Windows Startup

Hey,

In this post i'm gonna show you how to run an application on Windows Startup by adding the application to the registry in the right place.

First of all add this using:
using Microsoft.Win32;

So we can accesss the Registry of the computer.
Define a RegistryKey:

// The path to the key where Windows looks for startup applications
RegistryKey rkApp=Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);

You can change the CurrentUser to LocalMachine if you want your application to run on startup for all users.

Create the registry key and close the registry key handler:

rkApp.SetValue("notepad", @"C:\WINDOWS\system32\notepad.exe"); // Make notepad run on startup                 
rkApp.Close(); //Close the RegistryKey handler
Now notepad will always run when you turn on your PC :P


Nice registry locations can be found here: Registry Autostart Locations

Have fun tweaking the registry :)

1.5.11

Danpe Presents: C# HTML Builder

Having trouble building HTML with C# ?

Have you ever used something like this to generate HTML tables for example:
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 32; i++)
{
    sb.AppendLine("<tr class=\"row\">");
    sb.AppendLine("<td>" + i + "</td>");
    sb.AppendLine("<td> </td>");
    sb.AppendLine("<td> </td>");
    sb.AppendLine("<td> </td>");
    sb.AppendLine("</tr>");
}
return sb.ToString();
(As you see even the SyntaxHighlighter couldn't handle all of those Escape Characters)

I'm pretty sure you did :)
And you probably you hate all of those Escape Characters! (/" " +)
And doing sb.AppendLine all the time and doing all this formating!
Imagine you have 50 lines?? OMG...

So anyway i made an Application to make you'r life easier :)


Simple as it looks :)
Download Here: C# HTML Builder

18.4.11

Increase SQL Value By 1?

Hey,

Fast solution for today.
Ever wanted to increase SQL value by 1 ?
Yes, a stupid way to do it is by using SELECT to read the value, and then use UPDATE to update to the new value. (Your prolly asked your self, why cant i VALUE++?!)

So you can do it in 1 request using UPDATE :)
Ever thought of Value = Value + 1

using (SqlConnection con = new SqlConnection(SQL.ConnectionString())) //Use your own Connection String
{
    con.Open();
    string sqlString = "UPDATE Table SET Value=Value+1 WHERE Condition=@Condition";
    SqlCommand cmd = new SqlCommand(sqlString, con); 
    cmd.Parameters.Add("@Condition", ConditionString); 
    rowAffected = cmd.ExecuteNonQuery(); 
    if (rowAffected > 0) 
        DoSomething(); //Success! 
    else 
        DoSomething(); //Fail!
}

As you see i'm using Parameterized Query that i will explain in a later post.
This code will increase 'Value' by 1 where 'Condition'='ConditionString'. 
connection will automaticly close becuase of the 'using' statement.

Hope it helps you,
Have fun and Happy Passover :P

11.4.11

SQL NOW() wont work in Stored Procedures

Hey everyone im back after long time.. :)

This time i'm gonna give you a little solution to a problem i faced few weeks ago.

Have you ever wondered why 'NOW()' wont work in MSSQL Stored Procedure?
Yes, i wondered too.

So the replacment to 'NOW()' is 'GETDATE()' :)
So just use 'GETDATE()' it will give you the same result as 'NOW()'

Hope this little solution helps you
And i'm back now to give you all more solutions :)