Solved

C# Copy to Clip board, ignoring tex boxes

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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