Initialising values on a form from the calling form (1 Viewer)

KeithWilliams

Registered User.
Local time
Today, 22:42
Joined
Feb 9, 2004
Messages
137
Hi,

I have a form, Form_ValidateIncidents, which has a button that opens another form, Form_Property. The second form has a number of textboxes and other controls bound to a data source. I open the form in this case for the purpose of adding a new record. I want to initialise some of the values using code in the calling form.

So Form_ValidateIncidents has the following code:
Dim form_child As New Form_Property()
form_child.MdiParent = Me.MdiParent
form_child.Show()
form_child.OrgNameTextBox.Text = "ABC"

The application is an MDI, hence the setting of MDIParent, but I think that's irrelevant to my question. I'm setting the initial value to ABC to prove it works - there are a number of columns that need to be set, to various values derived from my calling form.

Form_Property has a DataSource, BindingSource and DataAdapter that were created using Visual Studio (not by my code). The Load event has the code:
Me.PropertyTableBindingSource.AddNew()

The problems are:
1. I can't set the Text of OrgName to "ABC" until after the form is visible, because the Load event on Form_Property does not fire until I call form_child.Show(). This could be a problem if performance is slow.
2. Surely I should be setting column values in the underlying DataAdapter (or DataSource or BindingSource?) rather than in the Text Box on the form? Otherwise I would have to change the code if I replaced the Text Box with a Combo Box or similar.
3. Is there a more object-oriented way to pass the initial values to the form?

As I said, I am currently working on how to add a new record to the database through this form. If its relevant, I also want to use this form later as the form for viewing and editing existing records.

Many Thanks,
Keith.
 

KeithWilliams

Registered User.
Local time
Today, 22:42
Joined
Feb 9, 2004
Messages
137
Made some progress! I think this is better, and works just as well.

In Form_ValidateIncidents:
Dim form_child As New Form_Property()
Dim dr_newProperty As DataRow
form_child.MdiParent = Me.MdiParent
form_child.PropertyTableBindingSource.AddNew()
dr_newProperty = CType(form_child.PropertyTableBindingSource.Current, DataRowView).Row
dr_newProperty("OrgName") = "ABC"
form_child.Show()

I removed the code from the Load event of Form_Property, as the new record is now added in the calling event code.

Would still greatly appreciate advice on how to do this in an object-oriented way.

Thanks,
Keith.
 

MarkK

bit cruncher
Local time
Today, 14:42
Joined
Mar 17, 2004
Messages
8,186
If your text box's text property was bound in design view then calling the bindingsource.AddNew() is sufficent. You should be able to simply assign the parameterized text "ABC" to the control's text property.

Try two constructors on your Form_Property...
Code:
Public Sub New()
[COLOR="Green"]  'can be called with no parameters and does not add a record
  'may allow users to navigate to existing data
[/COLOR]End Sub
And...
Code:
Public Sub New(ByVal YourData As String)
[COLOR="Green"]  'when data is supplied, add operation is assumed, so use BindingSource.AddNew()
[/COLOR]  Me.bsPropertyTable.AddNew()
[COLOR="Green"]  'and assign the data to the control's text property[/COLOR]
  Me.YourTextBox.Text = YourData
End Sub
So Form_ValidateIncidents does this...
Code:
  [COLOR="Green"]'pass data to the constructor, which does the add[/COLOR]
  Dim YourData as String = "ABC"
  Dim fChild as New Form_Property(YourData)
  [COLOR="Green"]'and show the form[/COLOR]
  fChild.Show()

But the main issue seems to be that if a property of a control is bound, there should be no need dig into the DataRowView of the binding source to assign data. You should be able to assign data directly to the text property like the user does with keystrokes.
Lemme know if this helps. Still a .NET newbie myself.
 

KeithWilliams

Registered User.
Local time
Today, 22:42
Joined
Feb 9, 2004
Messages
137
Hi lagbolt,

Many thanks for your detailed response. Your approach with two constructors sounds really valuable, I will look into that. Not so sure about assigning values directly to the controls on the form, as this seems less object-oriented than assigning to the underlying data source. Its more likely that the controls used to display data will change than the underlying data, so your suggested approach would probably increase the need for code changes in assigning the initialisation values in future.

I actually have a number of values to pass to the form to initialise fields, so I will consider whether I should be creating a PropertyTable class, and passing an object of that class as a parameter to the New() constructor method.

Thanks,
Keith.
 

Users who are viewing this thread

Top Bottom