Showing posts with label WindowsForms. Show all posts
Showing posts with label WindowsForms. Show all posts

Wednesday, 2 November 2011

Download File Synchronously

Questions Received:
        How to download a file synchronously.

Solution:
     The following is the logic to download a file from Web URL,      
        webClient webClient = new WebClient(); 
        webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");
 
[Note:] Reference Namespace,                  
          using System.Net;

Download File Asynchronously

Questions Received:
          How to download a file asynchronously using C sharp Client.



Solution:
      Here is the business logic for download a file from website URL,

        private void btnDownload_Click(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            webClient.DownloadFileAsync(new Uri("http://education.vnu.edu.vn/eng/pic_upload/1252901905~Master_Mathematics.doc"), @"d:\Files.doc");
        }

        private void ProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download completed!");
        }

[Note:] The Namespaces are,
                            using System.Deployment.Application;
                            using System.Net;

Thursday, 13 October 2011

Settings the controls in Windows Form

Questions Received:
        How to set/unset the controls in the Windows Form

Solution:
        To find the required using the accessible name of the controls, use the 'find' method in
Form.Controls.find().
        public static void set(Control form,string ctrlName,string DBName)
        {
            foreach (Control ctrl in form.Controls.Find(ctrlName, true))
            {
                if (ctrl is CheckBox)
                {
                    CheckBox chk = (CheckBox)ctrl;
                    if (chk != null)
                    {
                        chk.Checked = true;
                    }
                }
                if (ctrl is TextBox)
                {
                    TextBox tb = (TextBox)ctrl;
                    if (tb != null)
                    {
                        tb.Text = DBName;
                    }
                }
            }
        }

[Note:] Assume, Form-->this, ctrlName-->AccessibleCtrlName, DBName-->TextFieldEntry
           You can use this function to manage different controls in the Form

Saturday, 1 October 2011

Reset Form Fields to default NULL Values

Question Received:
         How to reset form fields and controls thru programming?

Solution: 
         To reset the form controls and fields with NULL (or) Empty String,
          Call function ResetFields(this) from any function,
              public static void ResetFields(Control form)
             {
                   foreach (Control ctrl in form.Controls)
                  {
                       if (ctrl.Controls.Count > 0)
                       ResetFields(ctrl);
                       Reset(ctrl);
                  }
             }
             public static void Reset(Control ctrl)
            {
                  if (ctrl is TextBox)
                  {
                       TextBox tb = (TextBox)ctrl;
                       if (tb != null)
                      {
                           tb.ResetText();
                      }
                  }
                  else if (ctrl is ComboBox)
                  {
                       ComboBox cb = (ComboBox)ctrl;
                       if (cb != null)
                      {
                           cb.ResetText();
                      }
                  }
                  else if (ctrl is CheckBox)
                  {
                       CheckBox chk = (CheckBox)ctrl;
                       if (chk != null)
                       {
                           chk.Checked = false;
                       }
                  }
                  else if (ctrl is RadioButton)
                  {
                       RadioButton rb = (RadioButton)ctrl;
                       if (rb != null)
                       {
                           rb.Enabled = true;
                       }
                  }
             }