Solved

C# Copy to Clip board, ignoring tex boxes

Posted on 2011-03-16
4
712 Views
Last Modified: 2013-12-17
Howdy,

Im making an app to store my contacts in.

Ive included the ability to copy the details of a selected contact to the clipboard so they can be pasted to a document or email quickly.

However my problem is that im not sure how to go about making my code, so it ignores empty/incomplete data or textboxes.

Currently this contact would come out like this:-
Badger


ratz_Atacks@hotmail.com
http://badgertracks@blogspot.com

He's a Beer Drinking Buddy!!!

Open in new window

Becuase its missing data (phone numbers and an address in this case)

It would be much better and much more user friendly if it just came out like -

Badger
ratz_Atacks@hotmail.com
http://badgertracks@blogspot.com
He's a Beer Drinking Buddy!!!

Open in new window


Heres my code:-

   //Copies Selected Contact to ClipBoard
        private void btnCopyfromclipboard_Click(object sender, RoutedEventArgs e)
        {
            
            Clipboard.Clear();

            if (listNames.SelectedIndex != -1)
            {

                String compareName = listNames.Items[listNames.SelectedIndex].ToString();

                StringBuilder copy = new StringBuilder();
                copy.AppendLine(tbName.Text);
                copy.AppendLine(tbMobile.Text);
                copy.AppendLine(tbSecphone.Text);
                copy.AppendLine(tbEmail.Text);
                copy.AppendLine(tbWebsite.Text);
                copy.AppendLine(tbAddress.Text);
                copy.AppendLine(tbNotes.Text);

                int attempts = 0;

                while (attempts < 5)
                {
                    try
                    {
                        Clipboard.SetText(copy.ToString());
                        return;
                    }
                    catch (COMException)
                    {
                        Thread.Sleep(10);
                        attempts++;
                    }
                }
            }

        }

Open in new window


So, how do I make my app ignore the empty textboxes/data? (im using WPF,Net 4.0)

Many Thanks

John

P.s yes I ran into the clipboard problem so had to dig up a solution from the interwebs, a tutorial in this case, just incase the code looks fmailiar :-)
0
Comment
Question by:Bronco78th
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35153207
For every single TextBox, do a check like this before appending:
if (tbName.Text.Trim().Length != 0)
{
    copy.AppendLine(tbName.Text);
}

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 35153340
You could also do something like this:
StringBuilder copy = new StringBuilder();
            List<TextBox> tbs = new List<TextBox>();
            tbs.AddRange(new TextBox[] { tbName, tbMobile, tbSecphone, tbEmail, tbWebsite, tbAddress, tbNotes });
            foreach (TextBox tb in tbs)
            {
                if (tb.Text.Trim().Length != 0)
                {
                    copy.AppendLine(tb.Text);
                }
            }

Open in new window

0
 

Author Comment

by:Bronco78th
ID: 35154686
Cheers Idle Mind, that makes sense, will give it a whirl abit later as ive gotta shoot off to work now.

Will try it out and grant point as nessary in my lunchbreak :)

Regards,

John
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35155751
Hi,

Storing and Retrieving Data

Clipboard has static methods to copy and paste data. The SetDataObject method is used to store data that is in object format on the clipboard. The following code snippet copies selected text from a RichTextBoc control to the clipboard.

Clipboard.SetDataObject(richTextBox1.SelectedText);

The GetDataObject method gets the data that is stored on the clipboard. The following code snippet gets data from the clipboard and displays in a Label control.

// Retrieves data

IDataObject iData = Clipboard.GetDataObject();

// Is Data Text?

if (iData.GetDataPresent(DataFormats.Text))

        label1.Text = (String)iData.GetData(DataFormats.Text);

else

    label1.Text = "Data not found.";

Clear Clipboard

Clear method removes all data from the Clipboard.

Clipboard.Clear();
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now