from form to owner - vb.net

I can get data from owner to form, but not the other way around.
How can I pass my data from my form to my owner. ?
I really cant not use
Dim ownerFrm As New ownerFrm()
If I do that, this form will not load in a database connection and a lot more things, so i'd rather avoid messing with that.
There must be a very simple solution for this. But i just can't come up with it.

The owner form needs to give a reference to itself to the child form. There are multiple ways to do this. For instance, when the owner form shows the child form, it could do something like this:
frmChild = New ChildForm()
frmChild.Parent = Me
frmChild.Show()
Or, instead of a property, you could overload the child form's constructor:
frmChild = New ChildForm(Me)
frmChild.Show()
Or, you could create a method that shows the child form and takes the parent as an argument:
frmChild = New ChildForm()
frmChild.ShowChild(Me)
In all of these cases, the child form must have a variable that keeps a reference to the parent form so that it can make calls back to it.
The other option would be to add events on the child form. When the child form needs to send data to the owner form, it could raise an event containing the data and the owner form would handle the event and get the data that way.

You should use events (see WithEvents).

Related

How to set parent to get access to another form in vb.net?

I got 2 forms:
Input.vb and Writer.vb
When Input was set as default form, I was able to do the following:
In Writer.vb
Input.mymessage = texter.Text
texter is btw an Textbox.
Since I needed to change the default form to another it does not work anymore.
How to fix that? Is there any "set parent" or "Dim Input as Input.Forms.all" way to get it work again?
Already tried Dim Input as new Input.
Edit:
I found the way of using CType(Me.ParentForm, Input).mymessage = texter.Text, but Writer.Parent = Me does not work for me :/
Remember that you have object definitions (classes), object instances of those classes, and variables that have references to those instances.
People tend to forget these things apply to forms, too.
When Input was set as the default form, VB.Net was giving you a default instance for the form, and a special global variable that refers to it with the same name as the class. So the name Input in your code could be one of two different things, depending on the context: the class type, or the special variable for the default instance of that class type.
Now that Input is not the default form, you're not using that default instance any more. When you show the form, you're creating your own instance. The same is true for the Writer form. You have a Writer class, but that's only the definition for an instance of the class you create somewhere. You need to provide this instance of your Writer form with a reference to the instance of the Input form that was created.
You do that the same way you handle object references with any other .Net class.
When going one to another form, instead of using the "Private Sub" I use Public sub, it shares everything in that form to anything else trying to use it.

Access VBA - Can I assign an instance of a form to a Subform control?

Let's say I have a MainForm in Access.
On that MainForm is a SubForm control.
I also have another form called AnotherForm that I want to plug into the MainForm's SubForm control using it's SourceObject property.
I then want to capture the instance of the newly created AnotherForm in a variable.
I can do that like this:
'Inside the MainForm's code-behind
Me.SubForm.SourceObject = "Form.AnotherForm"
Dim af As Form_AnotherForm
Set af = Me.SubForm.Form
Cool, af now holds the instance of the form created by the SubForm control when I set it's SourceObject property.
Now, instead, let's say I want to reverse that process by first newing up an instance of AnotherForm and then passing it to the SubForm control.
Theoretically it would look something like this:
'Inside the MainForm's code-behind
Dim af As Form_AnotherForm
Set af = New Form_AnotherForm
Me.SubForm.Form = af 'ERROR: Read-only
Any way to accomplish that using some other method?
I want to be able to attach different forms to the SubForm control and switch them out at run-time. However, if I attach AnotherForm to SubForm, then attach YetAnotherForm to SubForm, then I go back and attach AnotherForm to SubForm again, I want to use the same instance of AnotherForm as I used the first time so that any changes on it won't be lost when it reappears.
As I know, it's impossible. You cannot replace instance of subform, you can only change SourceObject, this operation destroys previous instance of subform and creates new one "from scratch".
I would use in your case tab control for holding instances of all subforms you want to switch between: create tab control with tabs style none, make borders transparent, so the appearance of subforms will be the same, as if one subform control used.
For switching between visible subforms just assign required value to tab conrol
Did you try using the Set statement:
Set Me.SubForm.Form = af

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.

How to Access DataTable Information Filled By a Module From a Form

I have a DataSet named BillMat.xsd
When my application loads, a module fills that dataset's DataTable with the correct information.
My question is ... How can I access that DataTable's already filled information from another form?
Here's how I tried to access it on one of my forms:
Dim View As New DataView
View.Table = BillMat.Tables("dtBillHeader")
But I get the following error:
If I create a new instance of my dataset and store it in a variable, I'll be able to get rid of this error message but it will also get rid of all my data in my dataset's datatables ... Is there a way to access a DataTable's information from another form?
You need to fix it so both forms are referencing the same DataSet or DataTable object. If one is a "child" form of the other, such as a dialog, you can pass it from the parent to the child via a property. Otherwise, ideally, the same data object would be injected into both forms by some third object which created both of the forms. Short of all that, you could create a singleton or global variable, but please don't!

Reading 'Checked' Property Of Dynamic CheckBox on Page Postback

Apologies in advance for the long-winded post, but I'm having some trouble with a .NET page I'm building.
END QUESTION: How can I check the 'Checked' property of a dynamically-created checkbox during the Page_Load subroutine?
DETAILS: Basically, I have a VB.NET page that creates some table rows dynamically, based on a number selected by the user. So, for example, if the user selects "3" from a dropdown list, the page posts back and creates three table rows. Each row contains a couple of textboxes, a dropdown list, and a checkbox (which are all .NET form controls rather than plain HTML controls, I should point out).
Typically, the user would enter a few details into the form controls, and click the 'Submit' button, after which the page iterates through each row, and inserts the data into a SQL Server table.
But if the user ticks the checkbox for that row, this signifies that the page is to ignore that row, and NOT insert that row of data into the database when the user clicks 'Submit'.
This works well, but there is a problem. If the user clicks 'Submit' and some of the values entered into the form controls are invalid (so, for example, the user didn't enter their name) then the page won't submit the data, and instead, shows an error to the user informing them of the values they need to change. But if the user creates three rows, for example, but decides to "ignore" the third row (by ticking the checkbox) then when the page posts back, finds some invalid entries, and re-shows the form to the user to allow them to correct any errors, I'd rather the page didn't render the third row altogether. After all, they chose to create three rows originally, but then decided that they only needed two. So it makes sense that the third row is not recreated.
To start with, I simply used code similar to the following within my Page_Load subroutine:
If objCheckbox.Checked = False
' Render the row, and recreate the dynamic controls '
Else
' Dont render the row or any of the controls '
End If
But what seemed to happen was that objCheckbox.Checked was always returning False, even when the checkbox was ticked. Once the page had loaded, the table rows had rendered again, and the tick was present in the checkbox, so it's not like the value was lost on postback. But at the point I check whether the checkbox is ticked, it always returns False, rendering a table row that the user doesn't need.
Does anyone know how to get round this problem? I've read lots of articles about the .NET ViewState, and the page lifecycle, but I've yet to find a solution that works. I simply need to be able to check if a checkbox is ticked before re-creating some dynamic controls.
I tried this alternative code, which utilises the ViewState, but to no avail:
If objIgnoreCheckbox.ViewState("Checked") = False Then
' Render the row, and recreate the dynamic controls '
Else
' Dont render the row or any of the controls '
End If
When doing this, I get the following error:
'System.Web.UI.Control.Protected Overridable ReadOnly Property ViewState() As System.Web.UI.StateBag' is not accessible in this context because it is 'Protected'.
So I tried to create a custom class, that inherited from Checkbox, and tried to override the ViewState property to make it public, so that it can be read from:
Public Class CheckboxControl
' Inherits all Checkbox properties and methods '
Inherits Checkbox
' Create the public ViewState property '
Public Overrides ReadOnly Property ViewState As StateBag
Get
Dim objChecked As Object = ViewState("Checked")
If Not (IsNothing(objChecked)) Then
Return objChecked
End If
End Get
End Property
End Class
But then I basically found out that you can't override a protected member with a public one. I've had very little experience with creating new classes etc, so I'm stumped.
So if anyone can tell me how to go about checking the darned checkbox, I'd be eternally grateful! I've spent a full working day trying to solve the problem, but with no luck!
Thanks in advance!
For static controls, the view state of controls is restored in Page_Init, which happens before Page_Load, so they contain the correct values in Page_Load. Dynamic controls are created in Page_Load, so their viewstate is incorrect in Page_Load and will be restored after Page_Load, but before calling event handlers. MSDN says:
After the Page_Load event has run, but before control event-handling methods are called, the remaining view state information is loaded into the dynamically created controls.
This is why Checked returns false, and why changing the visibility of CheckBox.ViewState will not solve your problem.
Solution (untested, but I think it should work): Create all controls (even those that you don't want to display) in Page_Load. Attach an event handler on CheckedChanged to your CheckBoxes (in Page_Load). After Page_Load has finished, ASP.NET will restore the view state of the newly created controls (that's why it is important to create the same controls in Page_Load, so that ASP.NET can correctly map the view state to the control), and ASP.NET will call your event handler for those CheckBoxes that have been changed. There, you can remove the rows you don't want to display.
This is how you add the event handler
AddHandler objCheckbox.CheckedChanged, AddressOf MyCheckedChangedFunction
This function would look something like this:
Function MyCheckedChangedFunction(sender As Object, e As EventArgs)
Dim objCheckbox = DirectCast(sender, CheckBox)
... access objCheckbox.Changed and do something useful here ...
End Function