Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, I'm getting this error:
"cannot convert from bool to string"


at this line:
pictureBox2.Visible = Settings.Default[true].ToString;


I want to make it so when the user press a button, another button becomes invisible and if he closes the app and runs it again it will be the same.

What I have tried:

I don't know how to fix it.‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎
Posted
Updated 23-Mar-20 4:19am
v2
Comments
Maciej Los 22-Mar-20 14:18pm    
Remove ToString() method!
Member 14778963 22-Mar-20 14:22pm    
it still gives me the same error. :(
any other fixes?
F-ES Sitecore 22-Mar-20 15:24pm    
What does "Settings.Default[true]" return?
gggustafson 22-Mar-20 15:47pm    
The Visible attribute is of type bool. The ...ToString is a string. Strings dop not directly convert to bools. Use one of the Convert methods to make the conversion. However if you want to make pictureBox2 visible, then just write pictureBox2.Visible = true;
gggustafson 22-Mar-20 15:49pm    
The Visible attribute is a bool. if you want the object to be visible just set the attribure to true as in pictureBox2.Visible = true;

Control.Visible Property (System.Windows.Forms) | Microsoft Docs[^] is a boolean value.
You are trying to assign it a string value, which the compiler refuses to do because that does not make sense.

The instruction Settings.Default[true] is incorrect. It should be something like Settings.Default.SettingName, with SettingName being the name of a boolean setting. And the ToString method surely doesn't belong here.

It is quite hard to tell you what to do, because your intent is not clear. What is the name of the setting you are trying to access? Maybe that will give you some clue about the way to use settings:
Application Settings - Windows Forms | Microsoft Docs[^]
 
Share this answer
 
Use the debugger and find out exactly what type (and value) Settings.Default[true] is and you can make a decision from there - you need a bool value to set the Visible property.

But ... it's not something I'd expect to see at all.
Normally, Settings would be used like this:
C#
bool value = Properties.Settings.Default.NameOfABooleanSettingThatIsStoredByTheSystemForYou;
So quite what your Settings class is doing is a bit ... odd.
 
Share this answer
 
Member 14778963 wrote:
I want to make it so when the user press a button, another button becomes invisible and if he closes the app and runs it again it will be the same.
You can take advantage of the application settings property binding:
- Have a boolean setting defined for the visibility of the button; name the setting "Button2Visible" for example.
- Select the button whose visibility to persist; in the properties panel, go to (ApplicationSettings)->(PropertyBinding) and click on the "..." button.
- Navigate to the "Visible" property and select the "Button2Visibility" setting.
- Click "OK".
Now, you can just use this simple code:
C#
private void Button1_Click(object sender, EventArgs e)
{
   Button2.Visible = false;
   Settings.Default.Save();
}

The visibility of Button2 will be automatically persisted across subsequent sessions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900