 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 10th, 2008, 11:54 PM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
working with 2 user controls
hi everybody,
I have an urgent problem here, can anybody help me? the problem is:
I have a custom combobox that will be attached to a custom toolbar.
My custom toolbar has a dataset that contains one or more tables.
Now i want to have a property for my custom combobox so that i can select one table from a list filled with my custom toolbar dataset tables, and in the next step selecting the field from the fields list of the selected table.
I have used CustomTypeEditor but the problem is in the custom editor class that i've written, i cant detect my toolbar in case to retrieve my dataset tables.
please help im a lot confused here,
Thanks.
Be Sure,
Hossein
__________________
Be Sure,
-hossrad
|
|

May 11th, 2008, 11:25 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I'm not familiar with the CustomTypeEditor but here's how I would approach this problem:
Create a method, property or event on each control that needs to expose its data. You can then use the appropriate members to get and put the data from and to each control in the class where you use the control.
A better approach that would reduce the coupling between the containing class and the individual controls would be to define and implement an interface on the data provider control (the toolbar in this case). Create an internal instance of that interface on your consuming class (the combobox) with a method, property or constructor that accepts an instance of the interface. In the containing class assign the toolbar control to the interface assignment member of the combobox control. (This gives the combobox a view to the toolbar.) The combobox control can call whatever interface members it needs to. If you need to add members there is no need to change the container class(es) as they just handle the full instance of the interface instead of the individual members.
-Peter
compiledthoughts.com
|
|

May 12th, 2008, 01:07 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi peter,
that was so nice, after all i had a reply from someone and so this site is not dead.
it seems that i haven't described my problem clearly so let me explain:
TypeEditor is a class that is called when you want to change a property of your control in design time, and when you want to write a custom property editor for any of your user control properties you need to define a class that inherits from typeeditor and override two methods. for reference i will follow by a sample code of my user control.
public class Custom_ComboBox : UserControl
{
[Description("TableCollection"), Editor(typeof(TableEditor), typeof(UITypeEditor))]
Ã
public string[] TableCollection
{
get
{
return tablename;
}
set
{
tablename= value;
Invalidate();
}
}
}
this means that i have a custom combobox as a user control that has a property named tablecollection. in here i have access to table names from my other control(Toolbar). now i want to edit the value of my tablecollection property through a new form, this is where typeeditor comes in place. the line above the property determines that if i want to edit this property TableEditor will handle it, so here is the code of table editor:
class TableEditor:UITypeEditor
{
private string[] TableNames = new string[20];
public TableEditor()// if i can pass a parameter here the problem is solved
à à Ã
{
MessageBox.Show("TableCollection");
TableNames = TableCollection;
}
public override UITypeEditorEditStyle GetEditStyleà à (System.ComponentModel.ITypeDe scriptorContext context)
{
return UITypeEditorEditStyle.DropDown ;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorCon text context, IServiceProvider provider, object value)
{
//return base.EditValue(context, provider, value);
IWindowsFormsEditorService FrmSvr = (IWindowsFormsEditorService)provider.GetService(ty peof(IWindowsFormsEditorService));
if (FrmSvr == null)
{
return null;
} Ã Ã
à à Ã
ComboBox Cb = new ComboBox();
// here i should add my tables to the combobox
FrmSvr.DropDownControl(Cb);
return Cb.SelectedValue;
}
}
}
the problem is just here, in the class table editor i have no access to the tables collection from my user control class and because i cannot create an instance of tableeditor so i can pass parameters to it(C# automatically handles the tableeditor class calling)
Be Sure,
Hossein
|
|

May 12th, 2008, 07:48 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Aha... I see the problem now.
TypeEditor defines the "EditValue" method with argument "object value" because it does not know what data type you will be editing. If my assumptions about the behavior of this method are correct, when the designer creates your custom editor class, it will pass the table collection to the EditValue method, then it should only be a matter of casting "value" to a TableCollection so you can then work with the discrete type you know will be passed into the method. To be safe, add a type check so your editor doesn't crash.
-Peter
compiledthoughts.com
|
|

May 13th, 2008, 01:56 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm very thankful of you Peter,
I haven't tested your solution yet but it seems to be the right solution. I'm now thinking of another problem that you first assumed to be mine. I read your post and googled about the "Interface" that you mentioned in it, but I cannot fully understand it. I need to send some data from one user control (Toolbar in my case) to another completely independent user control (ComboBox in my case) so I don't know the Instance of the Toolbar in order to call a method of it to get the data I need. how can Interface help me with this.
Be Sure,
Hossein
|
|

May 13th, 2008, 07:03 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Perhaps I over complicated it a bit by throwing in the use of interfaces.
You have two controls: Tool bar and combobox. They are both on a form. You need to communicate data between them, but each one doesn't necessarily know about the other. When the form that contains these two controls creates the instance of each control, it can wire up the connection for the data between the two.
The problems is how to do this. Without knowing more about how the data is being generated (and the fact that I work with ASP.NET far more than with winforms) I may be trying to provide solutions to problems I don't understand.
Perhaps you could describe where the data is coming from, how it's generated, when it is updated and more about the relationship between the toolbar and the combobox.
-Peter
compiledthoughts.com
|
|

May 13th, 2008, 07:40 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok let me explain my situation here:
First user control is ToolBar (I'll refer to it as CustomToolBar), it contains a DataSet. The DataSet itself contains one or more Tables.
The second user control is Combobox (I'll refer to it as CustomComboBox).
Now I drop a CustomToolBar and a CustomComboBox in a form.
In the next step I'll add table(s) to my CustomToolBar, so now my CustomToolbar's dataset has it's table(s) filled.
Now I want to set my CustomComboBox properties. I need a list of CustomToolbar's tables so that in the CustomComboBox property I have a list to select the table from.(And the next step is selecting a field from the table) and as you know i'm doing it with a CustomTypeEditor that I described earlier.
Be Sure,
-hossrad
|
|

May 13th, 2008, 08:11 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
What purpose is the dataset serving for the toolbar? Is the toolbar displaying items based on the data? What if you added the dataset as a member of the form and each control consumes it as needed?
You are in territory that I haven't explored so I might not be much more help unfortunately.
-Peter
compiledthoughts.com
|
|

May 13th, 2008, 08:23 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
For some considerations -that is boring to describe- I need the dataset in my Toolbar.
Anyway thank you for your time.
Be Sure,
-hossrad
|
|

May 13th, 2008, 09:21 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
You appear to be trying to add a lot of design-time editing to what could (and perhaps should?) be run-time considerations.
Simply loading the datasets and table names at run-time will make your life much easier.
/- Sam Judson : Wrox Technical Editor -/
|
|
 |