Set control property as the result of a function taking a data-bound paramater?

  • Thread starter Thread starter Computer Guru
  • Start date Start date
C

Computer Guru

Hi,

Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.

Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.

But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.

For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.

Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to send that
value to a function and use the output as the location property.

Any ideas?
 
Hi,

Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.

Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.

But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.

For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.

Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to send that
value to a function and use the output as the location property.

Any ideas?

It's not entirely clear how your DataBinding is set up, but it shouldn't be too difficult to achieve if you have an object between the GUI and the DataBase. The object should have a property you can bind to a Control.Location property. Calculating the location is done by the object and can be as complex or simple as you like.
 
Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.
Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.
But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.
For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.
Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to send that
value to a function and use the output as the location property.
Any ideas?

It's not entirely clear how your DataBinding is set up, but it shouldn't be too difficult to achieve if you have an object between the GUI and the DataBase. The object should have a property you can bind to a Control.Location property. Calculating the location is done by the object and can be as complex or simple as you like.

--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -

- Show quoted text -

Hi Morten, thanks for your reply.

I'm not sure I follow:

I have a GUI (datagrid), a bindingsource, and a dataset. The
PictureBox.ImageLocation property is the one I'd like to set via a
function - but the function needs to get it's parameter from the
bindingsource (which obviously changes every time a new row is
selected in the datagrid).

The current code (takes the information directly from the database and
sticks it directly in .ImageLocation):
this.pictureBox1.DataBindings.Add(new
System.Windows.Forms.Binding("ImageLocation", this.dvdsBindingSource,
"Image", true));

So what I need is somehow:
pictureBox1.ImageLocation = new
System.Windows.Forms.Binding("CalculateImage", this.dvdsBindingSource,
"Image", true)

Where CalculateImage is a delegate/callback to a function that takes a
single parameter (the field "Image" from the database) and returns a
value to store it in .ImageLocation....

Weird, I know.

Thanks :)
 
Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.
Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.
But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.
For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.
Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to send that
value to a function and use the output as the location property.
Any ideas?

It's not entirely clear how your DataBinding is set up, but it shouldn't be too difficult to achieve if you have an object between the GUI and the DataBase. The object should have a property you can bind to a Control.Location property. Calculating the location is done by the object and can be as complex or simple as you like.

--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -

- Show quoted text -

Hi Morten, thanks for your reply.

I'm not sure I follow:

I have a GUI (datagrid), a bindingsource, and a dataset. The
PictureBox.ImageLocation property is the one I'd like to set via a
function - but the function needs to get it's parameter from the
bindingsource (which obviously changes every time a new row is
selected in the datagrid).

The current code (takes the information directly from the database and
sticks it directly in .ImageLocation):
this.pictureBox1.DataBindings.Add(new
System.Windows.Forms.Binding("ImageLocation", this.dvdsBindingSource,
"Image", true));

So what I need is somehow:
pictureBox1.ImageLocation = new
System.Windows.Forms.Binding("CalculateImage", this.dvdsBindingSource,
"Image", true)

Where CalculateImage is a delegate/callback to a function that takes a
single parameter (the field "Image" from the database) and returns a
value to store it in .ImageLocation....

Weird, I know.

Thanks :)

Well, if your BindingSource contained a list of your own objects insteadof the DataSet you could easily create a CalculateImage property, although you would lose the ability to update the Database easily.

Something like

List<MyObject> list = FillListFromDatabase();
BindingSource source = new BindingSource(list);
pictureBox1.DataBindings.Add("ImageLocation", source, "Location");

class MyObject()
{
public Image Image{ get; set; } // C# 3 syntax
public Point Location
{
get{ return CalculateImageLocation(); }
}
}
 
Hi Morten, thanks for your reply.
I'm not sure I follow:
I have a GUI (datagrid), a bindingsource, and a dataset. The
PictureBox.ImageLocation property is the one I'd like to set via a
function - but the function needs to get it's parameter from the
bindingsource (which obviously changes every time a new row is
selected in the datagrid).
The current code (takes the information directly from the database and
sticks it directly in .ImageLocation):
this.pictureBox1.DataBindings.Add(new
System.Windows.Forms.Binding("ImageLocation", this.dvdsBindingSource,
"Image", true));
So what I need is somehow:
pictureBox1.ImageLocation = new
System.Windows.Forms.Binding("CalculateImage", this.dvdsBindingSource,
"Image", true)
Where CalculateImage is a delegate/callback to a function that takes a
single parameter (the field "Image" from the database) and returns a
value to store it in .ImageLocation....
Weird, I know.

Well, if your BindingSource contained a list of your own objects instead of the DataSet you could easily create a CalculateImage property, although you would lose the ability to update the Database easily.

Something like

List<MyObject> list = FillListFromDatabase();
BindingSource source = new BindingSource(list);
pictureBox1.DataBindings.Add("ImageLocation", source, "Location");

class MyObject()
{
public Image Image{ get; set; } // C# 3 syntax
public Point Location
{
get{ return CalculateImageLocation(); }
}

}

--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -

- Show quoted text -

Gotcha. So basically there is no easy way to add a proxy between the
bindingsource and the PictureBox alone; leaving the original
connection from other objects to the bindingsource and from the
bindingsource to the dataset/DB intact.

*just occurred to me*

Create a new control that inherits from PictureBox, and overrides just
the Location property.

customPictureBox : PictureBox
{
string ImageLocation {
get { return base.ImageLocation; }
set { base.ImageLocation = CalculatePath(value); }
}

Wouldn't that work?
 
On Jul 30, 8:58 pm, "Morten Wennevik [C# MVP]"
Hi,
Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.
Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.
But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.
For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.
Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to sendthat
value to a function and use the output as the location property.
Any ideas?
It's not entirely clear how your DataBinding is set up, but it shouldn't be too difficult to achieve if you have an object between the GUIand the DataBase. The object should have a property you can bind to a Control.Location property. Calculating the location is done by the object and can be as complex or simple as you like.
- Show quoted text -
Hi Morten, thanks for your reply.
I'm not sure I follow:
I have a GUI (datagrid), a bindingsource, and a dataset. The
PictureBox.ImageLocation property is the one I'd like to set via a
function - but the function needs to get it's parameter from the
bindingsource (which obviously changes every time a new row is
selected in the datagrid).
The current code (takes the information directly from the database and
sticks it directly in .ImageLocation):
this.pictureBox1.DataBindings.Add(new
System.Windows.Forms.Binding("ImageLocation", this.dvdsBindingSource,
"Image", true));
So what I need is somehow:
pictureBox1.ImageLocation = new
System.Windows.Forms.Binding("CalculateImage", this.dvdsBindingSource,
"Image", true)
Where CalculateImage is a delegate/callback to a function that takes a
single parameter (the field "Image" from the database) and returns a
value to store it in .ImageLocation....
Weird, I know.

Well, if your BindingSource contained a list of your own objects instead of the DataSet you could easily create a CalculateImage property, although you would lose the ability to update the Database easily.

Something like

List<MyObject> list = FillListFromDatabase();
BindingSource source = new BindingSource(list);
pictureBox1.DataBindings.Add("ImageLocation", source, "Location");

class MyObject()
{
public Image Image{ get; set; } // C# 3 syntax
public Point Location
{
get{ return CalculateImageLocation(); }
}

}

--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -

- Show quoted text -

Gotcha. So basically there is no easy way to add a proxy between the
bindingsource and the PictureBox alone; leaving the original
connection from other objects to the bindingsource and from the
bindingsource to the dataset/DB intact.

*just occurred to me*

Create a new control that inherits from PictureBox, and overrides just
the Location property.

customPictureBox : PictureBox
{
string ImageLocation {
get { return base.ImageLocation; }
set { base.ImageLocation = CalculatePath(value); }
}

Wouldn't that work?

That should work, although you may need to have your customPictureBox implement INotifyPropertyChanged. Not sure, and the base might take care of it, but if not it would look something like this

public string ImageLocation
{
get{ return base.ImageLocation; }
set
{
base.ImageLocation = CalculatePath(value);
notifyPropertyChanged("ImageLocation");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void notifyPropertyChanged(string property)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
 
On Jul 30, 8:58 pm, "Morten Wennevik [C# MVP]"
Hi,
Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.
Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.
But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.
For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.
Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to send that
value to a function and use the output as the location property.
Any ideas?
It's not entirely clear how your DataBinding is set up, but it shouldn't be too difficult to achieve if you have an object between the GUI and the DataBase. The object should have a property you can bind to a Control.Location property. Calculating the location is done by the object and can be as complex or simple as you like.
--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -
- Show quoted text -
Hi Morten, thanks for your reply.
I'm not sure I follow:
I have a GUI (datagrid), a bindingsource, and a dataset. The
PictureBox.ImageLocation property is the one I'd like to set via a
function - but the function needs to get it's parameter from the
bindingsource (which obviously changes every time a new row is
selected in the datagrid).
The current code (takes the information directly from the database and
sticks it directly in .ImageLocation):
this.pictureBox1.DataBindings.Add(new
System.Windows.Forms.Binding("ImageLocation", this.dvdsBindingSource,
"Image", true));
So what I need is somehow:
pictureBox1.ImageLocation = new
System.Windows.Forms.Binding("CalculateImage", this.dvdsBindingSource,
"Image", true)
Where CalculateImage is a delegate/callback to a function that takes a
single parameter (the field "Image" from the database) and returns a
value to store it in .ImageLocation....
Weird, I know.
Thanks :)
Well, if your BindingSource contained a list of your own objects instead of the DataSet you could easily create a CalculateImage property, although you would lose the ability to update the Database easily.
Something like
List<MyObject> list = FillListFromDatabase();
BindingSource source = new BindingSource(list);
pictureBox1.DataBindings.Add("ImageLocation", source, "Location");
class MyObject()
{
public Image Image{ get; set; } // C# 3 syntax
public Point Location
{
get{ return CalculateImageLocation(); }
}
- Show quoted text -

Gotcha. So basically there is no easy way to add a proxy between the
bindingsource and the PictureBox alone; leaving the original
connection from other objects to the bindingsource and from the
bindingsource to the dataset/DB intact.

*just occurred to me*

Create a new control that inherits from PictureBox, and overrides just
the Location property.

customPictureBox : PictureBox
{
string ImageLocation {
get { return base.ImageLocation; }
set { base.ImageLocation = CalculatePath(value); }

}

Wouldn't that work?- Hide quoted text -

- Show quoted text -

Just for the record, it worked great.

Add this class:

class cPictureBox : PictureBox
{
public new string ImageLocation
{
get
{
return base.ImageLocation;
}
set
{
base.ImageLocation = CalculatePath(value);
}
}

string CalculatePath(string dvdid)
{
return "C:\\" + dvdid;
}
}
}



Search for the declaration and intialization of pictureBox1 and change
the type from PictureBox to cPictureBox.
All done :-)
 
On Jul 30, 8:58 pm, "Morten Wennevik [C# MVP]"
Hi,
Pardon the unclear/verbose subject, I just can't think of another way
of phrasing it.
Basically, I can assign the (for instance) "Location" property of a
picture box to a data-bound item. Like, I can get the absoloute
location from an ADO.NET database, and assign it to the
PictureBox.Location property.
But what I want to do is assign MyClass.GetImage(DATABOUND_PARAMETER)
to the PictureBox.Location property.
For instance, I get the value "myimage.jpg" from the database. Now I
need to send the text "myimage.jpg" to a function and then assign the
*output of the function* to the PictureBox.Location property.
Via the DataBinding property I can tie the "Location" property of the
PictureBox to the datasource, but I can't seem to get it to send that
value to a function and use the output as the location property.
Any ideas?
It's not entirely clear how your DataBinding is set up, but it shouldn't be too difficult to achieve if you have an object between the GUI and the DataBase. The object should have a property you can bind to a Control.Location property. Calculating the location is done by the object and can be as complex or simple as you like.
--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -
- Show quoted text -
Hi Morten, thanks for your reply.
I'm not sure I follow:
I have a GUI (datagrid), a bindingsource, and a dataset. The
PictureBox.ImageLocation property is the one I'd like to set via a
function - but the function needs to get it's parameter from the
bindingsource (which obviously changes every time a new row is
selected in the datagrid).
The current code (takes the information directly from the database and
sticks it directly in .ImageLocation):
this.pictureBox1.DataBindings.Add(new
System.Windows.Forms.Binding("ImageLocation", this.dvdsBindingSource,
"Image", true));
So what I need is somehow:
pictureBox1.ImageLocation = new
System.Windows.Forms.Binding("CalculateImage", this.dvdsBindingSource,
"Image", true)
Where CalculateImage is a delegate/callback to a function that takes a
single parameter (the field "Image" from the database) and returns a
value to store it in .ImageLocation....
Weird, I know.
Thanks :)
Well, if your BindingSource contained a list of your own objects instead of the DataSet you could easily create a CalculateImage property, although you would lose the ability to update the Database easily.
Something like
List<MyObject> list = FillListFromDatabase();
BindingSource source = new BindingSource(list);
pictureBox1.DataBindings.Add("ImageLocation", source, "Location");
class MyObject()
{
public Image Image{ get; set; } // C# 3 syntax
public Point Location
{
get{ return CalculateImageLocation(); }
}
}
Gotcha. So basically there is no easy way to add a proxy between the
bindingsource and the PictureBox alone; leaving the original
connection from other objects to the bindingsource and from the
bindingsource to the dataset/DB intact.
*just occurred to me*
Create a new control that inherits from PictureBox, and overrides just
the Location property.
customPictureBox : PictureBox
{
string ImageLocation {
get { return base.ImageLocation; }
set { base.ImageLocation = CalculatePath(value); }
}
Wouldn't that work?

That should work, although you may need to have your customPictureBox implement INotifyPropertyChanged. Not sure, and the base might take care of it, but if not it would look something like this

public string ImageLocation
{
get{ return base.ImageLocation; }
set
{
base.ImageLocation = CalculatePath(value);
notifyPropertyChanged("ImageLocation");
}}

public event PropertyChangedEventHandler PropertyChanged;
private void notifyPropertyChanged(string property)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));}

--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -

- Show quoted text -

btw, it wasn't necessary to use the notifyPropertyChanged function to
get it to work.

Thanks for all your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top