Solved

C# Copy to Clip board, ignoring tex boxes

Posted on 2011-03-16
4
731 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
[X]
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
  • 2
4 Comments
 
LVL 86

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 86

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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

630 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