Commiting settings in Settings Tab (SettingsTabUserControl.vb\HomeServerSettingsExtender) RRS feed

  • Question

  • Hi, I've created a settings tab in my WHS Add-In project and would like to commit the changes. Now I could create a save button and have this do the actual commit but I would rather use the Apply\OK buttons.

    I'm slightly confused as to how I should do this - I have a text box that needs to be written to an ini file, I have a Sub called SetIniSetting() This takes the value and section (value from the text box, section hard coded) and writes it to the ini file. Now I did have a save button and used the _Click method to commit the changes, this obviously didn't check to see if the contents of the text box had been changed\updated, it just commited the changes regardless.

    I have this on my HomeServerSettingsExtender:
    Public Function Commit() As Boolean Implements Extensibility.ISettingsTab.Commit
            Return False
        End Function
    End Class
    and belive I need to implement : EnableSettingsApply() to enable the apply button, would I do this on textbox update?
    I'm fairly new to VB so sorry if this seems low level.

    So in summary I need to enable the Apply button when the text box contents is changed and pass the contents of the text box to the SetIniSetting Sub in SettingsTabUserControl.

    Wednesday, February 3, 2010 2:04 AM

All replies

  • TextBox's can raise an OnChange Event that you can use to see if fields in your settings dialog have changed.

    Saving settings can be done in a number of ways. 
    1) Write to the registry
    2) Write to an INI file
    3) Serailize an object with the settings to an xml file

    Though the later seems to be a bit complicated it is well used and actually quite straight forward - create a class with all your settings in, make it serializable and then save it to a file.

    Wednesday, February 3, 2010 2:21 AM
  • Al's suggestion to use the Textbox's TextChanged is correct. You can do this in the forms designer - select the textbox, open the Properties panel, change to the Events view (the lightning bolt icon) and then double-click in the TextChanged box. That will automagically create the event handler stub and subscribe to the event.

    Inside the event handler, you'll want to call:

    Which will light up the "Apply" button.

    When the user clicks Apply or OK, the Commit() method in your SettingsExtender gets run. That's where you should be doing your SetIniSetting() work, not in the control itself.

    There are a couple of issues with this, though.

    1) The TextChanged event gets fired when your textbox is first populated (when your tab first loads), so you can have the Apply button erroneously lit up. What you should do in the TextChanged event handler is to check if the value that's now in the textbox is the same as what you've got saved in the INI file - if it is the same, then you don't fire the EnableSettingsApply() method.

    2) Every time the Apply or OK button gets clicked, your Commit() method runs, regardless of whether or not your settings changed - WHS doesn't care, it just blindly calls Commit() on all settings tabs. You can avoid this if you want to by doing a check to see if some boolean has been set to true on the control - in your TextChanged event handler, if the value is actually changed, you can flip your SettingsHaveChanged boolean to true, and then check that value in Commit() to see if you really should save the INI file to disk.
    Tentacle Blog: http://www.tentaclesoftware.com/blog/
    WHS Disk Management: http://www.tentaclesoftware.com/WHSDiskManagement/
    Wednesday, February 3, 2010 4:06 AM
  • I'm fairly new to VB so sorry if this seems low level.

    Madlan, you might be interested in doing some computer based training to help you on your way to good programming practices.  Also I would suggest creating some regular Windows Forms apps before tackling a Windows Home Server Add-In to familarilse yourself more with how generic form controls work.

    I use a CBT program from Innerworkings.  They provide over 400 modules of C# and VB .net programming tasks and they give you an offer of 3 hours free:


    Also if you have just started learning VB.net I would suggest you might want to consider moving to C# as it seems to be used more and I think out of the two it has slightly clearly syntax (though I come from a C background so I would say that) - though I am setting myself up for a flame war here as to which is best.  At the end of the day it's your choice - if you are new to programming then VB.net might be easier for you but I think you gain a slight advantage in that there seem to be more professional C# programmers out there for support.  You might be interested in the following discussion:

    Wednesday, February 3, 2010 11:54 AM
  • Thanks Al,

    I've signed up for the trial - looks great. I was in the process of leardning VB, as I thought this would be more usefull in general, should I be switching to C# at this early stage then?

    Sam, Everything is working great as far as the comit method goes - only one small issue with enabling the Apply button.
    I get an Object reference not set to an instance of an object regardless of how I call the enablesettingsapply() function:

    Public Class SettingsTabUserControl
    Dim m_consoleServices As IConsoleServices
        Public Sub HostButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HostButton.TextChanged
            If UsernameButton.Text = GetIniSetting("1234", "misc") Then
            End If
        End Sub

    Wednesday, February 3, 2010 8:18 PM
  • You probably want
    If UsernameButton.Text = GetIniSetting("1234", "misc") Then
    to actually be
    If UsernameButton.Text <> GetIniSetting("1234", "misc") Then
    i.e. "if the text does not equal my stored value, then enable settings".

    As for your NullRefException, my guess is that you've got the same issue as your MainTabUserControl. Make sure your HomeServerSettingsExtender is using the constructor of the control to assign m_consoleServices a value, just like the other thread.

    I'd recommend switching to C#, and not just because it's what I know. Al is right that there are tons more examples of C# code available than VB.NET.
    Tentacle Blog: http://www.tentaclesoftware.com/blog/
    WHS Disk Management: http://www.tentaclesoftware.com/WHSDiskManagement/
    Thursday, February 4, 2010 1:17 AM