Using string resources from UserControl resource file - c#

I'm trying to add string resources to my UserControl. I have created a new UserControl and when I try to edit the automatically created mycontrol.resx file I'm met with this message:
You are trying to edit a resource file that is a poart of another project item (such as a form or a control). Editing this item could corrupt the project item, and you will have to recover it by hand. In addition, changes made to this resource file may be lost if further chagnes are made to the project item.
Do you really want to edit this file?
If I accept and continue, I can't access the strings through mycontrol.MyString unless I change the resource file access modifier to public (default is "no code generation") but then the project doesn't compile and shows these errors:
Missing partial modifier on declaration of type mycontrol; another partial declaration of this type exists
Type mycontrol already defines a member called '.ctor' with the same parameter types.
Thanks

Related

.NET 6 - WPF CustomControl Template not applied though being in App Resources

We are switching a huge WPF Appl. to .NET 6.0. At least one CustomControl which worked on 4.8 does not get it's template applied which is referenced via Generic.xaml. I am not sure if Generic.xaml is not loaded or something else needs to be considered.
The Style can be added manually to Application.Resources.MergedDictionaries via a simple "Add" call with the Source set to it. I can see the CustomControl Style afterwards with the Key being the correct Type. It is still not applied, as there is no visual representation and no call is made to the overriden OnApplyTemplate method.
If all else fails, can I apply a template manually if I have the given style, like just apply the Style manually to a newly created instance?
Also: the Projects are now SDK-Style, AssemblyInfo.cs was taken over and "generate assembly info" is set to false. It contains the standard ThemeInfo entry.
Just for clarification following the code which successfully finds the Generic.xaml. But before 6.0 Generic.xaml was loaded without doing anything.
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Contracts;component/Themes/Generic.xaml", UriKind.Relative)
});
Thank you all for your help!
I had the same Problem.
maybe u need to add the Assembly.cs at TopLevel of ur CustomControlLibrary
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

Derived class in UC gives "type exist in both dlls" error

User Control has a class ImButtonLink derived from Image button.
protected void reserv_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect(((ImButtonLink)sender).GetURL);
}
In Click Action function I get following error:
The type 'ImButtonLink' exists in both 'App_Web_carstable.ascx.6bb32623.yi5kff3g.dll' and 'App_Web_carstable.ascx.6bb32623.urge3_if.dll'
Any ideas? My derived class has only string parameter in addition to ImageButton class for storing navigation link. Maybe there's another way to store link in ImageButton?
I guess the control is CodeFile (and not Codebehind) mode, or included in a CodeFile page. You may transform it to CodeBehind and rebuild your solution.
If you want to keep it CodeFile, you may set batch="false" in the compilation element of your web.config like this :
<compilation defaultLanguage="c#" batch="false" ...>
Be aware that there are performance issues when a page is first accessed.
And don't forget to delete your temporary asp.net files (you may need to stop your web server)
You may also have a look at this, very interesting solution though I never had the opportunity to test it by now :
https://stackoverflow.com/a/15253750/1236044

Issue Related to Creating an Object of one User Control into Another

I have an asp.net Application i have added some .aspx pages in the root directory.. now i have created folder named usercontrols having following structure :
now i want to call a Method of one user control into another.. but i dont get the class name of any user controls in between..
i have used same namespace in all .ascx.cs file..
so how to call a Method of one user control into another ??
Thanks
You need a reference declaration to use the uc within your code:
Add it to your page:
<%# Reference Control ="~/WebUserControl1.ascx"%>

How do I cast an icon from a resource file to an image for use on a button?

I'm trying to use an icon that I've added as a resource as the image on a button. I know it's possible because I can do it in other projects through the designer. However, I'm trying to do this with code. I added the icon as a resource to my project by following the steps in the accepted answer to this question. The resource is named CancelButtonIcon.
Now, I'm trying to add that icon as the image on a standard button with this code:
this.CancelButton.Image = (System.Drawing.Image)Properties.Resources.CancelButtonIcon;
However, I get an error message:
Cannot convert type 'System.Drawing.Icon' to 'System.Drawing.Image'
In the code that Visual Studio automatically generates when I use the designer, it looks like this:
((System.Drawing.Image)(resources.GetObject("SaveButton.Image")));
which results from manually adding a resource through the Properties window. How can I convert this icon resource to an image so it can be used on the button? Adding it through the designer is not an option (this button is created programmatically and thus isn't present in the designer).
You can use the Icon.ToBitmap method for this purpose. Note that a Bitmap is an Image.
CancelButton.Image = Properties.Resources.CancelButtonIcon.ToBitmap();
Not sure why, but any time I tried using the accepted answer's approach, the .ToBitmap() call was giving me array index out of bounds exceptions. I solved this by doing it this way instead:
System.Drawing.Icon.FromHandle(Properties.Resources.CancelButtonIcon.Handle).ToBitmap();

asp.net Cannot create UserControl in class

My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.
The rending of HTML can be done using this example I believe:
How do I get the HTML output of a UserControl in .NET (C#)?
My Question:
I created my user control
public partial class Controls_MyUserControl : System.Web.UI.UserControl
I've registered it in my web.config
<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />
I can reference this on a page just fine
<UC:MyUserControl ID="MyUserControl1" runat="server" />
Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.
Controls_MyUserControl dummy = new Controls_MyUserControl();
The potentially strange thing is when I put this code into a code behind of a page, it works.
Any ideas on what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?
My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.
It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.
Thanks in advance
Some workaround as posted by Scott Alen http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-app_code-folder.html
Suppose you have all your user controls in ~/UserControls/
Then on your code behind add the following;
using YourSpaceName.UserControls;
This will set reference to any user control in that location.
Note: if you are using a user control within another, make sure to create a folder for each of them. I have experiencing problems, specifically with VB where it would compile but give me a runtime error. Since I encounter that problem I create my user controls each in it's own folder.

Categories