Image button on visual studio 2010

拥有回忆 提交于 2020-01-04 11:42:45

问题


I am trying to create a button with image and no text from the [Design] view on Visual Studio 2010. I drag & dropped the button, then I click the Image Property, I select a png image from my resource file, but the property always remain empty and the button doesn't have image neither on the [Design] view or in the compiled program.

I tried setting the image on the Form.Designer.cs file with:

    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(73, 11);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(39, 34);
    this.button1.TabIndex = 10;
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Image = Properties.Resources.close_project_img;

And when I go to the [Design] View, I get the error :

To prevent possible data loss before loading the designer, the following errors must be resolved: 

Projectitem unavailable.     

Instances of this error (1)  

1.   Show Call Stack  

at EnvDTE.ProjectItem.get_FileCount()
at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.GetFileNameForProjectItem(ProjectItem item)
at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.BuildType()
at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.GetObjectType()
at Microsoft.VisualStudio.Shell.Design.GlobalType.get_ObjectType()
at Microsoft.VisualStudio.Shell.Design.GlobalObject.GetHashCode()
at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GetGlobalObjects(Type baseType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetTypeFromGlobalObjects(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetRuntimeType(String typeName)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetType(String typeName)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetType(String typeName)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyReferenceExpression(IDesignerSerializationManager manager, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)  

Help with this error  

Could not find an associated help topic for this error.

I can click "ignore and continue" and the [Design] view opens fine, but the button doesn't show the image on that view (it does show it in the compiled program).

What am I doing wrong here?


回答1:


you should not edit the Form.Designer.cs directly because it will be overwritten by the designer. the designer actually runs the code in the Form.designer.cs file when you open the designer. since that code is actually run by vs2010, its environment is a little diffrent

just a test, try setting the image property from the button_click event method instead, it might be that the image is not found for some reason




回答2:


I closed the project, removed the resources.resx and resource.designer.cs, recreated the resources files and it works fine now, not sure what was the problem.




回答3:


Restart Visual Studio .NET

I had the same issue happen to me. The call stack doesn't lead to or show any information that is helpful to tracking down the issue in the designer file. I simply closed Visual Studio 2017 and then reopened the project. Problem fixed itself.



来源:https://stackoverflow.com/questions/5090435/image-button-on-visual-studio-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!