ToolBoxBitmap Attribute does not work in my component - c#

I have a Windows Form application. I've added a class to my project named MyLibrary.cs and I inherited this class of System.ComponentModel.Component and I've also added a bitmap image with its build action set to EmbeddedResource to my resources folder, but when I write the code below and build the project my customized icon does not change when I want to drag and drop my component to my Form. Would you please help me?
I've checked a lot of links like http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute.aspx and How to find the elusive ToolboxBitmap icon but none of them worked for me!
Here is my code:
MyLibrary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
internal class resfinder
{
}
namespace MyForm
{
[System.Drawing.ToolboxBitmap(typeof(resfinder),"MyForm.Bitmap1.bmp")]
public class MyLibrary:System.ComponentModel.Component
{
}
}

You should look here it says:
The bitmap must be 16x16
If you specify both a Type and a string, the control searches for an image resource with the name specified by the string parameter in the assembly containing the type specified by the Type parameter
So make sure your bitmap is located in the same assembly as "resfinder" and also put make sure in properties under build actions it is set as an Embedded Resource

Related

Change imported image settings via script

I am trying to make an editor script to set the import settings for my images. I don't want to do this manual since I need to import hundreds of images.
So I want to set edit the default import settings.
I tried the following:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[InitializeOnLoad]
public class EditorSettings : Editor {
private static TextureImporter CustomImporter;
static EditorSettings()
{
CustomImporter.npotScale.None; // see below for error.
}
}
the error I get is the following:
Member 'TextureImporterNPOTScale.None' cannot be accessed with an instance reference; qualify it with a type name instead
How do I do this? (it has something to do with how unity lets me acces the properties.)
And is this even the correct way to change the import settings for images?
Let me know if anything is unclear so I can clarify.
How do I do this? And is this even the correct way to change the
import settings for images?
No. This is not how to change the import settings of images. To change the settings of an imported image, you have to create an Editor script that derives from AssetPostprocessor then change the image settings in the OnPostprocessTexture function which will be called when the image has finished importing. The image is changed with the TextureImporter class.
public class PostprocessImages : AssetPostprocessor
{
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
}
}

Namespace could not be found

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!

The type or namespace name could not be found is shown

I have created sample application as layers.
I have a layer which is under DataAccess namespace
using System;
using System.Data;
using DataAccess.DataAccessLayer; //This line is showing error
namespace AdapterClass
{
public class SchemaAdapter
{
}
}
Inside a file, intellisense of current namespace only available.
For ex:
In above example, intellisense of AdapterClass only available.
Be careful with your terms. A namespace is different than an assembly. If your data access class (and namespace) is in another project (say class library) then it is in a different assembly that needs to be referenced. To do that, you can:
Right click the project you shown the code above and right click References => Add Reference => Solution Tab => Click that project

Take String from Clipboard? No assembly reference?

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();

Cannot inherit System.Window.Forms.Form class after manually change class name

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.

Categories