Custom Search
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Matrix Program


This is a matrix program.Execute it and see the Matrix

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace m7tr1x
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Site4Dotnet(MaHi)";
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WindowLeft = Console.WindowTop = 0;
Console.WindowHeight = Console.BufferHeight = Console.LargestWindowHeight;
Console.WindowWidth = Console.BufferWidth = Console.LargestWindowWidth;

Console.WriteLine("press any key");
Console.ReadKey();


Console.CursorVisible = false;
int width, height;
int[] y;
int[] l;
Initialize(out width, out height, out y, out l);
int ms;

while (true)
{
DateTime t1 = DateTime.Now;
MatrixStep(width, height, y, l);
ms = 10 - (int)((TimeSpan)(DateTime.Now - t1)).TotalMilliseconds;

if (ms > 0)
System.Threading.Thread.Sleep(ms);

if (Console.KeyAvailable)
if (Console.ReadKey().Key == ConsoleKey.F5)
Initialize(out width, out height, out y, out l);
}
}

static bool thistime = false;

private static void MatrixStep(int width, int height, int[] y, int[] l)
{
int x;
thistime = !thistime;

for (x = 0; x < width; ++x)
{
if (x % 11 == 10)
{
if (!thistime)
continue;

Console.ForegroundColor = ConsoleColor.White;
}
else
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.SetCursorPosition(x, inBoxY(y[x] - 2 - (l[x] / 40 * 2), height));
Console.Write(R);
Console.ForegroundColor = ConsoleColor.Green;
}
Console.SetCursorPosition(x, y[x]);
Console.Write(R);
y[x] = inBoxY(y[x] + 1, height);
Console.SetCursorPosition(x, inBoxY(y[x] - l[x], height));
Console.Write(' ');
}
}

private static void Initialize(out int width, out int height, out int[] y, out int[] l)
{
int h1;
int h2 = (h1 = (height = Console.WindowHeight) / 2) / 2;
width = Console.WindowWidth - 1;
y = new int[width];
l = new int[width];
int x;
Console.Clear();
for (x = 0; x < width; ++x)
{
y[x] = r.Next(height);
l[x] = r.Next(h2 * ((x % 11 != 10) ? 2 : 1), h1 * ((x % 11 != 10) ? 2 : 1));
}
}

static Random r = new Random();

static char R
{
get
{
int t = r.Next(10);
if (t <= 2)
return (char)('0' + r.Next(10));
else if (t <= 4)
return (char)('a' + r.Next(27));
else if (t <= 6)
return (char)('A' + r.Next(27));
else
return (char)(r.Next(32, 255));
}
}

public static int inBoxY(int n, int height)
{
n = n % height;
if (n < 0)
return n + height;
else
return n;
}
}
}

Read more...

FileStream Open File [C#]

This example shows how to open files for reading or writing, how to load and save files using FileStream in C#. To open file create instance of FileStream class with FileMode and FileAccess enumerations as parameters.

FileStream typical use
This is typical code when opening file using FileStream. It's important to always close the stream. If you don't close the stream it can take a minute to be file again accessible (it will wait to garbage collector to free the FileStream instance and close the file).

[C#]
using System.IO;

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);
try
{
// read from file or write to file
}
finally
{
fileStream.Close();
}

Open file examples
Following examples show the most common cases how to open a file for reading or writing or how to create a file.

[C#] Open existing file for read and write.
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);


[C#] Open existing file for reading.
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read);


[C#] Open existing file for writing.
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Write);


[C#] Open file for writing (with seek to end), if the file doesn't exist create it.
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Append);


[C#] Create new file and open it for read and write, if the file exists overwrite it.
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Create);


[C#] Create new file and open it for read and write, if the file exists throw exception.
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.CreateNew);

Read more...

Total Of Student's Marks



/* Simple Program in C# (for beginners) to input the name from the user and his marks in two subject and find the total in the two subjects. */

Click To View Full Size



//Form1.designer.cs
namespace Student_Marks
{
partial class Form1
{
///
/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///

/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(15, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(75, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Student Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(15, 102);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Marks1";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(15, 148);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(42, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Marks2";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(15, 194);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(37, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Result";
//
// button1
//
this.button1.Location = new System.Drawing.Point(18, 227);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(61, 23);
this.button1.TabIndex = 4;
this.button1.Text = "Calculate";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(84, 227);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(54, 23);
this.button2.TabIndex = 5;
this.button2.Text = "Clear";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 53);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 6;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(96, 95);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 7;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(96, 141);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 20);
this.textBox3.TabIndex = 8;
//
// textBox4
//
this.textBox4.Enabled = false;
this.textBox4.Location = new System.Drawing.Point(96, 187);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 20);
this.textBox4.TabIndex = 9;
//
// button3
//
this.button3.Location = new System.Drawing.Point(143, 227);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(54, 23);
this.button3.TabIndex = 10;
this.button3.Text = "Exit";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(52, 18);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(114, 13);
this.label5.TabIndex = 11;
this.label5.Text = "Display Student Result";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(215, 266);
this.Controls.Add(this.label5);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Student Report";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label5;
}
}



// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Student_Marks
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
/*Form2 form = new Form2();
form.Show();*/
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}

private void button1_Click(object sender, EventArgs e)
{

int marks1 = Convert.ToInt32(textBox2.Text);
int marks2 = Convert.ToInt32(textBox3.Text);
textBox4.Text = Convert.ToString(marks1 + marks2);
}

private void button4_Click(object sender, EventArgs e)
{

}
}
}

Read more...

Add Two Numbers in C#


This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together.

I am using the System.Console object to read from the console and then converting the string inputs into integers with the Convert.ToInt32 function (the System.Convert has many methods and ToInt32 is one of these).

  1. Start Visual Studio.
  2. On the File menu, point to New, and then click Project.
  3. In the Templates Categories pane, expand Visual C#, and then click Windows.
  4. In the Templates pane, click Console Application.
  5. Type a name for your project in the Name field.
  6. Click OK.

    The new project appears in Solution Explorer.
  7. If Program.cs is not open in the Code Editor, right-click Program.cs in Solution Explorer and then click View Code.

  8. Replace the contents of Program.cs with the following code.
The code:

using System;       // for the console object
 
class addtwonumbers
{
       public static void Main()
       {
              try 
              {
                     // output to the console
                     Console.Write("Please enter value 1 : ");
                     // readin the console input, and then convert to a integer value
                     int val1 = Convert.ToInt32(Console.ReadLine());
                     Console.Write("Please enter value 2 : ");
                     int val2 = Convert.ToInt32(Console.ReadLine());
                     // write out the answer 
                     Console.WriteLine("Answer = " + (val1 + val2));
              }
              catch (Exception e)
              {
                     // any errors. Like converting a string to a integer value
                     Console.WriteLine("Error : " + e.ToString());
              }
       }
}

save that as addtwonumbers.cs.


Once compiled  and executed(press F5 will using visual studio) the output will be:


Please enter value 1 : 50
Please enter value 2: 25
Answer = 75

Read more...

Calender Program In C#


Beginning of Calendar.cs

 This program prints out the calendar for a full year, specified by the user.


using System;

class Calendar
{
const int Sunday    = 0;
const int Monday    = 1;
const int Tuesday   = 2;
const int Wednesday = 3;
const int Thursday  = 4;
const int Friday    = 5;
const int Saturday  = 6;

static void Main (string[] args)
{
    int year;

    GiveInstructions();
    year = GetYearFromUser();
    PrintCalendar(year);
}

static void GiveInstructions ()
{
    Console.WriteLine("This program displays a calendar for a full");
    Console.WriteLine("year.  The year must not be before 1900.");

}
static int GetYearFromUser ()
{
    int year = 1900;

    while (true)
    {
        Console.Write("Which year? ");
        year = Int32.Parse(Console.ReadLine());
        if (year >= 1900) return year;
        Console.WriteLine("The year must be at least 1900.");
    };
}

static void PrintCalendar(int year)
{
    int month;

    for (month = 1; month <= 12; month++) 
    {
        PrintCalendarMonth(month, year);
        Console.WriteLine();
    }
}

static void PrintCalendarMonth(int month, int year)
{
    int weekday, nDays, day;

    Console.WriteLine("    " + (MonthName(month)) + " " + year);
    Console.WriteLine(" Su Mo Tu We Th Fr Sa");
    nDays = MonthDays(month, year);
    weekday = FirstDayOfMonth(month, year);
    IndentFirstLine(weekday);
    for (day = 1; day <= nDays; day++)
    {
       if (day > 9) Console.Write(" " + day);
       else Console.Write("  " + day);
       if (weekday == Saturday) Console.WriteLine();
       weekday = (weekday + 1) % 7;
    }
    if (weekday != Sunday) Console.WriteLine();
}

static void IndentFirstLine(int weekday) 
{
    int i;

    for (i = 0; i < weekday; i++) 
    {
        Console.Write("   ");
    }
}      


static int MonthDays(int month, int year)
 {

    switch (month) 
    {
      case 2:
        if (IsLeapYear(year)) return (29);
        return (28);
      case 4: case 6: case 9: case 11:
        return (30);
      default:
        return (31);
    }
}

static int FirstDayOfMonth(int month, int year)
{
    int weekday, i;

    weekday = Monday;
    for (i = 1900; i < year; i++) 
    {
        weekday = (weekday + 365) % 7;
        if (IsLeapYear(i)) weekday = (weekday + 1) % 7;
    }
    for (i = 1; i < month; i++)
    {
        weekday = (weekday + MonthDays(i, year)) % 7;
    }
    return (weekday);
}

static string MonthName(int month)
{
    switch (month)
   {
      case  1: return ("January");
      case  2: return ("February");
      case  3: return ("March");
      case  4: return ("April");
      case  5: return ("May");
      case  6: return ("June");
      case  7: return ("July");
      case  8: return ("August");
      case  9: return ("September");
      case 10: return ("October");
      case 11: return ("November");
      case 12: return ("December");
      default: return ("Illegal month");
    }
}

static bool IsLeapYear(int year)
{

    return ( ((year % 4 == 0) && (year % 100 != 0))
             || (year % 400 == 0) );

} 

} 

Read more...

Back to TOP