Visual Basic application store Settings in different paths - vb.net

need to ask, I have simple app, which had one textbox and one button. When I type something to that textbox and push that button, it saves to my Settings...
see:
Dim Settings As New My.MySettings
Settings.something = TextBox2.Text
Settings.Save()
MessageBox.Show(Settings.something.ToString)
Everything works great, but when I build it and run for example from my desktop path, Enter number save it and close it. When I make a copy of this file to my documents path and open it, nothing loaded, but when I start it from the place where I last save it, it loads properly.
In the short description:
If I run app on my Desktop path and make a save, when I close the app and copy this file (.exe) to another path, I need to load previous saved data from Desktop path, or other different location.
Save in path A, copy it from and open anywhere in path B, C, D... etc.
Is it possible, if yes, how can I do that?
Thanks!

You may be looking for something like the 'SaveSetting" method
SaveSetting("MyApp", "Form1", "Text", "ValueToSave")
The first, second and third parameters indicate a "Path" where your value will be saved, and the fourth is the value itself.
Then, you can load the saved value using the following method:
MyText = GetSetting("MyApp", "Form1", "Text", "DefaultValue")
Note that the first three parameters must be the same as used on the save method, and the fourth is the default value, in case no previous data has been saved.

Related

How to change the image displayed on a button at runtime in Visual Basic?

I am trying to change the Image attribute associated with a button when the button is clicked.
I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:
Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
Dim sprites As Object = My.Resources.ResourceManager
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
End Sub
But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.
I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.
Any help or advice would be a great help.
Thanks.
EDIT:
Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.
ORIGINAL:
There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:
boardButtonA2.Image = My.Resources.Black_Pawn
Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.
Private blackPawnImage As Image
Private Function GetBlackPawnImage() As Image
If blackPawnImage Is Nothing Then
blackPawnImage = My.Resources.Black_Pawn
End If
Return blackPawnImage
End Function
and then:
boardButtonA2.Image = GetBlackPawnImage()
Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.
EDIT:
If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.
(I am assuming the question is for WinForms, if it isn't I will remove this answer)
When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.
The image below is from one of my projects:
The name of the files on disk are:
CSV - Excel.png
GreenDot.png
RedDot.png
To fix your problem change the line:
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
to be:
boardButtonA2.Image = sprites.GetObject("Black_Pawn")
I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.
I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).

How can I save the textbox content to the application itself?

Simply put, for example, I want to save content of a textbox to the application itself.
I tried "My.Settings" but type of settings must be "user-scoped" & settings(string content of textbox) extracting to %appdata% folder(user.config).
I want store the data(confidential text) on the application without extracting it to anywhere.
On the other hand "application-scoped" settings are "Read-Only" and values of settings can not be change while type of settings "application-scoped". I want settings(string content of textbox) always changeable by the user of app.
TextFile in Resources is also read-only...I have to extract TextFile for changing the content of TextFile.
How can i do that?
You can use My.Computer.Filesystem.WriteAllText() & My.Computer.Filesystem.ReadAllText() to write/read files. And there's no better way.
But yeah, you can achieve your requirement by saving the data into Registry, i.e. if you want to save your any text data which is accessible anywhere from the device, it could really be helpful.
Note that your application will require to be ran as administrator for saving/reading registry values.
To save registry value:
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\something", "ValueName", "Value")
To access it:
My.Computer.Registry.GetValue("Same Location", "Same ValueName", Nothing) ' Note that "Nothing" is returned when value doesn't exists.
Hope it helps.

selectDialog with address bar instead of dropdown with Photoshop script

I'm writing a custom script for Photoshop to handle batch processing of images. I have two input folders and an output folder that I need to specify. Right now I'm using something like this to select my folders:
var inputFolder = Folder.selectDialog("Select a folder of images to process");
Because I'm working on a server with a pretty deep folder hierarchy, it can be a real pain to select through the drop-down menu that Photoshop presents to me in this dialog.
It would be so much easier to have a folder selection dialog with an address bar and quick access panel like this:
All other PS scripts that I've been digging around in use the Folder.selectDialog method to set file paths to a variable. Is there a reason for this? If not, then how can I instruct Photoshop the second style of folder navigation dialog?
It doesn't appear that Adobe supports this dialog as a folder selecting option.
There was a similar thread to this posted on the Adobe forums where a workaround was suggested:
https://forums.adobe.com/thread/1094128
The solution that was suggested is to use a saveDialog function instead of selectFolder. This gives you the folder dialog that we want, but comes with the downside of having to type a dummy name into the filename path. It also says "Save As" on the top of the dialog box, which is confusing.
Here's what was offered:
by lilsmokie on Nov 8, 2012 2:19 PM
var dskTop = Folder.desktop;
var dskPth = String(dskTop);
var newSpot = new File(dskPth+"/poop");
var selectedFolder = newSpot.saveDlg('Select Destination Folder');
var illFilePath = selectedFolder.path;
alert(illFilePath);
This opens the dialog at the desktop. Then put "poop" or whatever you
like in the text field. There the user can navigate to where ever.
When they it save illFilePath will have the folder path. Not perfect
but its close enough for me right now.
I've also discovered that I can set the starting location of the selectDialog by using selectDlg instead:
var outputFolder = Folder(app.activeDocument.path).selectDlg("Select a folder to output images to:");
This gives some control over the starting location so that the user doesn't have to click through a million dropdowns.
At the bottom of your first screenshot you can see the unput text area Folder: This PC. It works just like an address bar. You can type (or paste) something like \\server\work\folder\subfolder into this area and you get this folder ('subfolder' in this case) immediately.
On MacOS this dialog doesn't show the Folder: ... input area. But you can press Cmd-Shift-G and get the native system 'address bar' anytime.

Visual Basic: Saving, Naming, Loading

So, I'm making a dice-rolling interface. It has several textboxes where the player inputs their numbers, and then they just need to hit a button to roll the dice. Thing is, I wanted to be able to save several profiles for different players, since each player will have different configurations.
The videos and tutorials I've found are all about saving from a textbox onto a dropdown menu, or saving on the interface itself, meaning it's 1 exe per profile.
I'm a rookie, so I'm afraid I'm not explaining myself too well, and my code is likely to cause a few heart-attacks ^^''
How can I make a Save As function, where the player can name the file, save the values of each textbox that matters, and then have it load up the correct values? I'm attaching an image of the interface. Everything is working properly, except for the Save and Load buttons
enter image description here
A simple approach it's to save texbox values in the workbook cells before you save ( another sheet or where you need to ), for example :
cells(row1, column1)= texboxval1.text
cells(row2, column1)= texboxval2.text
And so on...
Then, you can call the save method :
Set NewBook = Workbooks.Add
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
NewBook.SaveAs Filename:=fName
You can add all this code to a button on click event or as you may need.
Hope it helps !

Inserting .DBF Files from different folder structure with their respective FilePath using SSIS?

Am working on creating SSIS Package the does the following, so far my SSIS Package works well to insert records from '.DBF' file from one folder location ONLY (Am using Data Flow Task onto the Control Flow and OLE DB Source/Destination to read a .DBF file and get it inserted). However, am not able to figure out how to make it works with 20 sites, for example: I have 20 '.DBF' files stored in the following structure:
1. E:\DBF Files\Site1\Data\records.DBF
2. E:\DBF Files\Site2\Data\records.DBF
3. E:\DBF Files\Site3\Data\records.DBF
4. E:\DBF Files\Site4\Data\records.DBF
and so on till Site20 (here file name remain the same but the content will differ from site to another).
I have table for 'records.dbf' file in my SQL Server in the following structure:
Records Table:
Product_Name Price FilePath FileName
---------------------------------------------------------------
now here, how can process these files from different sites to be stored in my records sql table with it respective FilePath using SSIS??
P.S File Names will always remain the same and the path will change "Like Site 1"
Grab a For Each Loop container for the SSIS Toolbox and drag it on to the Control Flow designer.
Double click the For Each container to bring up the editor, click the Collection tab from the left-hand menu
Leave the Enumerator set at Foreach Item Enumerator and click the Columns button in the lower right.
Click the Add button and just go with the defaults which should be Column 0 and String. Click OK to return to the previous dialog. In the new Column 0 column add your folder paths one per row.
Next click the Variable Mappings tab from the left-hand menu, for the drop-down menu in the Variable column select New Variable. Again go with the defaults except for the name which in my example I have called dbf_path.
Next click OK to complete the For Each. Now the difficult bit for me because I don't what tasks you currently have that are working for one file. Whatever it is you have working, select it with the mouse and then drag and drop it into the For Each container.
For each task or tasks that need to use the file path do the following:
double click it to edit, in the left-hand menu select Expressions, then click Expressions and then click the ellipsis.
From the Property drop-down in the Property Expression Editor you need to find the property that would hold the file path for this task. The names are not always obvious and can be called different things in different tasks. In my example I am using a File System Task which I think is the Source Property.
Click the ellipsis in the Expression column and add an expression that consists of your path variable concatenated a string that contains the name of your file. One thing to note is backslashes have special meanings so if your string contains any they must be escaped with a second backslash.
Now when you run the package the For Each will loop through each path you have defined and store it in your variable which tasks will reference.