jump to navigation

How to make a splash screen. December 8, 2005

Posted by Christian López Espínola in CSharp, Programming.
5 comments

When we execute some applications, we see a splash screen with the logo of the program on it.

The computer user thinks that if there’s no new window in his monitor, computer is not working.

By that, we should show something when the application is loading his data structures.

How can we make it? Using threads. In CSharp we have to include the System.Threading namespace.

At our Main function we have to declare a thread like follows:

Thread th= new Thread (new ThreadStart(Splash));

Splash is an static function we will see later.

Then we start thread’s execution, and we initialize our structures. After that, we kill the secondary thread:

th.Start();

//initialize structures…

th.Abort();

Finally we start our main window:

Application.Run(mainForm);

Our Splash function could be like follows:

public static void Splash()

{

SplashForm sf = new SplashForm();

sf.ShowDialog();

}

If our structures load fast, the splash screen will not can visible. We can make it visible with a thread sleep. The time that thread sleeps is taken in milliseconds:

Thread.Sleep(4000);

Now our applications will seem more professional. :D

Connecting to an Access DataBase with ADO.NET December 6, 2005

Posted by Christian López Espínola in CSharp, Programming.
4 comments

This is a simple guide for reading data from a database. I chose Access for the example, but using another Database Manager is very similar to this.

At first, we have to connect to the database, using its connection string, and open the connection:

OleDbConnection con;
con = new OleDbConnection();
con.ConnectionString = @�Provider=Microsoft.Jet.OLEDB.4.0;� +
@�Data source= C:\cd.mdb�;
con.Open();

Now we create the command. For this we use OleDbCommand. Its constructor receives as parameters one string with the SQL sentence and the connection object.

OleDbCommand cmd;
cmd = new OleDbCommand(“select * from Customers”, con);

For sentences that read data, we have to use the method ExecuteReader. Its result rows are saved in an object called DataReader.

OleDbDataReader dr;
dr = cmd.ExecuteReader();

Now we iterate over the reader and print data at console. For this we use a DataReader indexer, which parameter is the name of the column.

while (dr.Read())
{

Console.WriteLine(“Name: ” + dr["Name"].ToString();
Console.WriteLine(“Name: ” + dr["Phone"].ToString();
Console.WriteLine(“Name: ” + dr["Mail"].ToString();

}

Closing all opened connections is very important:

con.Close();

Writing good code is important. For writing good code, we should take care about exceptions methods can throw. In this little demo we don’t make it, but be careful in your applications.

Welcome December 6, 2005

Posted by Christian López Espínola in Uncategorized.
add a comment

Welcome to my new blog. It’s the first blog I write in english, so be comprehensive with me. I will try not to use dictionaries or other help, so if you find a mistake please comment it to me. In this blog I’m going to write some technical articles, code and things relationated to my work, my studies, and, why not, my passion.

Be like at home ;)