CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2013
    Posts
    3

    C# code remove text from label before email is compiled

    Hi everyone,

    Im newer to c# so any help would be appreciated. I have an application that will email several contacts. I want to delete anyone that is listed as "open position". Below is what i have so far...

    Code:
    mailItem.To = lblc1name.Text + ";" + lblc2name.Text + ";" + lblc3name.Text + ";" + lblc4name.Text;

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# code remove text from label before email is compiled

    What do you mean "open position"? Do you mean a blank string (i.e. String.Empty)?

  3. #3
    Join Date
    Jun 2013
    Posts
    3

    Re: C# code remove text from label before email is compiled

    "Open position" is just text that may populate in any of the lbl seen in the mail to code. I want the code to delete any lbl s that say open position before putting them in the TO section of an email. Hopefully this makes sense.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# code remove text from label before email is compiled

    Quote Originally Posted by hex2005 View Post
    "Open position" is just text that may populate in any of the lbl seen in the mail to code. I want the code to delete any lbl s that say open position before putting them in the TO section of an email. Hopefully this makes sense.
    You're doing it the hard way, so consider another approach. Consider only add entries that don't have "Open position" and aren't blank.

    To do this, write a couple of static helper methods..
    Code:
    private static bool IsValidEmail(string emailAddress)
    {
      return !String.IsNullOrEmpty(emailAddress) && !emailAddress.Contains("Open position");
    }
    
    private static void AddEmailAddress(StringBuilder sb, string emailAddress)
    {
      // Check if email address is valid
      if (!IsValidEmail(emailAddress)) { return; }
    
      // Check if 1st email address
      if (sb.Length == 0)
      {
        sb.Append(emailAddress);
      }
      else
      {
        // Not 1st email address, so pre-pend a ';' character
        sb.AppendFormat(";{0}", emailAddress);
      }
    }
    Next, use the helper methods.
    Note: we declare a StringBuilder object for a bit of efficiency (although not strictly needed when only adding a few entries).
    Code:
    ///
    /// Forms the mail To: list.
    /// Note: you should do some error checking to ensure that at least
    /// one email address is valid before entering this method.  Typically you would disable the 'Send' button
    /// on a form until all the relevant info has been filled out (I.e. valid, To line entries, subject, body, etc.)
    ///
    private void FormMailTo(MailItem mailItem)
    {
      // Declare the string builder object
      var sb = new StringBuilder();
    
      // Verify and add each address
      AddEmailAddress(lblc1name.Text);
      AddEmailAddress(lblc2name.Text);
      AddEmailAddress(lblc3name.Text);
      AddEmailAddress(lblc4name.Text);
    
      
      // Set the mail item To: list 
      mailItem.To = sb.ToString();
    }

  5. #5
    Join Date
    Jun 2013
    Posts
    3

    Re: C# code remove text from label before email is compiled

    Perfect! Thank you sir!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured