Friday, August 19, 2011

Friday 8.18.11

using System;
using System.Windows.Forms;

public class MainClass : System.Windows.Forms.Form
{
public MainClass()
{
Application.ApplicationExit += new EventHandler(Form_OnExit);
}
[STAThread]
static void Main()
{
Application.Run(new MainClass());
}

private void Form_OnExit(object sender, EventArgs evArgs)
{
Console.WriteLine("Exit");
}
}


using System;
using System.Threading;
using System.Windows.Forms;

public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
const int WM_LEFTBUTTONDOWN = 0x201;

public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LEFTBUTTONDOWN)
{
Exception LeftButtonDownException;

LeftButtonDownException = new Exception("The left mouse button was pressed.");
Application.OnThreadException(LeftButtonDownException);
return true;
}
return false;
}
}


public class ApplicationEventHandlerClass
{
public void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
Exception LeftButtonDownException;
LeftButtonDownException = e.Exception;
Console.WriteLine(LeftButtonDownException.Message);
}
}

public class MainForm : Form
{
public static void Main()
{
ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
MainForm myForm = new MainForm();
BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();

Application.AddMessageFilter(MsgFilter);
Application.ThreadException += new ThreadExceptionEventHandler(AppEvents.OnThreadException);
Application.Run(myForm);
}


public MainForm()
{
Text = "Application Exception Test";
}
}




Module Module1

Sub Main()
Console.Title = "Custom Command Window"
Console.BackgroundColor = ConsoleColor.White
Console.ForegroundColor = ConsoleColor.DarkBlue
Console.WindowHeight = Console.LargestWindowHeight - 15
Console.WindowWidth = Console.LargestWindowWidth - 15

Console.Clear()
Console.ReadLine()

End Sub

End Module



Module Module1

Sub Main(ByVal cmdArgs() As String)
Dim strArg As String

If cmdArgs.Length > 0 Then
For Each strArg In cmdArgs
Select Case strArg.Substring(0, 3)
Case "/a1"
Console.WriteLine("Processsing arg1: Value is {0}.", strArg.Substring(3, strArg.Length - 3))
Case "/a2"
Console.WriteLine("Processing arg1: Value is {0}.", strArg.Substring(3, strArg.Length - 3))
End Select
Next
End If

End Sub

End Module


Namespace MyApp.Info


Public Class Utilities
'Run time application
Public Sub DisplayData()
Console.WriteLine(Environment.MachineName)
Console.WriteLine(Environment.SystemDirectory)
Console.WriteLine(Environment.GetLogicalDrives())
Console.WriteLine(Environment.Version.ToString())
End Sub
End Class

Module Main

Sub Main()
Dim objHW As New MyApp.Info.Utilities
objHW.DisplayData()
End Sub

End Module

End Namespace


Sub Main(ByVal cmdArgs() As String)
If Environment.GetCommandLineArgs.Length > 0 Then
For Each strArg As String In Environment.GetCommandLineArgs
'process the arguments here
Console.WriteLine(strArg)
Next strArg
End If
End Sub


Public Class Tester
Public Shared Sub Main()
Console.WriteLine("Enter the numerator")
Dim a As Integer = Console.ReadLine()
Console.WriteLine("Enter the denominator")
Dim b As Integer = Console.ReadLine()


Try
Dim numerator As Integer = Convert.ToInt32(a)
Dim denominator As Integer = Convert.ToInt32(b)

Dim result As Integer = numerator \ denominator

Catch formattingException As FormatException
Console.WriteLine("You must enter two integers")
Catch dividingException As DivideByZeroException
Console.WriteLine(dividingException.Message)


End Try
End Sub

End Class


Imports System
Imports System.IO
Namespace Apress.VisualBasicRecipes.Chapter02

Public Class Recipe02_03

'create a byte array from a decimal
Public Shared Function DecimalToByteArray(ByVal src As Decimal) As Byte()
'Create a MemoryStream as a buffer to hold the binary data.
Using Stream As New MemoryStream
'Create a BinaryWriter to write binary data to the stream
Using writer As New BinaryWriter(Stream)
'write the decimal to the BinaryWriter/MemoryStream
writer.Write(src)
'return the byte representation of the decimal
Return Stream.ToArray
End Using
End Using

End Function

'create a decimal from a byte array
Public Shared Function ByteArrayToDecimal(ByVal src As Byte()) As Decimal
'create a MemoryStream containing the byte array.
Using Stream As New MemoryStream(src)
'create a binaryReader to read the decimal from the stream.
Using reader As New BinaryReader(Stream)
'Read and return the decimal from the
'BinaryReader/MemoryStream
Return reader.ReadDecimal
End Using
End Using

End Function


Public Shared Sub Main()

Dim b As Byte() = Nothing

'Convert a boolean to a byte array and display
b = BitConverter.GetBytes(True)
Console.WriteLine(BitConverter.ToString(b))

'convert a byte array to a boolean and display
Console.WriteLine(BitConverter.ToBoolean(b, 0))

'convert and interger to a byte array and display
b = BitConverter.GetBytes(3678)
Console.WriteLine(BitConverter.ToString(b))

'convert a byte array to integer and display
Console.WriteLine(BitConverter.ToInt32(b, 0))

'convert a decimal to a byte array and display
b = DecimalToByteArray(285998345545.563846696D)
Console.WriteLine(BitConverter.ToString(b))

'convert a byte array to a decimal and display
Console.WriteLine(ByteArrayToDecimal(b))

'Wait to continue
Console.WriteLine(vbCrLf & "Main method complete. Press Enter.")
Console.ReadLine()

End Sub


End Class
End Namespace


Imports System
Imports System.Text.RegularExpressions

Namespace Apress.VisualBasicRecipes.Chapter02

Public Class Recipe02_05

Public Shared Function ValidateInput(ByVal expression As String, ByVal input As String) As Boolean

'Create a new Regex based on the specified regular expression.
Dim r As New Regex(expression)

'Test if the specified input matches the regular expression
Return r.IsMatch(input)

End Function


Sub New(ByVal args As String())

'Test the input frm the command line. The first argument is the regular expression, and the second is the input
Console.WriteLine("Regular Expressions: {0}", args(0))
Console.WriteLine("Input: {0}", args(1))
Console.WriteLine("Valued = {0}", ValidateInput(args(0), args(1)))

'Wait to continue
Console.WriteLine(vbCrLf & "Main method complete. Press Enter")
Console.ReadLine()

End Sub

End Class

Public Class MainClass

''must be shared if you define it inside a class
Public Shared Sub Main()
Console.WriteLine("Enter regex here")
Dim a = Console.ReadLine()
Console.WriteLine("Enter test string here")
Dim b = Console.ReadLine()

Dim stringArray(1) As String
stringArray(0) = a
stringArray(1) = b

Dim ex As New Recipe02_05(stringArray)


End Sub

End Class

End Namespace


#include
int main(void)
{
long num;
long sum = 0l;
int status;

printf("Please enter an integer to be summed");
printf("(q to quit): ");
status = scanf("%ld", &num);
while (status == 1) //means 'is equal to'
{
sum = sum +num;
printf("please enter next integer (q to quit): ");
status = scanf("%ld", &num);
}
printf("Those integers sum to %ld.\n", sum);

return 0;
}


#include
int main(void)
{
int n = 5;

while(n < 7)
{
printf("n = %d\n", n);
n++;
printf("Now n = %d\n", n);
}
printf("The loop has finished.\n");

return 0;
}


using System;
using System.Data;

namespace CreateDataTableAddDataSet
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();

//add a DataTable named Table-1 directly
DataTable dt1 = ds.Tables.Add("Table-1");
//configure the DataTable...add some columns etc.
DataColumn col1 = dt1.Columns.Add();
col1.ColumnName = "PrimaryKey";
col1.DataType = typeof(int);
col1.Unique = true;
col1.AutoIncrement = true;

//add a DataTable named Table-2 by creating the table
//and adding it to the DataSet

DataTable dt2 = new DataTable("Table-2");
//configure the DataTable, add some columns, etc.
DataColumn col2 = new DataColumn();
col2.ColumnName = "CompanyName";
ds.Tables.Add(dt2);
//add multiple DataTables to the DataSet
DataTable dt3 = new DataTable("Table-3");
DataTable dt4 = new DataTable("Table-4");
//configure the DataTable -- add some columns, etc.
ds.Tables.AddRange(new DataTable[] {dt3, dt4});

//output the tables in the DataSet to the console.
Console.WriteLine("DataSet has {0} DataTables named: ", ds.Tables.Count);
foreach (DataTable dt in ds.Tables)
Console.WriteLine("\t{0}", dt.TableName);

Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
}
}

No comments: