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.
Related
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.
First of all, Why are forms classes?
But now on to the main question.
I have:
Form1.vb
Module1.vb
On the form there is a textbox, progress bar, labels etc.
I want to be able to change the properties of these objects on my form from module1, but I cant seem to access them.
These things on the form are objects, right? So do they have a certain scope? and how can I change it?
Wait but according to my solution explorer, these things are properties of a class??
But the form shows up when I run the program?? Wouldn't the form class have to be instantiated so that the form1 object is created?
Not that it matters, but here is a snippet from module1
Sub WriteObjectsToCSV()
Dim props As PropertyInfo() = MyCompanies(1).GetType().GetProperties()
Dim sw As StreamWriter =
My.Computer.FileSystem.OpenTextFileWriter(SaveAs, False)
Dim csv As New CsvHelper.CsvWriter(sw)
csv.WriteHeader(Of Company)()
csv.NextRecord()
For Each company In MyCompanies
'>>> want to write to my text box and change my progress bar here <<<
For Each prop In props
csv.WriteField(prop.GetValue(company))
Next
csv.NextRecord()
Next
End Sub
Forms are classes because they are created dynamically. You can instantiate and open the same form class serveral times and leave the instances open at the same time.
VB automatically instantiates the main form.
You can access the open forms through My.Application.OpenForms. The main form is always the first
Dim mainForm As Form1
mainForm = DirectCast(My.Application.OpenForms(0), Form1)
mainForm.txtOutput.Text = "hello"
To be able to access the controls of the form from outside, they must be declared Public or Internal. You can change the access modifier from the properties window (Modifiers property).
Instead, you can also declare a property in the form to make the text accessible outside
Public Property OutputText() As String
Get
Return txtOutput.Text
End Get
Set(ByVal value As String)
txtOutput.Text = value
End Set
End Property
Now you can write this in the module
mainForm.OutputText = "hello"
The question is simple, I have Form1 with a label and some other control. Now in an external class when I call a function I save a setting like this:
My.Setting.Update = Date.Now
this parameter is displayed on the label of Form1 in the Load event, so I want to know how I can refresh the label text when this string is updated. I tried with Form1.Refresh from the other class but didn't worked. Any suggestions?
Create a public sub on Form1 called RefreshFromSettings. This sub should set the label text to the settings value again, just like your code does now in the Load event.
Then in your external class, given a reference to the instance of Form1 in a variable named frm, you can call that sub like this.
frm.RefreshFromSettings()
That will call your code that updated the label text.
I have got a windows form with a Combobox and two datepickers objects in it. I want upon user selection of the values to pass the variables values to another windows form.
All I've seen is how to do this with showDialog method on an instance of the class, however this doesn't work for me as the user has to select a user from Combobox and pick the date range and click on search button.
A quick straight forward help will be appreciated as my time is running close to the deadline.
Thanks.
you can do something like this
1- creation of public event, singleton approach
Public Class EventClass
Private Sub EventClass()
End Sub
Public Shared Sub Invoke(sender as object, value as object)
RaiseEvent OnValueChange(sender,value) ' be sure OnValueChange is not nothing first
End Sub
Public Shared Event OnValueChange(sender as object,value as object)
End Class
2- raising the event, in the form that containing the combobox and datepicker
Handle the event of the combo on selected index changed
inside the event of combo raise the event as
EventClass.Invoke(ComboBox1,ComboBox1.SelectedValue)
and in the case of DateTimePicker use
EventClass.Invoke(DateTimePicker1,DateTimePicker.Date)
3- in the form that you want to pass the values to
public sub EventClass_OnValueChange(sender as object, value as object) handles EventClass.OnValueChange
' do your code here but be sure that this form was created before the form that contains the invoke or it will not be fired
end sub
hope this will help you
I currently have 2 forms. 1 holds user data and is modifiable. The other is a display/read only form.
I am trying to pull data (tbUSI.text) from ReportSettings and pass it to Form1 and display it on my UserData.text control.
I have tried using public properties but to no avail. I would rather use public properties, since its cleaner. Here is the code im using to set the property:
Public Property UserSignedInto As String
Get
Return tbUSI.Text
End Get
Set(value As String)
End Set
End Property
Here is my code attempting to call that property on the main form (form1)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UserData.text = UserSignedInto
End Sub
It doesn't pull anything, the textbox on my main form is blank.
Take a look here:
SCCMReports = New ReportSettings
UserData.text = SCCMReports.UserSignedInto
When was the SCCMReports form ever displayed to the user? Since it was never displayed, its tbUSI.Text value will of course be empty because the user never had an opportunity to enter text.
It sounds like you need a reference to an existing instance of ReportSettings, rather than creating a new instance. Where do you have that existing instance?
If Form1 created the instance in another block of code, store it in a class-level member on Form1 (perhaps called SCCMReportsInstance or something of that nature). When the instance is created, set it to the value of that property and reference that property in your code:
UserData.text = Me.SCCMReportsInstance.UserSignedInto
If the ReportSettings form is instead creating the instance of Form1 then it can pass a reference to itself. You'd still have a property on Form1, it would just be set in the constructor. Something like this:
Sub New(ByVal sccmReportsInstance As ReportSettings)
Me.SCCMReportsInstance = sccmReportsInstance
End Sub
So when initializing the Form1 instance, you'd pass the reference:
Dim form1 As Form1
form1 = New Form1(Me)
form1.Show()
Any way you go about it, you need to access the existing instance of the displayed form in order to access its properties. A new instance would have new versions of those properties and wouldn't have the same values.