I would like to bind the text field of a textbox to an element in an instance of a structure that I've created in a class module. When I try to create a Data Source using an OBJECT type, all I see in the tree is the name of the structure, not the instance I've created. If I select the structure anyway, the Data Sources tree does show the name of the structure and the element I'm interested in. How do I bind to a specific instance? I'm using VB.net
you have to assign instance programmatically to datasource , it will not available in design time, you can do it in formload or any other event.
something like..
BindingSource.Datasource = new instance //assign to your instance to binding source
then..
TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", BindingSource, "Name", True))// here name propery is binding to textbox
Related
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.
I have a Dialog which has a ComboBox (aComboBox, say) with the following properties set:
DataSource linked to a BindingSource which is linked to an SQL table
DisplayMember linked to a column in that table which is of type String
Here is a pic of how the properties are set:
I want the String value of the item selected in the combo box but I am getting System.Data.DataRowViewwhen I use this code at run time:
aComboBox.SelectedItem.ToString()
How do I get the String value?
Do not use aComboBox.SelectedItem.ToString()
Instead of that Use : aComboBox.SelectedValue()
I managed to fix this by setting the Value Member attribute as shown in the following image, I am then able to retrieve the String value by calling aComboBox.SelectedValue.ToString() :
Note: Before, I only had the Display Member attribute set.
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!
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).
I have an autocomplete box where I am trying to bind the selected item to a propery of the item that is actually selected.
I.e. I have a Client object with a Name property, and the ItemsSource of my autocomplete box is a List of Clients.
The property I am trying to bind as the selected Item is a String, called SelectedClientName.
But because SelectedClientName is not of type Client, I cannot bind it directly.
As a work around, I am using a converter.
But I would like to know if there is a way to do this in the binding without converters.
I'd suggest having SelectedClientName not be of a different type, in your case of type string. Keep it the Client type and utilize your Name property where a string is required.
Here's some additional information on binding to complex objects in an AutoCompleteBox: http://www.codeproject.com/Tips/79158/AutoCompleteBox-Binding-Custom-Objects