How to save user inputted value in TextBox? - vb.net

I have a Textbox that I want to be saved after it is closed, and then given to another computer.
Public Class Form1
Dim textBoxes() As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
textBoxes = New TextBox() {TextBox2}
With My.Settings
If .TextBoxValues Is Nothing Then .TextBoxValues = New System.Collections.Specialized.StringCollection
For i = 0 To textBoxes.Length - 1
If .TextBoxValues.Count <= i Then .TextBoxValues.Add("")
textBoxes(i).Text = .TextBoxValues(i)
Next
End With
In Settings I have a Setting called TextBoxValues
system.collections.specialized.stringcollection
And then, this works with Scope User, but it doesn't save if the application is moved, or given to another computer. So I have to change the Scope to Application. But when I turn it to Application I get this Error:
Property 'TextBoxValues' is 'ReadOnly'.
for
.TextBoxValues = New System.Collections.Specialized.StringCollection

I think you must save in a txt file, XML or somethig .. because in other way how you keep dtat typed data ? :)

Related

How to save settings made in VB?

Is there any way I can save changes made by a user , so that at the next run they would still be there? I have seen something about My.Settings , is that how it is done?
Yes you have multiple options, you can create a file and load it on startup, or you can use the built in settings object. Just go to your Project Property's --> settings set your variable there, the you can access it through My.Settings
Then load it like this:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim greeting As String = My.Settings.mySetting
End Sub
you would change the value and save it like this
My.Settings.mySetting = "GoodBye"
My.Settings.Save()

Passing Variable from Windows form to Modal

Windows Forms, VB. I have searched the web for the correct answer to this and no dice. Either they are missing on what I am trying to accomplish or are in CSHARP making it harder for me to see what they are doing. I have a need to pass a record Id from the main windows form into a modal Dialog Load Event.. I have tried throwing a with param in but then I have to change the Load event params and vb flags it.. I am trying to pass the value of _CurrentProp which is an integer into the dialog. This is the dialog constructor and the load event inside that dialog..
Private Sub PropertySettingsMenuClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PropertyDetailsToolStripMenuItem.Click
Dim _propertSettings As New PropertySettingsWindow()
_propertSettings.ShowDialog()
End Sub
Private Sub PropertySettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim _properties As New List(Of property_info)
_properties = db.property_info.ToList
For Each a In _properties
If Not p_settingsCityList.Items.Contains(a.city) Then
p_settingsCityList.Items.Add(a.city)
End If
Next
For Each b In _properties
If Not p_settingsPropertyList.Items.Contains(b.property_Name) Then
p_settingsPropertyList.Items.Add(Convert.ToString(b.idProperties) + " -- " + b.property_Name)
End If
Next
p_settingsZipCode.ReadOnly = True
p_settings_Address.ReadOnly = True
p_settings_PropertyName.ReadOnly = True
End Sub
I am going to simply assign the value to a global variable inside the PropertySettings Class but everything I try seems to fail in one way or another... Any ideas...
Add a public property RecordID to your dialog window, then open the dialog like this
Dim _propertSettings As New PropertySettingsWindow()
_propertSettings.RecordID = 15
_propertSettings.ShowDialog()
In the dialog form you can simply access the record id with
_properties = db.property_info_by_id(RecordID).ToList
Starting with .NET Framework 4.0, you can use auto-implemented properties
Public Property RecordID As Integer
With previous versions you would have to write
Private _recordID As Integer
Property RecordID As Integer
Get
Return _recordID
End Get
Set(ByVal value As Integer)
_recordID = value
End Set
End Property

Text is selected in the text box

When I load the form where some text has been given to text box. All the text in that textbox is highlighted. I want vb not to load it this way.
How to fix it.
Thanks
Furqna
You could set the tab index on your textbox to something else so that it's not the lowest index.
You could set the TextBox1.SelectionLength = 0 in the form.activated event.
I don't like this as much because if the user had the text hilited and minized the application then they will lose the hilite, but is fairly easy to do. I guess you could use a flag to make sure it only did it on the first activate.
You could set a timer event in the load to clear it immediately after the load event, but that seems like overkill. I have worked at places where they had a standard function that happened on every form 100 ms after load because of problems such as this.
You could try this(it looks like a workaround):
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
It depends on the TabIndex of your TextBox, if it has the lowest TabIndex it gets focus and therefore it's Text is selected.
' VS.net 2013. Use the "Shown" event.
' GotFocus isn't soon enough.
Private Sub Form_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TB.SelectionLength = 0
End Sub
Type 1 Method
Dim speech = CreateObject("sapi.spvoice")
speech.speak(TextBox1.Text)
Type 2 Method
Dim oVoice As New SpeechLib.SpVoice
Dim cpFileStream As New SpeechLib.SpFileStream
'Set the voice type male or female and etc
oVoice.Voice = oVoice.GetVoices.Item(0)
'Set the voice volume
oVoice.Volume = 100
'Set the text that will be read by computer
oVoice.Speak(TextBox1.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
Type 3 Method
Imports System.Speech.Synthesis
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim spk As New SpeechSynthesizer
For Each voice As InstalledVoice In spk.GetInstalledVoices
ListBox1.Items.Add(voice.VoiceInfo.Name)
Next
ListBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim spk As New SpeechSynthesizer
spk.SelectVoice(ListBox1.SelectedItem.ToString)
spk.Speak(TextBox1.Text)
End Sub
End Class
This will also happen sometimes if The TextChanged or other similar Event is fired twice for the control.
When creating each form. Each object is indexed you can set the tab Index higher then the indexed object. Example: On the third form you put a text box in.
private void textBox1_TextChanged(object sender, EventArgs e)
This was the 12th object in the project, it would be indexed at 12. if you put the tab index higher then the indexed objects throughout the project. Tab index 1000 (problem solved.)
Have a great day.
Scooter

Error with code?

Hi can anyone tell me why the following dose not work:
(p.s I dont want the file to append upon clicking abutton just upon clicking the checkbox.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim FILE_NAME As String = "C:\RXF\log.txt"
'Adding items for AutoCAD 2006...
If CheckBox1.CheckState = CheckState.Checked Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine("module: 4FNV-67-5H")
objWriter.Close()
End If
End Sub
End Class
Not reproducible, even with your exact code as posted. This works perfectly fine for me, creating a text file in the specified location if one does not exist and appending the specified text to the end of the file.
The only thing I suggest is wrapping your StreamWriter object in a Using statement to ensure that its Dispose method always gets called, even if an exception is thrown (which is all the more likely when you're doing disk I/O). So, your existing code would simply change to:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim FILE_NAME As String = "C:\RXF\log.txt"
''#Adding items for AutoCAD 2006...
If CheckBox1.CheckState = CheckState.Checked Then
Using objWriter as New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine("module: 4FNV-67-5H")
objWriter.Close()
End Using
End If
End Sub
Also, if you anticipate this method getting called a lot (i.e., the user clicking and unclicking and clicking the checkbox repeatedly), you might consider creating the StreamWriter object once and saving it as a private class-level variable, instead of creating and disposing of it each time the method is called. Then you just have to make sure that you dispose of it whenever your class (presumably the containing form) is disposed.

how do I catch events on runtime objects

I'm creating a form with a few buttons and a combobox at runtime.
dim f as new form
(blah blah)
then the buttons acceptDescription and rejectDescription are set up...
then the combobox descriptionCombo is set up...
then...
AddHandler acceptDescription.Click, AddressOf handleAcceptedDescription
AddHandler rejectDescription.Click, AddressOf handleRejectedDescription
then I have these two methods to catch the click events... but can't figure out how to reference the other runtime generated controls. (combobox if accepted, form if rejected)
Private Sub handleAcceptedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
'stub
'will need to reference the chosen combobox value here
dim acceptedDescription as string = descriptionCombo.selectedValue .tostring
End Sub
Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
'I want to close the runtime created form here, but can't reference it
f.close()
'and return user to main form
Me.Focus()
End Sub
If the code for generating the form is in your main form, then declare the Form variable at the class level of the main form class so you can access it from the event handlers. Same goes for your combobox and text field- you need to make sure the variables are declared outside the scope of the handlers so you can reference them within the handlers.
Why can't you reference it? Just save it as a module/form-level variable and you're set.
In order to avoid global definitions, the best answer is
Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
'sender is a button in this case.
'get the button
dim b as new button
b = ctype(sender,button)
'now get the button's parent form
dim f as new form
f = ctype(b.parent, form)
'now close the form
f.close()
End Sub