If the current (most recent) item in the clipboard is a string, I want to take the string from the clipboard, and put each word in an array (just a simple loop which eliminates spaces and newlines, to take just words from the string). Then I simply want to print each item in the array (let's say each separated by a newline for testing purposes) to the terminal.
This is super easy, simple, etc. My trouble is that I cannot seem to find/use the Clipboard class that is built in to C#! Here is what I am using as my assembly references:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Object;
using System.Windows.Clipboard;
Note that the System.Windows.Clipboard; gets a red underline, because it is not lining up. Why could this be? http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx shows the clipboard class information.
It's probably a fault on my end, but is it otherwise possible I have not set my paths correctly or something? I just installed Visual Studio 2012 For Desktop SP3 (although, I have had VS2012 for Web for quite some time, and it works properly).
System.Windows.Clipboard is a class and not a namespace, there is no point putting a class into a using directive.
The compiler error message should tell you the same.
First of all you must add a reference to System.Windows.Forms in your application. Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened. You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.
using System;
using System.Windows.Forms;
class Program {
[STAThread]
static void Main(string[] args) {
Clipboard.SetText("this is in clipboard now");
}
}
i hope this will help you
http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/
it shows how to use the Clipboard functions in C# console application
You don't need this line: using System.Windows.Clipboard; using statements are for namespaces, and Clipboard is a class. That class is provided from the assembly PresentationCore.dll, and since you have WPF project, your project already has a reference to it.
You should have reference to System.Windows namespace only it has Class Clipboard in it.
You can set and get data from there
System.Windows.Clipboard.SetData();
You'll need a namespace declaration:
using System.Windows.Forms;
OR for WPF:
using System.Windows;
To copy an exact string
var dataObject = System.Windows.Clipboard.GetDataObject();
string text= dataObject.GetData("UnicodeText", true).ToString();
Related
I'm working on a cross platform app using the GTK toolkit and when I'm trying to access the System.IO namespace to extract Path fields, I'm getting the following error:
Error CS0119: 'Widget.Path(out uint, out string, out string)' is a method, which is not valid in the given context (CS0119) (netmonmd)
I have using System.IO; in the source file and I'm using other namespaces without any issue (eg System.Console and System.Refelection without any issues further down in the code).
I have almost identical code in a console application working without an issue.
Here is the code, if I fully provide the Path it works, I just can't use it within the namespace (but I can use other things that are in the same namespace)
using Gtk;
using System;
using System.IO;
using static System.Console;
public partial class MainWindow : Gtk.Window
{
private netmonmd.iplabel[] ids;
private int fSize = 12;
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
//char filesep = System.IO.Path.DirectorySeparatorChar; // works
char filesep = Path.DirectorySeparatorChar; // error
I'm trying to understand the error (I am new to C#) and think it has to do with calling it within the MainWindow function since it seems to be referring to Widget.Path and not System.Path
Is this a case of just having to fully qualify it each time?
I could move all the code out of the main window and process it elsewhere in the app.cs file, however it is a single window app that I was developing in the one space. If it is better practise to separate it out I will do that.
(as is usually the case in asking on stack, while writing this question out I'm becoming more aware of what's actually happening here, I just don't know how to work around it)
It can be seen and compiled inside the same file it is in inside the same project, the file is called default.aspx.cs.
However when I try to include the namespace in another file of the same project using the using DBConnStrings; statement -> I keep getting the compiler error "The type or namespace name 'DBConnStrings' could not be found".
The Code in the file which compiles called default.aspx.cs is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DBConnStrings;
namespace DBConnStrings
{
public class GlobalStrings
{
public static string carSalesDBConnString =
ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
}
}
public void BindManu()
{
SqlConnection conn = new
SqlConnection(GlobalStrings.carSalesDBConnString); // Connect to Carsales database
conn.Open();
// .....
}
The other files can not see this namespace although they are in the same project.
How do I make them see it?
First of all, I'd suggest to put the method BindManu into the class, because I can't imagine this would work like that.
But to your problem: You have to specify the whole namespace. That means, if the file with the DBConnStrings namespace is in the folder test, you have to use using test.DBConnStrings to import the namespace. You should name the namespace like that as well to avoid confusion (namespace test.DBConnStrings).
But you actually don't have to specify the whole path, just the path within the project. That means, if your class is in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject\DirectoryA\DirectoryB\MyClass with the project located in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject, your namespace would be BlaProject.DirectoryA.DirectoryB. If the file your using isn't in your project folder, then you have to add a reference and use the path within the other project as above (but with another project name, obviously). If you want to add references, open the Solution Explorer, right click onto References, select add Reference, and select the reference to the project.
If you don't want to struggle with all that you can let vs do it for you. Simply type in the class you want from the other namespace, it will be marked as an error, click onto the lightbulb and select something like add using reference.
Furthermore, if you want to add a class to your project, don't do it with the explorer - simply right click onto the folder from your project you want to add the class to in your Solution Explorer, select Add, then New Item. In the popup select class and type in your name for the class. That's it!
I'm attempting to compare two images using Visual Studio 2013 Pro. The MSDN provides information (http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.imagecomparer.compare.aspx) on ImageComparer.Compare, alas I'm failing to get it implemented in my code. On the last line of my code I'm told that "The name 'Compare' does not exist in the current context". Can someone please help? Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
namespace Intranet.SmokeTests
{
public class Intranet_Login : Intranet_Setup
{
public List<string> IntranetLoginTest(string BrowserURL, string Host, int Port)
{
Image expected = Image.FromFile(#"\\webdriver\ImageVerification\Expected\IntranetHome.png");
Image actual = Image.FromFile(#"\\webdriver\ImageVerification\Actual\IntranetHome.png");
bool equal = Compare(actual, expected);
}
}
}
You must do it like this:
bool equal = ImageComparer.Compare(actual, expected);
When you want to use a class's static member in c# you must always qualify it with the class first. Otherwise the compiler will try to locate the member on the current class.
Another problem you might be having with your IntranetLoginTest is that it's supposed to return an instance of List<string>, but it doesn't. I must also say I find it strange that you are making an image comparison test in a method that would suggest it performs authentication mechanisms testing.
1- Using nuget Install System.Drawing.Common
2- Reference Microsoft.VisualStudio.TestTools.UITesting.dll from C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\TestPlatform
Image expected = Image.FromFile(#"2020-09-01_15h31_24.png");
Image actual = Image.FromFile(#"2020-09-01_15h31_30.png");
Image difference = null;
var isTestPass = ImageComparer.Compare(actual, expected, out difference);
if (!isTestPass)
difference.Save("diff.png");
Console.ReadLine();
Expected
Actual
Difference
It turns out that the correct version of the referenced dll was not being added. The complete answer is here: How can I make the namespace locally match what is listed on MSDN?
I made the same ASP.NET C# project in both VS2010 and MonoDevelop using these two classes among the standard files (Site.Master, Web.Config, Default.aspx, etc.) and recieve this same error (CS0234) seen at the bottom.
Login.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mynamespace;
namespace mynamespace
{
public partial class Logon
{
public void btnClicked(object sender, EventArgs e)
{
//ERROR IS HERE:
mynamespace.Test session = new mynamespace.Test();
//Obviously, this doesn't work either:
Response.Write(session.echoUser());
}
}
}
Test.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mynamespace;
namespace mynamespace
{
public class Test
{
public string echoUser()
{
return "foobar";
}
}
}
I recieve the same error in both IDEs, here is the MonoDevelop error:
The type or namespace 'Test' does not exist in the namespace 'mynamespace' (are you missing an assembly reference?) (CS0234) Logon.cs
Basically, the class Test refuses to instantiate. Any input is appreciated!
If you have that Test class in an ASP.Net web project, then you need to place it in the App_Code folder, not just anywhere in the site.
You need to refer the projects to each other, if you haven't done so yet. I'm guessing that you don't get the intellisense to show the class in the other namespace, right?
You can see the References on the right side (most cases) under your project view. You can right click there and choose to Add reference. Then you browse to the binaries from the pther projects. (You might be able to point to the project itself too - I don't have VS in front of me at the moment.)
Also, It's a convention to use camel case for namespaces, so it should be MyNameSpace.
If the classes are in the same project, you might want to skip using mynamespace and refer to the class by Test intead of mynamespace.Test.
If these are both in the same project, please check that both files are included in the project and have the "Build Action" property set to "Compile". Please also check that all of these namespaces have exactly matching spelling and casing.
I have written a simple Form1 (inherit from Form) class in Visual Studio C#. All was good.
Then I wanted to change the name of the class and the namespace to something meaningful instead of the default 'WindowApplicationForm1' . I also changed the file name of the Form1.cs class to match the new class name (IRISReaderGUI and Com.Harmonysoft). First I manually rename the code, then compiled, the compiler gave me lot of error that I could not figure out. So I tried to rename my class and namespace using 'refactoring' menu. My code still didn't compile.
I did some research and change the 'IRISReaderGUI.Designer.cs' class name and namespace to match the new names. C# still didn't give me no joy.
The compiler erros message was :
'Com.Harmonysoft.IRISReaderGUI.Dispose(bool)': no suitable method
found to override'
On the designer view it said : The base class'System.object' can not be designed.
So I guess my IRISReaderGUI did not correctly inherit the System.Windows.Forms.Form class, this was confirmed by putting the mouse over the Form word in the code, Visual Net does not popup the text describing the class, and pressing F12 does not get me to the Form definition.
Here is the code :
"IRISReaderGUI.Designer.cs"
namespace Com.Harmonysoft
{
partial class IRISReaderGUI
{
.......
}
"IRISReaderGUI.cs"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using Com.StellmanGreene.CSVReader;
using System.Windows.Forms;
using System.IO;
namespace Com.Harmonysoft
{
public partial class IRISReaderGUI : Form
{
......
}
I am new to C# and Visual Studio, I have previously worked with Java compiler and Vim editor mostly. Could anyone please help me to compile my code ?
Forms in VS2005 and up are partial classes. You edited the one part but not the other. In the Solution Explorer window, expand the node next to the form and double-click the Designer.cs file to open it.
Using the Refactor + Rename context menu command is the better approach, it doesn't forget to edit the other source files as well. It didn't work when you tried it because the damage was already done, the parts no longer had the same name.