.resx form icon cascade updates - c#

Suppose that we have a Windows Form, Form1, for which we have set an icon. Visual Studio will store the icon in Form1.resx ($this.Icon).
Now we decide to localize the application into N languages, so we set Localizable to True, we pick the first language from the Language option, we translate the texts and we continue with the next language repeating the procedure (pick another and translate) up to N. The result will be N .resx files containing the $this.Icon entry with the original icon.
Then we realize we want to update the form icon, so we set Language to "(Default)" and we set the new icon. To our surprise, we discover that the N .resx files were not updated.
Do we have to update the N .resx files manually? Is there something like cascade updates? What would you do in this case to avoid updating N icons?

I just added code to my Program.Main to modify all solution .resx files to remove Form.Icon.
try
{
string solutionDirPath = #"path\to\solution";
string[] resxFilePaths = Directory.GetFiles(solutionDirPath, "*.resx", SearchOption.AllDirectories);
foreach (string resxFilePath in resxFilePaths)
{
XDocument xdoc = XDocument.Load(resxFilePath);
var iconElement = xdoc.Root.Elements("data").SingleOrDefault(el => (string)el.Attribute("name") == "$this.Icon");
if (iconElement != null)
{
iconElement.Remove();
xdoc.Save(resxFilePath);
}
}
}
catch (Exception ex)
{
}
finally
{
}
And my bin size reduced almost twice!
Also for all forms I just will use icon from my app executable
Icon.ExtractAssociatedIcon(Application.ExecutablePath)

Honestly there is no reason to have the same icon in every resx.
ResX files cascade based on language, from the most specific language resource to the least.
If you remove the icon from all your resx files excepting your default, that icon will always be used.
If you wanted a different icon for a particular language, you would simply add the different icon to that resx file.
You can see this easily yourself. Add a label to your form, and populate it based on the value of a key called 'test' (You can do this with a call to 'GetLocalResourceObject("test")' in your code).
Add the key-value pair 'test'-'Hello' to your resx file. Let's say your resx is called 'foo.resx'. In a language-specific file (such as 'foo.fr.resx') add 'test'-'Bonjour'. If you run your program in any non-French language, you will see 'Hello', but if you switch to French you will see 'Bonjour'.
It will work the same way with your icon - you can set it in the base resx, and it will show up for every language. You can then override it in a different resource file.

Related

How to create resx files

I am trying to use Icons in a windows form application. I read that you can use resx files to do this. (I also read that resx files can be used for localization but this is not the point of this question)
I know more or less how to use a resx file if I have one(see below). What I don't know and I can't find anywhere is how to create these resx files (I know what these files are already)
Can someone teach me how to create a resx file that holds information on icons non-programmatically (the few responses about this in SO are programmatical ones)
The objective is to have a "resource.resx" file in my project that holds data about a "myicon.ico" file.
I am planning to use this as in
ResourceManager rm = new ResourceManager("resource",
typeof(Resource).Assembly);
Object ret= rm.GetObject("something");
if(ret!=null && ret is Icon)
return (Icon)ret;
else
return null;
Please don't point me to this link since I have already read it and could not find a practical way to do what I asked.
Assuming you're using Visual Studio:
Right-click your project
Select Add | New Item
Select Resources File
Give it a name (e.g. Resources)
Click Add
You will now have a Resources file in your project with the name you provided, and it should auto-open the resources editor. If it doesn't, double-click it in the project.
In the top-left of the editor, click the "Strings v" drop down and select "Icons".
Drag your icon into this screen.
Rename it to whatever you want (e.g. something).
If you named your resources file "Resources", you should be able to access the icon like so:
Object ret = Resources.ResourceManager.GetObject("something");
if (ret != null && ret is Icon)
return (Icon)ret;
else
return null;

Making GTK# File Chooser to Select File Only

Im using GTK# FileChooserDialog widget.The problem is that even though the widget is named file chooser you can select folders with it and it returns folder names also.
Is there any way by which i can restrict it to choosing only files? I have checked almost class all properties i could not find any.
you can restrict the action by defining its Action-Property in the constructor
private void OpenOFD()
{
Gtk.FileChooserDialog filechooser =
new Gtk.FileChooserDialog("Choose the file to open",
this,
FileChooserAction.Open,
"Cancel",ResponseType.Cancel,
"Open",ResponseType.Accept);
if (filechooser.Run() == (int)ResponseType.Accept)
{
System.IO.FileStream file = System.IO.File.OpenRead(filechooser.Filename);
file.Close();
}
filechooser.Destroy();
}
There are 4 FolderChooserActions:
CreateFolder: Indicates a mode for creating a new folder. The chooser will let the user name an existing or new folder
Open: Will only pick an existing file
Save: Will pick an existing file or type in a new filename
SelectFolder: Pick an existring folder
According to the documentation, that behavior depends on the Action property:
If it is set to FileChooserAction.Open or FileChooserAction.Save, only files can selected.
If it is set to FileChooserAction.SelectFolder or FileChooserAction.CreateFolder, only folders can be selected.

Resource file code not generated

I have my default resource file Resources.resx for which visual studio nicely generates a designer.cs class, but when I try to create Resources.de-DE.resx, it does not generate.
I checked all the properties for both files are same.
It does generate for Resources1.resx, but not for Resources.de-DE.resx or Resources.en-US.resx.
hello #TRS you may be confused about that there is a Designer.cs present For Resources.resx and there should be someDesigner.cs for Resources.de-DE.resx but this is not the case because designer file would be same for all the resource files. and also the property that is created in Designer.cs is also common that means you will use this property for every conversion so the difference that you can make is on the basis of ResourceCulture.
Yes, Designer resx file remains empty.
you should follow the below steps, it will work 100%.
1) Create Resource.resx file under any folder
2) set CustomTools = PublicResXFileCodeGenerator and
Build Action = Embedded Resource in Resource.resx file property.
3) Now create the different language files like below,
Resources.en-US.resx
Resources.nl-NL.resx etc
4)If MVC, in your MVC Model refer this namespace as project.folder
5) change the one of the property as below (example)
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}"), Display(Name = "dtMovinDate",ResourceType = typeof(Resources.Resource))]
public DateTime dtMovinDate { get; set; }
6)
In config file add this under System.web
<globalization enableClientBasedCulture="true" culture="en-US" uiCulture="en-US" />
7) rebuild the project and test it.
It will work. The real reason of designer.cs file empty is resource designer.cs will refer these files based on your web.config file.
Interestingly, I had similar situation today. I will share the scenario and solution here because I think it's related to your question.
I wanted to have a resource file for Arabic culture only, SharedResources.ar.resx, and I don't have another resource called SharedResources.resx.
The solution was to create the resource file with single [dot] in it, then rename it.
For example, in my case, I created the file with this name, SharedResources-ar.resx. Then double click and change its access modifier to public using the editor, like this:
This will generate .cs file with the correct static properties.
After that, rename the file to the correct format. SharedResources.ar.resx. Otherwise it will not be generated!
I don't know if it's a good solution, but solve my problem and hope to solve someone's else too.

Load from text file at program startup

Sorry for the likely noobish question, just starting to learn c#, and couldn't find anything that worked.
I'm making a text editor in c#, and so far it can open and save text files from inside the program with dialogs, but how can I make it load the text from a file that I open in windows explorer, outside of the editor, with the editor
Basically, I can already read from text files opened inside the editor, but how can i make it so that if I open a text file (and have the default program for opening text files set to my editor), it'll read it?
I saw something about getting the filename somehow and passing it as an argument, if that helps.
If I understood you correctly, you want to pass the filename/names as command line arguments ?
If you look at the Main, which starts the program you can see that it will store parameters in a string[] (string array) so if you pass arguments you can just check the args[] inside the program to get the parameters you sent in. Please ask more if you need more help !
UPDATED
As per your request if you open a file from windows explorer it will send the path of the file it to the Main method. So lets say you right click a file and choose to open it with your text editor. You have to use the path as I do below, and read the file's content. Then you can do whatever you want with the content.
class TestClass {
static void Main(string[] args) {
// Now you have all arguments in the string array
if (args.Length != 0) {
string pathToTextfile = args[0];
}
StreamReader textFile = new StreamReader(pathToTextfile);
string fileContents = textFile.ReadToEnd();
textFile.Close();
}
}
So you have a text editor coded in C#, and you want to be able to open a text file through double clicking on the file in Windows explorer. If so, basically 2 steps:
1. Your editor program must accept one argument as the file name. Carl had already given an example.
2. You need to associate *.txt files with your text editor. This could be done through editing Windows registry. please check What registry keys are responsible for file extension association
You can use the OpenFileDialog class to select a file to show in your program.

OPEN a Resource.resx file instead of Creating it which overrides the previous Resource.resx file

I start my application from withint Visual Studio 2010.
I add then some files into my application and each file type`s icon like icon from doc,docx,xls,pdf etc are added as String/Bitmap key/value pair to my IconImages.Resx file via
private void DumpTempResourceToRealResourceFile(IDictionary<String, Bitmap> tempResource)
{
using (ResXResourceWriter writer = new ResXResourceWriter("IconImages.Resx"))
{
foreach (KeyValuePair<String,Bitmap> item in tempResource)
{
writer.AddResource(item.Key, item.Value);
}
writer.Generate();
}
}
When the icons are added to the resource I close the application.
Then I start my application again with VS 2010 and add some files within my document application. The file types are written again to my IconImages.Resx.
Then I close my application and check the IconImages.Resx file under the \bin\ folder and the previous saved images are gone and I have new/different ones now.
Why can I not say OPEN a .resx file and append stuff to it? Everytime I create a ResourceWriter object with the same name "IconImages.Resx" I overwrite the previous added stuff and thats stupid.
How can my IconImages.Resx file stay alive over an application session without being overwritten by other stuff I add?
I haven't used ResXResourceWriter, but usually *Writer classes simply write a data file from scratch.
If you want to "append" new data you would typically have to use a *Reader class to deserialise the existing data into memory, then merge/add in any new data you wish to, and use a *Writer object to then write the resulting data back out. Take a look at ResXResourceReader to see if it supports what you need to do this.
I am having now a lookup table "FiletypeImage" with the filetype ".docx" and the raw binary data aka blob. This table gets retrieved in my documentService and cached in a static variable. with a Get and Add method which are called by my DocumentListViewModel. Its very fast thx to sqlite :)

Categories