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

Thursday, August 8, 2013

Reloading a Form in C#

I'm been doing some work recently in C# on a Windows Form I'm creating for a pet project.  I'm creating a form that presents customer information and allows the user to scroll through each customer using "Previous" and "Next" buttons.

While working through the form, I was able to get my data into an array and then present the first customer in the form.  I then figured out how to update the index of the array for the next customer, but I couldn't figure out how to present the next data into the form.  Through my research, I found the following post:

Reloads Form in C#

Scrolling through the answers, one answer says this:

try this
this.formName_Load(this, null);

Kapul
I then put this code into my program right after I've updated the index of my array for the next customer.  Magically (not really) the form reloaded with my new data.

I wanted to discuss the different portions of the above code and what it does:

  • this - refers to the current instance of the class.  For example, you could have multiple classes that have the same form names and each could be referenced by other classes.  "this" makes sure that you are referencing the current class.
  • formName - the name of your form.  For example, my form is called "customerForm", so I would replace formName with customerForm.
  • _Load - tells the form to load.  To read more on Load event, go here: Form.Load Event
  • (this, null) - The "this" again refers to the current class.  null is used in place of the EventArgs class (which is used to provide data for events).  Since this is an event that doesn't contain any data, we then pass a null, which is an empty field.  To read more on the EventArgs class, go here: EventArgs Class

Monday, July 8, 2013

Creating a Child Form in C#

I spent some time this weekend working in C# on a personal project.  One of the subjects I came across was creating a child form.  A child form opens a new form within your current window.

During my research, I came across the following help document:

How to: Create MDI Child Forms

I have a program I created at work that has different tabs for specific tasks that need to be run.  I will be working on migrating from the tabbed format to the child form format, as this seems to be less confusing and the layout looks better.

A few key points for creating a child form:

  1. Create a new windows form in your project for each new window.
  2. Create an event handler for when you click on the button/menu item that will open the new form (Create Event Handler)
  3. Paste the following in your event handler:
protected void MDIChildNew_Click(object sender, System.EventArgs e)
{
   Form2 newMDIChild = new Form2();
   // Set the Parent Form of the Child window.
   newMDIChild.MdiParent = this;
   // Display the new form.
   newMDIChild.Show();
}

Wednesday, May 29, 2013

Sending Email Using C#

I thought I would change things up a bit today and actually talk about a little bit of C#.  I currently use Visual C# at work for some projects and some of those include sending email.  First, you need to make sure to add the System.Net.Mail namespace to your C# project.  This namespace gives you a list of different classes you can use to send email.  Once the namespace has been added, here is the code I use to create a method to send email.  It allows you to provide the body, subject and an attachment to the method:

public static string SendEmail(String body, String subject, Attachment attachment)
        {
            SmtpClient smtp = new SmtpClient("mail.companyname.com");

            string fromEmail = "my.email@companyname.com";

            MailMessage objEmail = new MailMessage();

            objEmail.IsBodyHtml = true;

            objEmail.To.Add("to.email@companyname.com");

            objEmail.From = new MailAddress(fromEmail, "My Name");

            objEmail.Subject = subject;

            objEmail.Body = body;

            objEmail.Attachments.Add(attachment);

            try
            {
             
                smtp.Send(objEmail);

                return "true";

            }
         
            catch (Exception exc)
         
            {
                return exc.Message;
             }

        }

If you wanted to create a method that sends an email, but doesn't include an attachment, use the following:

public static string SendEmail(String body, String subject)
        {
            SmtpClient smtp = new SmtpClient("mail.companyname.com");

            string fromEmail = "my.email@companyname.com";

            MailMessage objEmail = new MailMessage();

            objEmail.IsBodyHtml = true;

            objEmail.To.Add("to.email@companyname.com");

            objEmail.From = new MailAddress(fromEmail, "My Name");

            objEmail.Subject = subject;

            objEmail.Body = body;

            try
            {
             
                smtp.Send(objEmail);

                return "true";

            }
         
            catch (Exception exc)
         
            {
                return exc.Message;
             }

        }

These methods are pretty basic, since the email addresses are hard-coded.  You will want to create your own method if your "to" email is dynamic or you want to pass the email values into the method.