Sergei Drew
Sergei Drew

Reputation: 15

Converting a path to a string?

I've got a button that saves the path to a folder as the text in a text box and I want to save it to the settings file in c# but I keep getting the error:

Cannot implicitly convert type "System.windows.type.textbox" to String

Now the code I'm using to set the text box as the path is:

folderBrowserDialog1.ShowDialog();
textBox1.Text = folderBrowserDialog1.SelectedPath;

And the code I'm using to save it to the settings file is:

string pathh = textBox1;
Properties.Settings.Default.Path = textBox1;

Even if I replace

Properties.Settings.Default.Path = textBox1
Properties.Settings.Default.Path = pathh

I get the same error. Can some one tell me how to fix this please?

Upvotes: 0

Views: 183

Answers (2)

Rahul
Rahul

Reputation: 77876

Use the Text property of TextBox control class which is of String type like below. What you are trying is assigning a control instance to string and so receiving the error cause there are not same type.

string pathh = textBox1.Text; 

Upvotes: 2

Ronan Thibaudau
Ronan Thibaudau

Reputation: 3603

Your path is not the textbox but the TEXT of the textbox, so change

Properties.Settings.Default.Path = textBox1;

to

Properties.Settings.Default.Path = textBox1.Text;

Upvotes: 3

Related Questions