RSS

Monthly Archives: March 2013

remember me information winform c#


You can use the ConfigurationManager Class to manage your application’s settings.

you can use this function to add new Keys to your configuration file:

surveyform

 

 

public bool setSetting(string pstrKey, string pstrValue)
{
    Configuration objConfigFile =
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    bool blnKeyExists = false;

    foreach (string strKey in objConfigFile.AppSettings.Settings.AllKeys)
    {
        if (strKey == pstrKey)
        {
            blnKeyExists = true;
            objConfigFile.AppSettings.Settings[pstrKey].Value = pstrValue;
            break;
        }
    }
    if (!blnKeyExists)
    {
        objConfigFile.AppSettings.Settings.Add(pstrKey, pstrValue);
    }
    objConfigFile.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    return true;
}


and then save up your username (for example)

write this code on login button click event

if (chkrember.Checked==true)
{
setSetting(“username”, txtloginid.Text);
setSetting(“mypasswd”, txtpassword.Text);

}
else
{
setSetting(“username”, “”);
setSetting(“mypasswd”, “”);
}

 

On Load write this code

txtLogin.Text = ConfigurationManager.AppSettings[“username”];
txtPassword.Text = ConfigurationManager.AppSettings[“mypasswd”];
if( txtLogin.Text.Length>0){
chkrember.Checked = true;
}

Once your application starts up, you can read the information you saved earlier from yourConfigurationManager
txtloginid.Text = ConfigurationManager.AppSettings["username"];

txtpassword.Text = ConfigurationManager.AppSettings["mypassword"];
Click on Project properties and add username & mypassword 

remberme
Advertisements
 
Leave a comment

Posted by on March 28, 2013 in WinForm

 

Tags:

How do I make a textbox that only accepts numbers?


private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) 
        && !char.IsDigit(e.KeyChar) 
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }

    // only allow one decimal point
    if (e.KeyChar == '.' 
        && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}
 
Leave a comment

Posted by on March 19, 2013 in WinForm

 

MDI Parent background color


The reason you are having this problem is because when you turn a form into an MdiParent you are also having an MdiClient object  added on your behalf that doesn’t show up in your code. When you change the BackColor of the form, any change are hidden by the MdiClient that is taking up all of the area where your desired BackColor is actually being displayed.

To fix this… we need to enumerate through the controls on the form to find the MdiClient, once we’ve found it, we change it’s BackColor ala:

 

foreach (Control c in this.Controls)
{
if( c is MdiClient)
c.BackColor = Color.Red;
}

 

For System Colors Change MDI Background Color

 

private void frmMDIParent_Load(object sender, EventArgs e)
{

foreach (Control c in this.Controls)
{
if (c is MdiClient)
c.BackColor = SystemColors.GradientInactiveCaption;
}
}

 
Leave a comment

Posted by on March 15, 2013 in WinForm

 

Tags: , , , ,

how check if the form is already open then Close all open form


Hi dear,

his is what I used to close all open forms (except for the main form)

private void CloseOpenForms()
    {

           // Close all open forms - except for the main form.  (This is usually OpenForms[0].
           // Closing a form decrmements the OpenForms count
           while (Application.OpenForms.Count > 1)
           {
               Application.OpenForms[Application.OpenForms.Count-1].Close();
           }
    }

 

 

 
Leave a comment

Posted by on March 15, 2013 in WinForm

 

Tags: , , ,

Age Calculation in C#


 
This post explains how Age can be calculated in C# using DateTime and TimeSpan Classes.
SourceCode:
private string GetAge(DateTime birthday)
{
    DateTime Now = DateTime.Now;
    //Get Years  
    int Years = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year - 1;
    //Get Past Year to calculate months, days and time  
    DateTime dtPastYearDate = birthday.AddYears(Years);
    int Months = 0;
    for (int i = 1; i <= 12; i++)
    {
        if (dtPastYearDate.AddMonths(i) == Now)
        {
            Months = i;
            break;
        }
        else if (dtPastYearDate.AddMonths(i) >= Now)
        {
            Months = i - 1;
            break;
        }
    }
    int Days = Now.Subtract(dtPastYearDate.AddMonths(Months)).Days;
    int Hours = Now.Subtract(dtPastYearDate).Hours;
    int Minutes = Now.Subtract(dtPastYearDate).Minutes;
    int Seconds = Now.Subtract(dtPastYearDate).Seconds;
    return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s) {3} Hour(s) {4} Second(s)",
                        Years, Months, Days, Hours, Seconds);
}

 

 

 
Leave a comment

Posted by on March 15, 2013 in ASP Dot Net C#

 

Tags: