pass control values window forms - vb.net

I have a form with 5 controls.
For textbox I am accesing like frmNote.txtNumber.Text.
When I am accessing this property directly from form frmNote then I am getting the value of textbox.
But when I created a method for example NewMethod() in different vb file and call this method from frmNote form button click event then I am not able to access frmNote.txtNumber.Text value over there inside method.
It is coming blank. Do I need to pass all control values to method from form or is there any other way around.

Because txtNumber isn't shared, you can't access it from other class. You must pass it's value to NewMethod. Code will look like this:
Public Sub NewMethod(text as String)
'Use text
End Sub
And calling NewMethod:
NewMethod(txtNumber.Text)

Related

How to use Form Controls in Modules

In my modules, I want to use my controls from my form. For example: I want to set focus on a textbox after a certain Sub.
My current solution is to make a subroutine to set all controls in a public variable (see below).
My questions:
What is the best practice? How do people usually do this?
When should I call my subroutine? (is it the first call in the FORM_LOAD sub?)
Public TBnr As TextBox
Public Sub controlsInitieren()
Set TBnr = Forms("frm_TreeView_Example").pstNr
End Sub
Well, as a general rule, while many platforms seperate out the UI part and the code part? Well, Access is quite much a different approach - it is standard fair to place the required code inside of the form "class" (all forms in Access are a "class", and you can even have muliple instances of the SAME form open more then one time).
So, in general, your code should be in the forms code (class) module.
However, you could and call a external routine.
So in the form, you could call your above routine like this:
Call MySetFocus(me, "NameOfControlToSetFocusTo")
And your sub would look like this:
Sub MySetFocus(f as form, sCtrl as string)
f(sCtrl).SetFocus
End Sub
However, as noted, the amount of code above is more code then simply in the forms code module going:
me.ControlName.SetFocus
However, while the above is a less then ideal example, passing the form "instance" (me) to a external sub or function allows you to referance any property or method or feature that exists in the form in an external routine.
So in place of say
me("LastName") = "Zoo"
In the above sample routine, you would and could go;
f("LastName") = "Zoo"
So any place you would and could use "me" in the form, you can use the form instance you passed from the form. As noted, it is a good idea to use "me", since as I noted, Access does allow multiple copies of the form to be opened at the same time - and thus your code can't distinguish between what form instance you are using unless you pass the current "in context" form. So like in JavaScript, using "this" ?
In access that current instance of the class object is "me", and you are free to pass that instance to any sub or function you want as per above.
The best practice is to use only procedures inside the form code. In such a case you refer to a text box control in this way: Me.Textbox1.SetFocus. If you want to set some controls properties during the form loading, you can do that in the frm_TreeView_Example_Initialize event;
They usually do it in the way I described at item 1;
If you want to use such a strange/unusual way you can do it calling the subroutine whenever you want. But, in order to set a specific property value of the form frm_TreeView_Example controls you can simply use frm_TreeView_Example.TextBox1.SetFocus. You can use this way of setting in a module procedure, even before the form has been shown. You can simply show it in that procedure code using at the end: frm_TreeView_Example.Show;

Extension method does not respect My.Application.Culture

In a VB.NET 4.72 WinForms project, i have placed an MDI form (MDIForm1), an MDIChild form (Form1) and a module. In the module, i have declared an extension method:
<Extension()>
Public Sub ShowChild(SourceForm As Form)
MessageBox.Show(My.Application.Culture.ToString)
End Sub
In the MDIForm's Load event, i have the following code:
My.Application.ChangeCulture(NewCultureName)
My.Application.ChangeUICulture(NewCultureName)
'NewCultureName is a string which is set to any valid value other than "en-US".
Form1.ShowChild
When debugging the project, the MessageBox that appears when ShowChild is called, displays "en-US" instead of the value of NewCultureName. If a breakpoint is placed in "Form1.ShowChild" statement, i can see that the culture has indeed changed to the value of NewCultureName. The application acts though as if there are two different instances of My.Application, one in the MDI form and another inside ShowChild. Why is this happening?
2019-04-14: Edited to a more precise description of the issue.

How to assign an output to a label in a second form through the first form?

I wanna show the output of the calculations in a second form but I am writing the code in the first form. How can I do it?
Use the parent form name in front of the control that you are trying to work with. You may need to set the modifier to Friend.
Say that the label exists in form2 and you have code in form1 that is needing to change it. So you would do it like this: form2.label.text = "the string value here"
However, keep in mind that if the control was created or is owned by a different thread then the one that is trying to edit the control, you will receive a runtime exception.
To resolve that you will need to create a delegate for the calling sub or function.

Updating contents of an iRibbon control from another control's callback

Back in the old days of VBA, we used to be able to directly access controls on a form by name. I'm a little perplexed by how to do this in VBA 2010 (or if it's even possible any longer).
Say I have a ribbon control with a dropdown list called "channelList", and elsewhere on the ribbon I have a textbox called "labelText". Each of these items has a unique callback function - when I type something into labelText, its callback fires, and when I select an item from the channelList listbox, its callback fires with the list item passed as an argument.
Where I'm stumped by is how I would update the labelText textbox contents with 'something' from within the channelList callback.
Is there a way to directly access the textbox control from within the listbox callback, or must I generate some sort of event? What method or function would I use to set the text value for the control? (Do I need to cast the IRibbonControl object to something?)
The solution was a combination of an answer and the comments, so here goes:
Add a "getText" callback subroutine name to the Ribbon XML, specific to the "labelText" editbox.
editBox id="labelText" label="Text:" sizeString="xxxxxxxxxx"
onChange="TextboxCallback" getText="UpdateTextBoxText"
screentip="Channel label"
supertip="Limited to 10 characters. Press Enter once finished typing."
In the combo box callback function, set the desired editbox text to a global and call Ribbon.InvalidateControl with "labelText" as the argument.
MyRibbon.InvalidateControl "labelText"
Implement the editbox callback, passing a handle to the ribbon and another variable ByRef to contain the 'stuff' to be updated. Use the global to update the control's text via the ByRef argument.
Sub UpdateTextBoxText(control As IRibbonControl, ByRef returnedVal)
Select Case (control.id)
Case "labelText"
returnedVal = LabelText
End Select
End Sub
I guess this is "progress". labelText.Value = "blah" is far too simple.
Take a look at the Ribbon.Invalidate method. When you "invalidate" the ribbon you basically reset it, and in that moment you can set the properties of controls on the ribbon based on whatever you can track in that moment. In order to do so, you need to have Control.GetEnabled, GetVisible, etc., subroutines in your VBA.
You can also invalidate individual controls with Ribbon.InvalidateControl.
Here's a link that might help: http://sourcedaddy.com/ms-excel/resetting-controls.html

How to reference a subform in MS Access

In my MS Access application, I am using a form which contains only two controls - a textbox and a command button. This form is named as HEADER FORM.
HEADER FORM is used as a subform in header section of various other forms.
What I want to do is that whenever a particular form loads, I want to fill details in the textbox of the HEADER FORM (that will be name of the person who has logged in. The same would be clear from the picture below).
I am trying to call a global subroutine named updateHeader in form load event of all the forms.
Public Sub updateHeader()
Me![HEADER FORM].Form.txtHeaderName.Value = strPerson
End Sub
Following is the picture showing HEADER FORM in Design View and the same being used as a subform in a login form.
I tried various other options but am not able to come out with the correct way to reference the form. Am I doing something wrong fundamentally?
The error that I am seeing is invalid use of Me keyword.
Also, my updateHeader subroutine is a global subroutin which is called from Form_Load event of all the forms.
If your updateHeader() procedure is contained in a standard module, that would explain the complaint about the Me keyword ... it's not valid in a standard module.
In a form module, Me means "this form".
You could change the procedure declaration to accept a reference to a form.
Public Sub updateHeader(ByRef TheForm As Form)
' Me![HEADER FORM].Form.txtHeaderName.Value = strPerson
TheForm![HEADER FORM].Form.txtHeaderName = strPerson
End Sub
.Value is the default property and therefore not needed here, so I left it out. But it won't hurt to add it back if you prefer.
You can then call the procedure from the parent form, and pass the procedure a reference to itself (the parent form).
updateHeader Me
I got these "syntax versions" from Wiley.Microsoft.Office.Access.2007.Bible:
When referencing subform controls:
Forms![FormName]![SubformName].Form![ControlName]
When using/referencing subforms within subforms, use the following syntax:
Forms![FormName]![SubformName].Form![SubSubformName].Form.[ControlName]