Click here to Skip to main content
15,885,669 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
                 if (textBox1.Text == Clipboard.GetText())

So if the textbox has the same text as the clipboards text, I want a messagebox to say "yes". Doesn't seem to work though. Why? :)

What I have tried:

Updated 21-Mar-19 8:15am
Mehdi Gholam 21-Mar-19 12:17pm    
Debug your code and see why.
[no name] 21-Mar-19 12:21pm    
Think about it for a while before running around half-cocked. Display what "GetText()" returns. Aren't you even curious?
F-ES Sitecore 21-Mar-19 12:27pm    
It doesn't work because textBox1.Text does not exactly equal Clipboard.GetText. We don't know what textBox1.Text is, or what Clipboard.GetText is, you'll have to debug your code to see what both of those values are, find the difference and that will give you your answer.
RedDk 21-Mar-19 14:49pm    
I'm looking with interest at the stack of posts you've piled up here at CP in the last few lapses of time and can see that you are truly hooked on this idea of programming. This happened to me long ago. Nineteen-ninty-five to be exact.

What I did was go to => ... you can clear the wheat from the chaff in the treeview panel to the left by simple tick. Using more discreet terms results in a final narrower return.

Often the samples are ExACTLY what a dev/progr is after. If not exactly, at least in the language he/she wishes to address. Methods and all.

We can't tell: it's going to depend on what is on the clipboard - which we can't see - and what is in your textbox - which we can't see either.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. A quick Google for "Visual Studio debugger" should give you the info you need to do the following:

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

To make it easier for a beginner, break up your code slightly so variable contents are easier to see:
string fromTextBox = textBox1.Text;
string fromClipboard = Clipboard.GetText();
if (fromTextBox == fromClipboard)
Now if you put a breakpoint on the if line, you will be able to see exactly what you are comparing by hovering the mouse over the variable names.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
GenJerDan 22-Mar-19 2:50am    
Heck with the breakpoint; put in an else (temporarily) and show what actually is in the TextBox. (In the meantime, Trim both the TextBox and the Clipboard text, and do a ToUpper or ToLower on them both (or otherwise de-case-sensitize the compare)) ;)
OriginalGriff 22-Mar-19 3:17am    
Yes, you could - but the VS debugger is so damn useful that it's worth giving them "the extra effort" of a few minutes learning to get them started using it regularly. We both know just how much time and head scratching it can save you! :laugh:
So if the textbox has the same text as the clipboards text, I want a messagebox to say "yes". Doesn't seem to work though. Why? :)

Most obvious answer is both are not same!
First thing, make sure the test is executed:
                 if (textBox1.Text == Clipboard.GetText())
                 MessageBox.Show("Test executed");

then as shown in S1, use intermediate variables, it will allow you to see their contain with debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900