Get textbox value in user control - vb.net

I'm anand. I have a problem about how to get text value in textbox in user control. I make a small application. I use Form1 as main form, and in the form I put one panel. In panel I will put custom user control that contain textbox. Now, I want to get the text on the textbox in user control and show it in the msgbox. Can anyone help me to fix this problem please?

As André Leal said in the comments. If you define a public property in your user control you will be able to access the value.
Public Property textboxValue as String
Get
Return MyTextBox.text
End Get
Set(value as String)
MyTextBox.text = value
End Set
End Property
Then you can access it using code like this.
YourUserControl.textboxValue
If you don't need to edit the value of the textbox you can make the property readonly as follows.
Public ReadOnly Property textboxValue as String
Get
Return MyTextBox.text
End Get
End Property

Related

VB.net Texbox Value not being updated to Public Shared Variable

Im having a textfield set up with the text "Hello" and a button
When i change the text and press i'd like to have a msgbox replying the changed text
Public Class TestText
Dim Text As String = Textbox.text
Private Sub BtnchangeTXT_Click(sender As Object, e As EventArgs) Handles BtnchangeTXT.Click
Text = Textbox.Text
Msgbox(Text)
Msgbox(TextField.Fieldtext)
End Sub
End Class
Public Class TextField
Public Shared FieldText As String = TestText.Textbox.Text
End Class
All works well untill i request for the "TextField.FieldText" msgBox this will always return the defaulttext ("hello")
where do i go wrong?
Ive tried to set a different text "On Load" but still the default "Hello" is being returned.
In this line:
Public Shared FieldText As String = TestText.Textbox.Text
The value of "FieldText" only gets set ONCE, when that variable is created (and that occurs even before the form with the textbox is shown). It does not get updated whenever the TextBox changes.
If you want that variable to always have the current value of that TextBox, then use the TextChanged() event of that TextBox to update the variable.
Private Sub Textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox.TextChanged
TextField.Fieldtext = Textbox.Text
End Sub
There are two problems with your code. Firstly, because this code:
Public Class TextField
Public Shared FieldText As String = TestText.Textbox.Text
End Class
uses the name of the TestText class to refer to an instance of that class, it is referring to the default instance. If you don't know what default instances are, I suggest that you do some reading on the subject. You can find my own writeup here.
That means that that code will always get the Text of the TextBox on the default instance of that type. If you display the default instance then that's fine but if you explicitly create an instance and display that, then modify the contents of the TextBox on that instance, that will have exactly zero effect on the default instance.
The second issue is the fact that you are using a field. You seem to be under the impression that that FieldText field should change dynamically as you change the contents of the TextBox. Why would you think that? The code you have DOES NOT get the current contents of the TextBox every time you get the field value. What is does is get the current contents of the TextBox and assign it to the field on the first occasion you get that field and then NEVER changes the field value again. If what you actually want is the current contents of the TextBox then you would need a property rather than a field:
Public Class TextField
Public Shared ReadOnly Property FieldText As String
Get
Return TestText.Textbox.Text
End Get
End Property
End Class
The whole point of properties is that they act like fields on the outside, so your code to get that property value won't change, but they act like methods on the inside, so the code to get the current contents of the TextBox will be executed every time you get the property value.

Creating form setting in VB Programmatically

My question is how to create a new form setting in vb.net language to save data Programmatically.
For example when i click button it will create the setting which it,s name is the text of the textbox1.
is this possible and how.
And is there any functions which can save data when program is closed?
You can do that through the form designer.
Go to the ApplicationSettings / PropertyBinding and click the ... button.
Then assign a New setting to the Text property by clicking here:
.Net takes care of making the settings save automatically when the program exits. If you want to force it to save, just call My.Settings.Save()
You can create your own settings-class which inherits ApplicationSettingsBase:
Imports System.Configuration
Public Class MyUserSettings
Inherits ApplicationSettingsBase
<UserScopedSetting()> _
<DefaultSettingValue("white")> _
Public Property BackgroundColor() As Color
Get
BackgroundColor = Me("BackgroundColor")
End Get
Set(ByVal value As Color)
Me("BackgroundColor") = value
End Set
End Property
End Class
Save the settings:
Dim Mus As New MyUserSettings
Mus.BackgroundColor = Color.AliceBlue
Mus.Save()
Load the settings:
Dim Mus As New MyUserSettings
MessageBox.Show(Mus.BackgroundColor.ToString)
Source: MSDN
One of the way in VBA is a standard functions to save setting to Registry:
Call SaveSetting(appName, Section, Key, Value)
Value = GetSetting(appName, Section, Key)
Just put them in form constructor and form destructor:
Private Sub UserForm_Initialize()
...
end sub
Private Sub UserForm_Terminate()
...
end sub

DatetimePicker and other Controls in MenuStrip

I know I can add a DateTimePicker to my MenuStrip with the following lines
Dim dp = New ToolStripControlHost(New DateTimePicker)
MenuStrip1.Items.Add(dp)
But I can't figure out how to add a DateTimePicker to the MenuStrip at designtime. What's the trick behind it? I have been trying and searching for like an hour and I am about to give up even though I know there has to be a way!
TL;DR
How do I add a DateTimePicker to my MenuStrip at design-time?
Alternatively we can add it to a ToolStrip instead.
You are close to a solution in using the ToolStripControlHost, but you will need to derive from that class as shown in the linked-to example. The frustrating thing with that example is that it does not decorate the derived class with the System.Windows.Forms.Design.ToolStripItemDesignerAvailabilityAttribute to make it available on the design surface.
The following is a minimalist implementation to get a working example. You may need to override the automatic sizing to suit your needs/wants for the control. The implementation overrides the Text property to prevent designer from assigning invalid text to the underlying DateTimerPicker control.
<System.Windows.Forms.Design.ToolStripItemDesignerAvailability(
System.Windows.Forms.Design.ToolStripItemDesignerAvailability.ToolStrip _
Or System.Windows.Forms.Design.ToolStripItemDesignerAvailability.StatusStrip _
Or System.Windows.Forms.Design.ToolStripItemDesignerAvailability.MenuStrip)> _
Public Class TSDatePicker : Inherits ToolStripControlHost
Public Sub New()
MyBase.New(New System.Windows.Forms.DateTimePicker())
End Sub
Public ReadOnly Property ExposedControl() As DateTimePicker
Get
Return CType(Control, DateTimePicker)
End Get
End Property
<Browsable(False), EditorBrowsable(EditorBrowsableState.Advanced), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Overrides Property Text As String
Get
Return ExposedControl.Text
End Get
Set(value As String)
' verify valid date
Dim dt As DateTime
If DateTime.TryParse(value, dt) Then
ExposedControl.Text = value
End If
End Set
End Property
End Class
Was going to add as a comment but I trust it justifies an answer.
The only way I have succeeded in this is to add one at design time (to the form) and set Visible to False and use the menu item to set Visible to True (may also need to set the position and/or bring it to the front).
You do need to manually handle setting Visible to False again.

Best way to change specific property of set of controlsin VB.net

I have a set of controls on my form and i want to enable/disable some of them. what is the best way?
Hint: I don't want to change all controls available in my form.
If your meaning from "enable/disable" is "preventing user from changing them", then you can do this:
THE_NAME_OF_CONTROL.Enabled = False 'Disable a control with THE_NAME_OF_CONTROL Name
And
THE_NAME_OF_CONTROL.Enabled = True 'Enable a control with THE_NAME_OF_CONTROL Name
Or you can put all of your controls in a "Group Box" and disable/enable whole group box.
If you want change controls outside of form, then create a public property or method which do it, instead of making controls public
Public Class MyForm
Inherits Form
Private _MyCheckBoxControl As CheckBox
Private _MyTextBoxControl As TextBox
Private _IsGroupOfControlsEnabled As Boolean
Public Property IsGroupOfControlsEnabled As Boolean
Get
Return _IsGroupOfControlsEnabled As Boolean
End Get
Set (value As Boolean)
_IsGroupOfControlsEnabled = value
'Update controls
_MyCheckBoxControl.Enabled = _IsGroupOfControlsEnabled
_MyTextBoxControl.Enabled = _IsGroupOfControlsEnabled
End Set
End Class

VB.net - Element Reference

I know that you can refer to a form "indirectly" by simply including "me" instead of "form1," for example.
Is there a way to do the same thing for form elements like text boxes or radio buttons?
Example:
If me.checked = true then
count += 1
Else
count -=1
End If
The only way to do that is to create a property named checked that wraps a check box control which is a member of your form class. Something like:
Public Property checked() As Boolean
Get
return myCheckbox.Checked
End Get
Set(ByVal Value As Integer)
myCheckbox.Checked = value
End Set
End Property
Doing this doesn't get you much though. It would actually lead to code obfuscation rather than clarity and brevity.
In VB the keyword me is a reference to an instance of the class which contains the scope of code (as a function of that class) which contains the me reference. I don't know if this is clear enough, but basically me can't be used from inside the Form class to refer to a member of the class (such as a CheckBox or RadioButton control) - only to the class itself.
The CheckBox and RadioButton controls that you place on a Form are created as private objects inside of the Form class which contains them. They are called members of the class. From within the class which contains the CheckBox and RadioButton member instances you can refer to the class itself (the Form) as me. So, assuming that you have a checkbox called "checkbox1" as a control on that form, that CheckBox control would be created as a private member inside of the Form like this:
Private checkbox1 as CheckBox
After that, from that form you could refer to that CheckBox like so:
Me.checkbox1
VB.net uses a sender variable that indicates who's action is being sent to the procedure. By adding multiple elements to the procedure's "handles" property you can use the sender to identify the element being active.