Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Copy to Clip board, ignoring tex boxes

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 54
vb.net winforms sizing/resolution? 4 40
Powershell File Sort 8 41
What are the simplest ways to create a Merge Sort? 3 29
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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