Solved

C# Copy to Clip board, ignoring tex boxes

Posted on 2011-03-16
4
718 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

13 Experts available now in Live!

Get 1:1 Help Now