Inheriting from a component and showing the result in the designer?
Inheriting from a component and showing the result in the designer?
I already posted this in the Visual Studio C++ Express Edition board, but after not having received a single answer despite dozens of views I figured this might be a better place to ask this question:
Joltan wrote: |
| In Visual Studio 2005 C++ Express Edition I extended the ListView class through inheritance with some methods of my own to create a specific ListView type I need repeatedly in a small project of mine. Now the new ListView-based class works fine as it's intended once I compile my project, but I can't find a way to add it to the toolbox or to get the forms designer to work with it. I really just added a few methods that make updating the specific list's content simpler, no original methods were overwritten. Is there a way to get the designer to display my new ListView class |
|
I don't nescessarily need it placable in the toolbox, but it would be great if the designer at least could display the ListView so I can add events and resize it more easily. Any help would be appreciated as it's a bit tiring to do all this manually in code, then compile to check the results...
Inheriting from a component and showing the result in the designer?
trager
Regards
sakifcy
here's the C++/CLR code:
public interface class IAsLVItem{
public: inline Windows::Forms::ListViewItem ^asItem(void);
};
public ref class ddlListView : public Windows::Forms::ListView
{
public:
ddlListView(void){}
inline void AddList(Collections::ArrayList ^itemlist){
Windows::Forms::ListViewItem ^item = gcnew Windows::Forms::ListViewItem();
bool isUnique;
for(int idx=0;idx<itemlist->Count;idx++){
item = ((IAsLVItem ^)itemlist[idx])->asItem();
isUnique = true;
for(int j=0;j < this->Items->Count;j++){
if(this->Items[j]->Name->Equals(item->Name)){
isUnique = false;
for (int k=0;k<this->Items[j]->SubItems->Count;k++){
this->Items[j]->SubItems[k]->Text = item->SubItems[k]->Text;
}
break;
}
}
if(isUnique) this->Items->Add(item);
}
}
inline void AddItem(IAsLVItem ^itemobj){
Windows::Forms::ListViewItem ^item = gcnew Windows::Forms::ListViewItem();
bool isUnique = true;
item = itemobj->asItem();
for(int idx=0;idx < this->Items->Count;idx++){
if(this->Items[idx]->Name->Equals(item->Name)){
isUnique = false;
for (int j=0;j<this->Items[idx]->SubItems->Count;j++){
this->Items[idx]->SubItems[j]->Text = item->SubItems[j]->Text;
}
break;
}
}
if(isUnique) this->Items->Add(item);
}
inline void UpdateItem(IAsLVItem ^itemobj){
Windows::Forms::ListViewItem ^item = gcnew Windows::Forms::ListViewItem();
item = itemobj->asItem();
for(int idx=0;idx < this->Items->Count;idx++){
if(this->Items[idx]->Name->Equals(item->Name)){
for (int j=0;j<this->Items[idx]->SubItems->Count;j++){
this->Items[idx]->SubItems[j]->Text = item->SubItems[j]->Text;
}
break;
}
}
}
inline void CheckWithList(Collections::ArrayList ^itemlist){
Windows::Forms::ListViewItem ^item = gcnew Windows::Forms::ListViewItem();
bool isObsolete;
for (int idx=(this->Items->Count - 1);idx>=0;idx--){
isObsolete = true;
for (int j=0;j<itemlist->Count;j++){
item = ((IAsLVItem ^)itemlist[j])->asItem();
if(this->Items[idx]->Name->Equals(item->Name)){
isObsolete=false;
break;
}
}
if(isObsolete) this->Items->RemoveAt(idx);
}
}
};
Edit: Looks like this forum doesn't like the letter 'i' in square brackets... hope I didn't forget to change any i's when copying the code.
ThomasBear
1) Right click on your project, click Add->Add User Control (note: NOT Inherited Control)
2) Name it whatever you want, say "NamedUserControl" for the purposes of this list.
3) In the code behind for the control, replace "NamedUserControl : System.Windows.Forms.UserControl" with "NamedUserControl : ListView"
4) Compile
5) Goto the form designer for some form you want to add this too, whatever form, just not the NamedUserControl.
6) Click on the ToolBox, click on the tab (usualy the first one) that says "My User Controls"
7) NamedUserControl should appear in that list, and you should be able to drag and drop it onto your form/control/whatever it is you want to add this custom control too.
You can now add whatever new properties and functionality you want to, however you want to be able to "redraw" anything regarding the control.
Kamal Hathi
1. I *believe* (though I cannot confirm at the moment) that "auto toolbox generation" is by default turned off for this profile. You can change this in Tools->Options->WinformsDesigner->AutoToolboxPopulate
2. When you add a custom control that appears elsewhere in your form, the designer does not automatically add a reference to the header file at the top. This will cause your project not to build. We will fix this in a future version.
There should be nothing stopping you from going into "Choose Items" on the toolbox and manually adding your control. If you cannot do this, can you post the code for your listview