VB.Net How to add a String to Settings [duplicate] - vb.net

How to add a new name in My.Settings?
So far, I've learned how to read and edit values in it, but how can I add another row/name at run-time?
Something like:
My.Settings.AddName.String("foo1")
My.Settings.Save()
My.Settings.foo1 = "fooNew"
I can't see such on the web.. Thanks
EDIT: will be used for the name,text and location of dynamically added buttons.
SeeThisLink

The My.Settings application settings are compiled into your assembly. The properties cannot be altered at runtime because of this. Expecting new settings to be available at runtime through the My.Settings property will not be possible. You would need to resort to more rudimentary techniques like those highlighted by this MSDN question.

You cannot do it at runtime with whats built into my.settings namespace, however its entirely possible. Id love to take credit for this but it has already been answered on stackoverflow.
As long as you know the file path and node name, you can build your own xml parser class to do whatever you want using this very simple example I couldn't write any more clearly:
How to programatically add nodes at certain locations in xml config file

Related

syntax editor - how to write dynamic language xml definition file?

So I want to create my own programming language in vb.net 2013 and I see SyntaxEditor by actipro software fits, but how do I write xml definition file that tells which words how to be colored? is there a tutorial somewhere?
Also, how do I make the syntax editor to display the number of the line?
SCREENSHOOT OF MY PROGRAM
Showing the line number is just a property of the SyntaxEditor-control: in WPF-version it is "IsLineNumberMarginVisible". you have to set it to "true". I assume the property in WinForms-version has the same name.
when you install Actipro then there are a lot of samples included. Just look for files named *.langdef and you will find a lot of files where you can see how they shall look like.
There is also a tool "LanguageDesigner.exe" delivered with SyntaxEditor. It gives you a nice UI to create and edit the language file.
Generally I recommend to take a look at the sample projects, specially the SampleBrowser-Project with the "GettingStarted01" to "GettingStarted15" controls.

VB.NET Localization of strings

I've been successful in using form.location.resx files to localize the strings associated with form controls. However, I don't see how to (safely!) add strings to the .resx file(s) and then access them for such things as message boxes.
If I try to add a string to the .resx file using Visual Studio (2017), I get a warning dialog pop-up telling me that this could corrupt the "project item" (form) or my changes could be lost if I change the associated form.
Also, if I use this method, would I need to manually add my strings to each language file separately?
Alternatively, I could create a bunch of Labels to the form with visible=false and then edit their strings in the .resx files and use something like:
msgbox(LabelSampleMessage.text,MsgBoxStyle.Information,LabelSampleMessage2.text)
But that seems like it would be massively inefficient.
For Windows Forms, the best way to do this is to go into the Designer and set the Language property on the form. This will create a formName.Designer.languagecode.resx file for you. Then you just edit the Text properties of the controls. The changes will go to the resx file for the language you're editing. You can even have different layout and control spacing for each language (useful if a label is short in English but translates to something long in German).
For MessageBox messages, you do the same thing with the Resources.resx file. Just put all the messages in your Resources and duplicate the resx file for each language. Then use the Resource editor to translate the message in the other languages. You can then look up the message using My.Resources.Default.SomeMessageKey
#David: Note that I'm the author of a commercial ".resx" localization program for VS (in the interest of full disclosure). Yes, dwilliss is correct, though the names he used are a little off. For Windows forms, you can't manually update the ".resx" files yourself (directly), hence the warning from VS (don't know why they did things this way - go figure). You won't get that warning for strings you put in "Resources.resx" however. Just manually add "Resources.[LangCode].resx" and update it on your own. You have to add the keys yourself (again, no warnings). You can then simply access each string via the static properties seen in the "code-behind" file "Resources.Designer.cs" (one static property exists for each string you add). These are what MSFT calls "strongly typed" resources. If you access, say, "YourApp.Properties.Resources.SomeMessage" for instance (from "Resources.Designer.cs"), it will be returned in whatever language is currently set in "System.Threading.Thread.CurrentThread.CurrentUICulture" (assuming that language's satellite assembly is installed of course). This is all a pain and highly error-prone for other reasons as well, hence the reason I wrote my program (shameless plug but MSFT's way does work, though it's very primitive for handling translation in general - a professional organization relying on an external translator will save a lot of problems and $ in the long run by using a 3rd-party translation program, not mine necessarily though it would be my tool of choice of course, but anything is better than handling it manually).

Is there a Cut & Paste as New Class File (Macro)?

I'm looking for a free Visual Studio feature, extension or macro. that can help with the following situation.
When I prototype I tend to keep all my classes in one file (bad practice I know, but yeah it a prototype). Then comes the point the where the files is too hard to navigate. So I breakout the classes into separate files inside the project, the folder structure reflecting the namespaces.
To achieve the is;-
1. Add new Folder
2. Add new Class
3. Name class
4. Cut and paste corresponding section into new class file.
For me, Steps 2 through 4 are prime fodder for a new Menu entries.
Cut Class as New Class File
Cut as New Partial Class File.
I've seen this feature in C# but not VB.net.
So does know any how to achieve this for VB.net?
Here's a macro that does what you want in C#... looking at the code it's probably fairly straight forward to modify it to work in VB...
http://plisky.net/main/macros/documentation
Also, I'm pretty sure all the commercial refactoring tools (Resharper, CodeRush, etc.) support this...
Resharper can do this using Move Type to Another File or Namespace
I just stumbled across this and can point you to an updated version of the macro that scrappy kindly linked. Its at http://plisky.net/main/plisy.net-visual-studio-productivity-macros.
If you still want it and wish to test it for VB I can happily make the changes to support VB.net but as I don't use VB I'd need a tester :) As its a while since this post you probably have something working already though.

VB.NET combobox persistence?

I'm fairly new to VB.NET, and I've mainly been doing ASP programming up 'til now, and I have a pretty simple question.
I'm creating a program that will copy a selected file to a selected directory, and I want to store recent files/dirs so that they can be selected from a combo box. I was planning to just create a settings with "files" and "dirs", and just store the strings as | separated values (since that's an illegal file character).
Is there anything wrong with this approach, or are there any better methods?
I think your approach is fine as it seems to be simply a local cache of recent directories. You can persist the data in the application at the module level(create a module with a public object essentially is a global variable) but it goes away when the application is terminated.
This article is using a similar approach to what you were thinking although the example is in C#

using my own config file in my application

As a practice exercise at my college we have to make a simple room booking system, complete with its own config file. We're not allowed to use the one built into VB.NET (the professor wants us to adapt to not relying on things like that) so I've made my own. This is a sample:
// Config file.
// First column is the variable name that will be used to
// reference the value in the second column. Seperate each
// setting with a new line.
MasterUser Chris
DatabasePath C:\Users\Chris\Desktop\Project.mdb
Comments in the file are pretty self-explanatory. I can parse the file fine, but what I'm having trouble with is making a variable the same name as whats in the first column. For example I need to make a variable called MasterUser and one called DatabasePath that holds Chris and C:\Users\Chris\Desktop\Project.mdb as values respectively. But I have no idea how to make a variable called that.
Any help would be cool, thanks. :)
EDIT: You can't see it from the code view here, but the variable name and value in the config file are separated by a tab :)
You might want to consider using a better format for your configuration file, like XML. This will give you a lot more library support for parsing and searching within the file... that said, in this simple case, it looks like you just need a simple Dictionary(Of String, String) which probably won't make a lot of sense if you haven't learned about Generics yet :)
Basically you can load values in like this:
Dim settings As New Dictionary(Of String, String)
settings.Add("MasterUser", "Chris")
settings.Add("DatabasePath", "C:\Users\Chris\Desktop\Project.mdb")
Then when you want to retrieve a value based on the key in the dictionary, you can do it like this:
Dim master As String = settings("MasterUser") ' master = "Chris"
I hope that helps clear things up.
Well, in the strict sense, you cannot "Make a variable named from file data" in most compiled languages. What you could do is to make a name-value association set (that's a Dictionary in .Net) and add entries to it based on what you read from a file.
However, what most programmers do is to "invert" that logic: as you code reads each line, it just does a series of IF's on the Name to see if it is recognized, and if so then just assings it to the corresponding predefined variable of the same name.
There are formats & libraries specially made for this (like XML, that's why the built in configs use it), but that might still be the kind of thing that you are not supposed to rely on.
Since your configuration file is not compiled into your code in any meaningful way, you aren't going to be able to create a variable (that is, a strongly-typed property or field on a class) dynamically. You have a couple of options.
If you want to keep your configuration file format:
Create a class that contains all possible configuration keys as properties. Give that class a Load() method that knows how to parse a file into the class.
Use a Dictionary -- either the Generics version or good old fashioned System.Collections.Dictionary. You'll retrieve elements with string keys.
If you're willing to change your configuration file format to XML, then you can use Serialization. This frees you from having to maintain a parser, but it requires a little bit more setup.
See here for my answer to another SO's question using nini and where to download it from. Essentially nini is a simple INI read/write library for configuration files. The question that remains to be seen is will your professor allow this inclusion of 3rd party open source?
Hope this helps,
Best regards,
Tom.