Personalized lock screen

Microsoft Windows 8 Personalized lock screen. from windows.microsoft.com

The tiles of the Start screen

Microsoft Windows 8 The tiles of the Start screen from windows.microsoft.com.

Touch keyboard

Microsoft Windows 8 Touch keyboard from windows.microsoft.com.

Showing posts with label General C#. Show all posts
Showing posts with label General C#. Show all posts

How to Convert Time in Different Time Zones C#

Time Zone
In this article, we will discuss about how to convert time according to time zone. I am using TuneZoneInfo class to retrieve information about the current time zone and you can convert it to UTC (Coordinated Universal Time) or vice versa. And also you can use this class to retrieve information on any time zone defined on the local system.




#region TimeZoneConverter Source 

      private void frmTimeZoneConverter_Load(object sender, EventArgs e)
      {
         // get and set current date and time
         lblLocalTimeIndicator.Text = DateTime.Now.ToString();

         // Get system (local) time zone information in to readonlycollection list
         ReadOnlyCollection<TimeZoneInfo> TimeZoneColl = TimeZoneInfo.GetSystemTimeZones();
         cmbTimeZones.DataSource = TimeZoneColl;
         cmbTimeZones.DisplayMember = "DisplayName";
         cmbTimeZones.ValueMember = "Id";

      }

      private void cmdTimeConvert_Click(object sender, EventArgs e)
      {
         if (cmbTimeZones.SelectedIndex > 0)
         {
            DateTime dt = DateTime.Now;

            // Convert current time in to selected time zone.
            lblConvertedTime.Text = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, cmbTimeZones.SelectedValue.ToString()).ToString();

         }
      }

      #endregion TimeZoneConverter

      #region Information 
      private void GenaralInformation()
      {
         TimeZoneInfo tzi = TimeZoneInfo.Local;

         // Gets and sets standared time zone name.
         lblCurrentTimeZoneStd.Text = tzi.StandardName;

         // Gets and sets the daylight saving time zone name.
         lblCurrentTimeZoneDlt.Text = tzi.DaylightName;

         //set current time and date
         lblCurrTime.Text = DateTime.Now.ToString();

         // check whether daylight serving is available
         lblDaylightSavingYes.Text = tzi.IsDaylightSavingTime(DateTime.Now).ToString();

         // set universal time
         lblUniTime.Text = DateTime.Now.ToUniversalTime().ToString();

         // get and set time different between local and universal time(UTC).
         lblOffset.Text = tzi.BaseUtcOffset.ToString();

      }

      private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
      {
         GenaralInformation();
      }

      #endregion





This will Support this .Net frameworks
.net framework 4.0
.net framework 3.5
.net framework 3.0
.net framework 2.0

.NET Framework Client Profiles
Supported in: 4, 3.5 SP1

The entire source code of this article can be downloaded over here.

If you have any question regarding this article please be kind enough to mention it on comment area.
Thank you for viewing this article.
Give me a +1 if you think it was a good article


Add a row / Multiple rows to the data table c#.net

I suppose to use a data table as data source in my grid View controller. Can you please explain how I need to do it?

1. The first step is to create an empty Data Table.
DataTable dt = new DataTable();

2. Second step is to create a new DataRow object.
DataRow newRow = dt.NewRow();

3. Now you have created and initialized a DataRow. So then it is needed to add values to rows.
newRow["id"] = 1; // we have created column call “id” and add it to value 1
newRow["username"] = "adam" ; // we have created column call “username” and add it to value “adam”.

4. Next, we want to add this newly created row (with two columns) to the blank Datatable we created in the first step.

dt.Rows.Add(newRow);

5. You can now set this created Datatable as a DataSource and then DataBind to the GridView or any other similar C# control.

gridView1.Datasource = dt;
gridView1.Databind();

All the steps from above are put together in the below:

DataTable dt = new DataTable();
DataRow newRow = dt.NewRow();
newRow["id"] = 1;
newRow["username"] = "adam" ;
dt.Rows.Add(newRow);
gridView1.Datasource = dt;
gridView1.Databind();


Let see how to add multiple records in datatable without write multiple time code

DataTable dt = new DataTable();
dt.Columns.Add("Min", System.Type.GetType("System.Int32"));
dt.Columns.Add("Max", System.Type.GetType("System.Int32"));
dt.Columns.Add("Avg", System.Type.GetType("System.Int32"));

for (int i = 0; i < i =" i">
{
DataRow dr = dt.NewRow();
row["Min"] = i;
row["Max"] = i + 9;
row["Avg"] = (i + 9) / 2;
dt.Rows.Add(dr);
}

You can add values to row using for loop.










How to use savefiledialog box in the c#.net ?

// Create a new SaveFileDialog object
SaveFileDialog obDialogSave = new SaveFileDialog();

// Set Default file extension to dialog
obDialogSave.DefaultExt = "txt";

// Other Available file extensions to dialog
obDialogSave.Filter = "Text file (*.txt)|*.txt|XML file (*.xml)|*.xml|All files (*.*)|*.*";

// Adds a extension if the user does not select
obDialogSave.AddExtension = true;

// Restores the selected directory, next time
obDialogSave.RestoreDirectory = true;

// To display Dialog title
obDialogSave.Title = "Where do you want to save the file?";

// Start directory
obDialogSave.InitialDirectory = @"C:/";

// Show the dialog and process the result
if (obDialogSave.ShowDialog() == DialogResult.OK)
{
// your code go here if user click on “OK” button.
MessageBox.Show("You selected the file: " + DialogSave.FileName);
}
else {
// your code go here if user click on “Cancel” button.
MessageBox.Show("You click cancel.");
}

obDialogSave.Dispose();

// Set object to null.
obDialogSave = null;

What is .Resx file in Dot Net?

What is .Resx file in Dot Net? How to access?

Resource files are used in .NET to store culture-specific data all in one place, separate from the code. For example, suppose you are developing a multi-language Web site and you have a form on a page with a label beside a text field and the label in English says "First Name." Instead of having code like this:

if (language == "English")
{
lblFirstName = "First Name";
}
else if (language == "German")
{
lblFirstName = "Vorname";
}
You can just do this:

ResourceManager resourceManager = new ResourceManager("nameSpace.resourceFileBaseName",
Assembly.GetExecutingAssembly());
lblFirstName = resourceManager.GetString("lblFirstName");
And that's it—just two lines. There's no need to change a million sections of code each time you add or take away a language.


Reading from .Resx File:

You can access the .Resx file using this code:

ResXResourceReader reader = new ResXResourceReader(Server.MapPath("fileName.resx"));
IDictionaryEnumerator rsxr = reader.GetEnumerator();
foreach (DictionaryEntry d in reader)
{
Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
}
//Close the reader.
reader.Close();

Writing to .Resx File:

We can write the binary data of an image to the .Resx file using the following code

Image img = Image.FromFile("abc.jpg");
ResXResourceWriter rsxw = new ResXResourceWriter("abc.resx");
rsxw.AddResource("urdu.jpg",img);
rsxw.Close();

Use of .Resx File:

It is very useful while working on Localized project [Multiple Languages], You can make different resource files for different languages and depending upon the user choice you can change the Language of the application.

C# reading data from Serial port

Question -

I have a device (a GPS type of device) that constantly pushes communication through a standard serial connection (COM1).

What I need to be able to do is:

Programatically:
1) Open the serial port
2) Listen for a given period of time (say 2 seconds) to the data that is being received via the serial port.
3) populate a multi-line text box with the data that was received from the serial port.
4) close teh serial port

Then I will build additional code surrounding handling the data in the textbox.


Answer -

I set up my serialport like this

sp.BaudRate = 115200;
sp.PortName = "COM53";
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.None;
sp.ReadTimeout = 0;
sp.Open();
sp.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(sp_ DataReceived);


and then I have this

void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{

string Mystring = sp.ReadExisting();
byte testbyte = 254;
// Gather all the bytes until 102 is reached
foreach (byte c in Mystring)
{
if(pixelcount<102) pixelcount ="="" testbyte =" {0}" oldx =" 0;" pixelcount =" 0;">

How to use GridView and DataSets in C#.NET 2.0?

Question -

I need a example of how to create a gridview in .cs versus just dragging and dropping the gridview on the aspx page?

I created a dataset and called a stored procedure using data adapter, but i do not know how to data bind the data set into the grid view I have.

It seems everywhere I search the gridview examples they have is just dragging and dropping to the page and configuring a datasource.
Answer -

//connect to database via dataset & dataadapte
SqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("stored proc name", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);

//create gridview by code & bind dataset to it
GridView g = new GridView();
Page.Controls.Add(g);
g.DataSource = ds;
Page.DataBind();