YellowShadow

I setup a Settings String called Hompage.

When the application starts it navigates the web browser to the string I put in the HomePage settings. I set this up to Users so I could allow the user to change this.

In the options dialog I added a textbox and a button.

I want it to save whatever the user enters in the Textbox to the HomePage settings.

This is the code I have:

Code Snippet

Explore.Properties.Settings.Default.HomePage.Replace(textBox1.Text);


but I get an compile error:

Error 1 No overload for method 'Replace' takes '1' arguments C:\code\Explore\Explore\OptionsForm.cs 20 13 Explore

Can anyone help me fix this



Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

Would I be correct in saying that the method "Replace" is a string method If so, the Replace method takes 2 parameters, the old value and new value.

so you need the old value as the first parameter, and the new value as the second parameter - you will find the parameters/method overloads if you bring up intellisense and highlight the method and it should come up with a tooltip explaining the method and each of the parameters, press the cursor button (down/up) or press the keyboard cursor (up or down) to move between the methods and choose the one that you think best fits for what you are trying to do.

So....

.......HomePage.Replace("oldValue", this.TextBox1.Text);

Does this help in any way






Re: Visual C# Express Edition Help with My.Settings

YellowShadow

Thanks! But what if the user keeps chaning the homepage for example it went from google.com to yahoo.com. How would the application know that yahoo.com is the new old value





Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

Well you would need to read the old value first. Example:

If the old value stored in the property was "yahoo.com" and you wanted to replace it with "microsoft.com" then:

string oldValue = Explore.Properties.Settings.Default.HomePage;

Explore.Properties.Settings.Default.HomePage.Replace(oldValue, this.textBox1.Text);

or a one liner:

Explore.Properties.Settings.Default.HomePage.Replace(Explore.Properties.Settings.Default.HomePage, this.textBox1.Text);






Re: Visual C# Express Edition Help with My.Settings

YellowShadow

For some reason it doesn't work. When I press home it still takes me to the old homepage. How can I check it if actually changes the setting




Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

Did you forget to Save the setting

To check the value, simply open up the config file in notepad or within VS.NET or place a breakpoint on the line of code that is reading/writing to the file then hover over the property to see what the value is of that property, before and after that line of code.






Re: Visual C# Express Edition Help with My.Settings

YellowShadow

I don't think it saves properly.

Isn't this the save code

Code Snippet

Explore.Properties.Settings.Default.Save():







Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

yes it is.

This works for me....

Properties.Settings.Default.MyProperty = "bob";

Properties.Settings.Default.Save();

next time I load, I see "bob" and I can change it to say "Sandler" and it save it, it saves.






Re: Visual C# Express Edition Help with My.Settings

YellowShadow

Thanks! Now it works perfectly Smile





Re: Visual C# Express Edition Help with My.Settings

YellowShadow

One more quick question, how would I access the webBrowser on the MainForm from the OptionsForm

I tried to do MainForm.webBrowser but IntellSense didn't list the webBrowser.





Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

you need to expose it publically and have the same instance of that form.

Why would you want to do this

The best thing to do if you need this is to expose the webbrowser control as a public property on the main form, pass the main form instance or even better, just the webbrowser control instance to the optionsForm and do what you need to do. Example:

//main form:

private void DoShowOptionsForm()

{

FrmOptions optionsForm = new FrmOptions(this.webBrowser);

optionsForm.ShowDialog();

}

//FrmOptions:

private WebBrowser browser = null;

public FrmOptions(WebBrowser browserControl)

{

this.browser = browserControl;

//now you can access the browser private variable in this class from any where within this class.

}






Re: Visual C# Express Edition Help with My.Settings

YellowShadow

I don't want it to show. I have a button on the options form that says Use Current page and I want it to get the current page that is displayed in the web browser control and display the URL in the textbox.





Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

ok, in that case same thing as above except pass it a string, the string being the url navigated to. Example:

//main form:

private void DoShowOptions()

{

FrmOptions optionsForm = new FrmOptions(this.webBrowserControl.Url.ToString();

optonsForm.ShowDialog(); //showing the options form.

}

//FrmOptions:

private string currentUrl = String.Empty;

public FrmOptions(string currentPageUrl)

{

this.currentUrl = currentPageUrl;

}

private void DoSaveHomePage()

{

//save homepage. The homepage being the current url the webbrowser control is navigated to. This is now being passed from the main form into this form via constructor so save this.currentUrl variable in this class (form).

Properties.Settings.Default.HomePage = this.currentUrl;

Properties.Settings.Default.Save();

}






Re: Visual C# Express Edition Help with My.Settings

YellowShadow

It doesn't show my options form, it shows a blank form. Why is that





Re: Visual C# Express Edition Help with My.Settings

ahmedilyas

FrmOptions is just a form name I made, replace it with your options form. Is there an options form where this button is to set the current page as a home page - use that form :-)