VB.Net - Changing a Label's Text Property

  • I have a VB program where I am trying to change the value of a Label's text property. I'm persisting user settings, like a user name.

    The Label is named ProgramTitle. The current text says "Brandie's Cute Program". I have a variable set up under Project -> Properties -> Settings called userprefUserName which is set up as a type String and a scope User. The value is ScoobyDoo.

    Eventually (when I figure out how to do it), this value will be set by the user in a menu called Preferences. Before I do that, I want to test the "easy" part, but having the label text change from "Brandie's Cute Program" to "ScoobyDoo's Cute Program".

    Here's what I have so far:

    Dim UserNameTitle As String = My.Settings.userprefUserName

    ProgramTitle.Text = UserNameTitle + "'s Cute Program"

    It should work, but VS 2012 is giving me an error of "Declaration expected".

    I know I'm missing something, but I have no clue what... Any ideas?

    Also, if (after the above question is answered) anyone can give me hints on how to connect up the values in the Project -> Properties -> Settings to the menu menu so I can change the values in the menu, I would greatly appreciate it.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • GAH. Let's try this again. The label text issue was caused by me putting the text update in the wrong part of the program. I put it under Public Class instead of under the Form Load sub. My bad. This is fixed.

    So to the second issue, anyone have any thoughts, links, etc. on how to change the values in the Project -> Properties -> Settings via a Preferences menu? I have the menu created. I just need to code it so it's linked up to these persisted settings.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Personally I use the appSettings section of the app.config and have a standard form I use to add to projects to show and edit the settings and restart the app if necessary.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • You can use the My.Setting object,

    Set value : My.Settings("UserName") = varUserName

    Save: : My Settings.Save

    See https://msdn.microsoft.com/en-us/library/saa62613.aspx

    Louis.

  • Thank you, Louis. That's my next project now that I've shaken all the bugs out of the basic form.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • If you have any other questions, please ask, I might give you an answer.

    For the last 8 years I've been developing a ERP system in VB.Net and SQLServer.

    Louis.

  • Okay, I still seem to be missing the essential part where I click on the menu and it pops up with the text box to fill in. I can find plenty of articles on how to create a FILE menu with Open, Close, Exit, etc. What I can't seem to find is how to create a user preferences or options menu with editable text fields.

    Any links or advice on what I should be googling?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Hi Brandie,

    You can add a ToolStripTextBox to the menu and bind it's Text property to an application setting, if that is what you mean?

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • mister.magoo (2/26/2015)


    Hi Brandie,

    You can add a ToolStripTextBox to the menu and bind it's Text property to an application setting, if that is what you mean?

    GAH! That's exactly what I was missing. I thought that stuff to the right was a submenu kind of thing. Let me play with that and see how far I get now.

    Thanks.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Brandie Tarvin (2/27/2015)


    mister.magoo (2/26/2015)


    Hi Brandie,

    You can add a ToolStripTextBox to the menu and bind it's Text property to an application setting, if that is what you mean?

    GAH! That's exactly what I was missing. I thought that stuff to the right was a submenu kind of thing. Let me play with that and see how far I get now.

    Thanks.

    Hrm. No go. The application binding is prevented me from changing the application properties. In fact, it's using the binding to override whatever I put in the text box. I want the data to go the other way around.

    Darnit.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • So I can get the menu to open up and the values to pass to the application settings, but I can't seem to make the form update its values when I make the choice. I put the binding on the name and the text properties. This seems to update the application settings.

    The RandomInt form textbox only updates, however, after I click on the form (which closes the menu) and then reopen the menu, navigating to that menu option and clicking in the menu option's box.

    The UserName label on changes after I close the menu and click on the label in the form. (There's something wrong with that).

    I also cannot get the menu to close once I've made my entry / choice. I have to click on the form to get the menu to disappear.

    Thoughts?

    Here's examples from a generic form I just built.

    Public Class Form1

    Private Sub RandomIntDisplay_TextChanged(sender As Object, e As EventArgs) Handles RandomIntDisplay.TextChanged

    End Sub

    Private Sub RandomIntComboBox_Click(sender As Object, e As EventArgs) Handles RandomIntComboBox.Click

    Dim BreakTime As Int32 = 1

    If Int32.TryParse(RandomIntComboBox.Text, BreakTime) Then

    My.Settings.RandomInt = Convert.ToInt32(RandomIntComboBox.Text)

    My.Settings.Save()

    RandomIntDisplay.Text = Convert.ToString(RandomIntComboBox.Text)

    End If

    End Sub

    Private Sub UserNameLabel_Click(sender As Object, e As EventArgs) Handles UserNameLabel.Click

    My.Settings.UserName = UserNameEntry.Text

    My.Settings.Save()

    UserNameLabel.Text = UserNameEntry.Text

    End Sub

    End Class

    RandomInt is set up as a combo box (dropdownlist) with a collection of values from 5 to 30 (5,10,15,20,25,30). UserName is set up as a text box. The idea is that UserName changes the text of a label on the form (from UserName to whatever I enter in the MyMenu UserName) while RandomInt updates a read-only text box on the form (From 5 to whatever value I choose).

    Pictures of my application settings and the form are attached.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • RandomInt is set up as a combo box (dropdownlist)

    You should use SelectedIndexChanged event to monitor when the user selects a value from a combo box and in your case use the value selected to populate the label/textbox and close the menu using the Close method for the menu in question.

    TextChanged events are only fired when the control loses focus (that is why you only update then you click away from the control or close the form.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • David Burrows (2/27/2015)


    RandomInt is set up as a combo box (dropdownlist)

    You should use SelectedIndexChanged event to monitor when the user selects a value from a combo box and in your case use the value selected to populate the label/textbox and close the menu using the Close method for the menu in question.

    TextChanged events are only fired when the control loses focus (that is why you only update then you click away from the control or close the form.

    Interesting. Thank you. I'm going to play with the SelectedIndexChanged event now.

    BTW, the only Close method I've been able to find is the Me.Close() which closes the entire form. Every time I try to use the close method on any containers, it gives me syntax errors.

    I did just discover something else, though. The below code makes changes in the label as I type them in preferences, plus makes the changes to the random int when I hit enter. And by adding a new menu item called "Exit Menu," I can get around the stupid need to click the form. I'm sure this is a very cludgy way to do all this, though.

    Private Sub UserNameToolStripMenuItem_KeyDown(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.KeyEventArgs) _

    Handles UserNameToolStripMenuItem.KeyDown

    If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then

    UserNameToolStripMenuItem.HideDropDown()

    End If

    End Sub

    Private Sub RandomIntToolStripMenuItem_KeyDown(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.KeyEventArgs) _

    Handles RandomIntToolStripMenuItem.KeyDown

    If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then

    Dim BreakTime As Int32 = 1

    If Int32.TryParse(RandomIntToolStripMenuItem.Text, BreakTime) Then

    My.Settings.RandomInt = RandomIntToolStripMenuItem.Text

    My.Settings.Save()

    Else

    MsgBox("Whoops")

    End If

    RandomIntToolStripMenuItem.HideDropDown()

    End If

    End Sub

    Private Sub ExitMenuToolStripMenuItem_Click(sender As Object, e As EventArgs) _

    Handles ExitMenuToolStripMenuItem.Click

    MenuStrip1.SendToBack()

    End Sub

    End Class

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • GAH. So frickin' close to the end of this and now I have 2 menu items that keep "sticking" to old values that I'm no longer using.

    For instance, on the UserName, I cleared it in the Application Settings, made sure its text was bound to the Application setting userprefUserName and every time I debug the form, instead of coming up with a blank text box like it does in Design mode, it comes up with "Me" (which was my original test value).

    I cannot get the damn thing clear, no matter what I do. And it's not populating from the code. I don't actually set the value to "Me" anywhere.

    The same thing with the Random Int. It keeps populating with 1, even though I keep changing the application setting to 2 or 3 or 4 or 5.

    My head hurts. I cannot figure out what I've done wrong that I can't change the value.

    So I thought I lost this stuff (see strike out text) when I realized that some how all my HANDLES code had disappeared. Odd, that. I put it back in and suddenly it's all working again.

    And when I say the HANDLES code disappeared, I still had the Private Sub through the arguments

    example


    Private Sub RandomIntToolStripMenuItem_KeyDown (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)

    Dim BreakTime As Int32 = 1

    If Int32.TryParse(RandomIntToolStripMenuItem.Text, BreakTime) Then

    My.Settings.RandomInt = RandomIntToolStripMenuItem.Text

    My.Settings.Save()

    Else

    MsgBox("Whoops")

    End If

    RandomIntToolStripMenuItem.HideDropDown()

    End Sub

    But after the "KeyEventArgs)" part of the line, there was NOTHING. YIKES.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • Sorry forgot you are using ToolStrip, to close the menu use the HideDropDown method on the owning ToolStripMenuItem of RandomIntToolStripMenuItem

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Viewing 15 posts - 1 through 15 (of 15 total)

    You must be logged in to reply to this topic. Login to reply