Friday, March 30, 2012

Scroll page with javascript

Sometimes it's useful to scroll the page automatically to send users to certain parts of the page. To do that the easiest way is using javascript, more specifically jquery that is a javascript framework.

Test it here:

Scroll down!
 
Example code:

<html>
<head>
<SCRIPT LANGUAGE="JavaScript" 
SRC="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function scroll_up()
{
    $(window).scrollTop(0);
}
function scroll_to_last_div()
{
    $(window).scrollTop($('#last_div').offset().top);
}
</SCRIPT>
</head>
<body>
<center>
<div style="height:100px;width:400px;">
<br><br>
<a href="javascript:scroll_to_last_div();">Scroll to last div!</a>
</div>
<div style="height:600px;width:400px;background:#234a34;">
</div>
<div style="height:100px;width:400px;" id="last_div">
<br><br>
<a href="javascript:scroll_up();">Scroll to top!</a>
</div>
<div style="height:600px;width:400px;background:#fa342a;">
</div>
</center>
</body>
</html> 

Monday, March 19, 2012

Insert one or multiple rows in mysql db using php

Normally, one of the first thing that you need to do after connect to a mysql database is insert data. This tutorial is about inserting a single row or multiple rows into a mysql database using php. If you don't know how to connect to a database check our previous tutorial "Connect to a mysql database using php".

Let's imagine for this example that my table name is "people" and inside this table i have 3 columns "id", "first_name" and " last_name".

This way to insert a single row with the person "Chuck Norris" i need to execute the following query:

$validation = mysql_query("INSERT INTO platform_types 
                         (id, first_name, last_name) 
                         VALUES ('1', 'Chuck', 'Norris')"
                         ,$db_con);

if (!$validation) //test query for errors 
{
    echo("Error in query: " . mysql_errno($apbl_db_con) . ": " 
    . mysql_error($apbl_db_con) . '
');             
}
//Closing connection (don't forget to close the connection 
//when you don't need to query stuff to the db)
apbl_db_close($apbl_db_con); 

The $db_con variable is the one returned by mysql_connect when you connect to the database. If you need to insert multiple rows at same time the procedure is the same but you add more values at the end like this:

$validation = mysql_query("INSERT INTO platform_types 
    (id, first_name, last_name) VALUES 
    ('1', 'Chuck', 'Norris'), 
    ('2', 'Jackie', 'Chan')",$db_con);

//...the rest of the code it's the same.
 Notice that if your "id" column is set to auto-increment (usually is) you don't need to give a value to the "id" in the query because it will auto-increment by him self. Then your query will look like this:

$validation = mysql_query("INSERT INTO platform_types 
    (first_name, last_name) VALUES 
    ('Chuck', 'Norris'), 
    ('Jackie', 'Chan')",$db_con);

//...the rest of the code it's the same.

Thursday, March 15, 2012

Connect to a mysql database using php

In almost all web projects now a days you need to use at least one database to store information. So let's start using a database! First step is to connect to the database.
For this example let's imagine that a i have mysql running on the adress "localhost" with the username "root" and password "password". The database name is "mydatabase".

With this information we can start coding our php script to connect with the database.


I hope it has been helpful, in the next tutorial let's see how to insert data on your  mysql database using php.

        
//Try to connect to database
$con = mysql_connect('localhost','root','password'); 
      
if (!$con) //Test if connection was successfully
{
    echo ('Error on connect: ' . mysql_errno() . ": " 
    . mysql_error());
}
else
{
    //Connect to the table inside the database
    $db_check = mysql_select_db('mydatabase', $con); 
    if (!$db_check)
    {
        echo ('Error on the database : ' . mysql_errno() 
        . ": " . mysql_error());
    }
}

Next tutorial:  "Insert one or multiple rows in mysql db using php".

Sunday, March 11, 2012

ShutItDown RBS Review

Following my last post about licensing free and open source software, today i will review on of my applications that i just released for free under the GNU GPL v3 license. Both the application and the code written in Visual Basic are available to download.

ShutItDown RBS


ShutItDown RBS is a software that allows the user to schedule task like shut down, restart or log off on his machine. You can specify date and time and even specify if you want them to repeat every day or every week.

Features:
  • Quickly shut down, restart or log off your pc.
  • Schedule shut down, restart or log off tasks in your pc.
  • Able to run minimized and start automatically with windows.
  • You can schedule daily or weekly tasks and they will be performed automatically until you disable them.
  • Automatically generated log of ShutItDown activities so that you can check if certain tasks were performed.


shutitdown free and open source


User guide video:






This software was already approved in several websites like softpedia.com, freewarefiles.com, majorgeeks.com and soft82.com.

So if you are into programming and sometimes you develop some applications, why not share them with world?


Links:
ShutItDown RBS website 
ShutItDown RBS sourceforge project

Tuesday, March 6, 2012

How to license your free software with GNU GPL v3

As you all know a software license is something that tells the users what they can or cannot do with that especific software and it's also a way of giving some credit to the author for his software.

There are several types of licenses:

- Perpetual license
- Subscription license
- Freeware license
- Shareware license
- OEM (original equipment manufacturer)
- Educational or Academic Software
- Not for Resale (NFR) Software License
- And more...

This post is specifically about Freeware Licences and specially about Open Source because, as you already know, inside free software applications we have some that are open source and others that are closed source.

So let's see a little bit more about free and open source software (quoting Wikipedia.org):
A primary consequence of the free software form of licensing is that acceptance of the license is essentially optional — the end-user may use, study, and privately modify the software without accepting the license. However, if the user wishes to exercise the right of redistributing the software, then the end-user must accept, and be bound by, the software license.
Free and open-source licenses generally fall under two categories: Those with the aim to have minimal requirements about how the software can be redistributed (permissive licenses), and those that aim to preserve the freedoms that is given to the users by ensuring that all subsequent users recives those rights (copyleft Licenses).
An example of a copyleft free software license is the GNU General Public License (GPL). This license is aimed at giving all user unlimited freedom to use, study, and privately modify the software, and if the user adheres to the terms and conditions of GPL, freedom to redistribute the software or any modifications to it. For instance, any modifications made and redistributed by the end-user must include the source code for these, and the license of any derivative work must not put any additional restrictions beyond what GPL allows.[1]
Examples of permissive free software licenses are the BSD license and the MIT license, which give unlimited permission to use, study, and privately modify the software, and includes only minimal requirements on redistribution. This gives a user the permission to take the code and use it as part of closed-source software or software released under a proprietary software license.

free and open source license GPL


One of the most known and used license for this type of software is the GNU GPL(General Public License) that is now on version 3. The bases of this license, quoting gnu.org, are:

The Foundations of the GPL

Nobody should be restricted by the software they use. There are four freedoms that every user should have:
  • the freedom to use the software for any purpose,
  • the freedom to change the software to suit your needs,
  • the freedom to share the software with your friends and neighbors, and
  • the freedom to share the changes you make.
When a program offers users all of these freedoms, we call it free software.
Developers who write software can release it under the terms of the GNU GPL. When they do, it will be free software and stay free software, no matter who changes or distributes the program. We call this copyleft: the software is copyrighted, but instead of using those rights to restrict users like proprietary software does, we use them to ensure that every user has freedom.
So if you developed a software and you are thinking about distributing it as a free software, i created a little video explaning what you need to do to license your software with GNU GPL v3. I am not an expert on this but i have already done this and i am just trying to share my experience.

Video turorial:


Sunday, February 26, 2012

Read a file line by line to the end in VB.NET

This tutorial it's about reading a file line by line until the end in VB.NET. This is usefull, for instance, if you are using this file as a settings file and you inserted one setting per line then you can read them by order.

Visual Studio screenshot:


Code:


Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim file_dir As String = "file.txt"
        'parse file
        Try
            Dim sr As StreamReader = New StreamReader(file_dir)
            Do Until sr.EndOfStream
                TextBox1.Text += vbNewLine + sr.ReadLine()
            Loop
            sr.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

In this case you are reading and adding each line to a textbox but you can read this to a string array and keep the information to work with later. It's also important to use try catch because if the file doesn't exist or you don't have permissions to read it will not crash the application.

Monday, February 20, 2012

VB.NET Write data to XML using a DataSet


In this tutorial i will talk about getting the name of all the files (in this case images) in a directory and store them into a XML file using a DataSet in VB.NET.


Code:
Imports System.IO

Public Class Form1
    Dim images_dir As String = "C:\myfolder"
    Dim xml_dir As String = "C:\myfolder"
    Dim image_list As String()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataSet_images.Tables("Table1").Clear()
        image_list = Directory.GetFiles(images_dir)
        Dim filename As String
        Dim counter As Integer
        For Each filename In image_list
            counter += 1
            DataSet_images.Tables("Table1").Rows.Add(New Object() {counter, filename})
        Next filename
        DataSet_images.Tables("Table1").WriteXml(xml_dir + "\images.xml", XmlWriteMode.IgnoreSchema)
    End Sub
End Class
Video Tutorial:





Sunday, February 12, 2012

VB.NET Limit textbox minimum and maximum characters

Let's imagine that you hava a textbox for a user to input something and you want that to be between a minimum and a maximum number of characters, how you do it on VB.NET? Let's see!

Code:

Maximum size limitation can be done in the textbox properties in the "MaxLenght" option.

textbox vb.net
To minimum size limitation it's only necessary to check when user clicks the button to send that information.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        check_text()
    End Sub

    Private Sub check_text()
        If TextBox1.Text.Length = 3 Then
            MessageBox.Show("Ok")
        Else
            MessageBox.Show("You need to insert 3 numbers")
        End If
    End Sub


Video tutorial:


Monday, February 6, 2012

Textbox for numbers only VB.NET

Sometimes we need to limit our user inputs to only numbers for instance in a quantity input and for that i leave here a tutorial on how to do that in VB.NET.

Code:

Public Class Form1    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8 Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub



End Class


Video tutorial:


Friday, January 27, 2012

Checking if a directory exists and creating one in VB.NET

This tutorial contains you the necessary code to check if a directory exists and to create one if it returns false using VB.NET.

Code:

Imports System.IO


If Directory.Exists("c:\mydir") Then
        MessageBox.Show("Directory exists!")
Else
        Directory.CreateDirectory("c:\mydir")

        MessageBox.Show("Directory created!")
End If

Thursday, January 26, 2012

Minimize a VB.NET application to system tray using NotifyIcon

This tutorial is about using the NotifyIcon in VB.NET to minimize your application o the system tray that can be very usefull in applications that run as a server in background for instance.

Form screenshot:

VB.NET Form

Code:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon1.Visible = True
                NotifyIcon1.ShowBalloonTip(1, "NotifyIcon", "Running Minimized", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon1.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon1.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class


Video Tutorial:

Saturday, January 21, 2012

For Loop VB.NET / C#

This time i leave another very usefull loop, the "For Loop", in both VB.NET and C#.
Enjoy.


C#:
  for (int i = 0; i <= 5; i++)
  {
     //Do your code here
  }


VB.NET:
  Dim i As Integer
  For i = 0 To 5
      'Do your code here
  Next i
 

Monday, January 16, 2012

Switch Case / Select Case VB.NET / C#

I needed to remember the syntax of the switch / case for the .net languages and then i decided to share them with you here too so you can learn or remember them too.

C#:
  switch (option)
    {
        case "option1":
            // code for case "option1"
            break;
        case "option2":
            // code for case "option2"
            break;
        default:
            //code for every other options
            break;
    }

VB.NET:
Select Case option
    Case "option1"
          ' code for case "option1"
    Case "option2"
          ' code for case "option2"
    Case Else 
          ' code for every other options
End Select