Callavin

My aplication needs a usercontrol consisting a combobox.
So I have to provide the commonly used propertiesfor the comboBox for this usercontrol as well.

I build this UserControl DLL and using in some other Windows Forms, but then some problems arises.

I have few problems in that:

1. There is a property called "FormatString", I have attached the editor "FormatStringEditor" also with this while declaring this property. But when I try to use this property in the "property-pane" I am getting an error

Object Reference not set to an instance of an object"

2. I have also exposed the property for "Text" and "Items" and attached these properties to the ComboBox corresponding properties. In design time they all work fine i.e if i insert some string against the 'Text' property in the property pane of this UserControl, it will reflected in the Combox in the Form-designer. But when i run the application this control goes back to default setting all the data which i have inserted is not reflected in the user control i.e now there is not textbox in the comboBox.

I have developed TextBox like this also and it works fine in desing mode as well runtime.

So i am confused why the combobox is failing

any helpl will be greatly appreciated.



Re: Windows Forms General UserControl with ComboBox

Geert Verhoeven

Hi Callavin,

Can you post the code of your UserControl so I can take a look at it.

Greetz,

Geert

 

 

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog






Re: Windows Forms General UserControl with ComboBox

Callavin

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}

//For the Property Text
[Browsable(true)]
public override string Text
get{ return comboBox1.Text;}
set{ comboBox1.Text = value;}
}

//For the Property FormatString

[DefaultValue("")]
[
Editor("System.Windows.Forms.Design.FormatStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[
MergableProperty(false)]
public string FormatString{
get{ return this.comboBoxCtrl.FormatString;}
set{ this.comboBoxCtrl.FormatString = value;}
}
}





Re: Windows Forms General UserControl with ComboBox

Geert Verhoeven

Hi Callavin,

The FormatStringEditor is marked as internal which means that it can only be used from within the same assembly file, being System.Desgin.dll. Therefor it is not possible to use it like this.

If you want to have the same functionality as the FormatString dialog box, you will need to create it yourself by using the UI Type Editor.


Sample:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconuitypeeditorforwinformssample.asp

Greetz,

Geert

 

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog






Re: Windows Forms General UserControl with ComboBox

Callavin

Got the point about the FormatStringEditor.

Any idea how to expose the Text Property.





Re: Windows Forms General UserControl with ComboBox

Geert Verhoeven

Sorry, forgot to answer on the other question.

I did a small test and it worked for me. Here is my code:

public partial class MyUserControl : UserControl
{
   public
MyUserControl()
   {
      InitializeComponent();
   }

   [Browsable(true)]
   [Category("CUSTOM"
)]
   public override string
Text
   {
      get { return
comboBox1.Text; }
      set { comboBox1.Text = value
; }
   }
}

Calling code:

public
partial class Form1 : Form
{
   public
Form1()
   {
      InitializeComponent();
   }

   private void button1_Click(object sender, EventArgs
e)
   {
      this.myUserControl1.Text = "New text"
;
   }
}

Greetz,

Geert

 

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog






Re: Windows Forms General UserControl with ComboBox

Callavin

Can't we do the same thing by editing the value against the "Text" Property Pane. As it was my concern.



Re: Windows Forms General UserControl with ComboBox

Geert Verhoeven

I don't really understand what you mean. The code above, maps the Text property of the usercontrol to the Text property of the combobox. Isn't that what you needed

Greetz,

Geert

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog






Re: Windows Forms General UserControl with ComboBox

Callavin

When we put any item in the Windows-Forms. Then we can set so many properties of that item(control) from the properties pane.

Same way I want to do with this usercontrol which embeds a ComboBox. Can we do that





Re: Windows Forms General UserControl with ComboBox

Geert Verhoeven

Yes, this is possible. All public properties with the attribute [Browsable(true)] will be shown in your IDE. You can add as much properties to your user control as you want.

Geert

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog






Re: Windows Forms General UserControl with ComboBox

nobugz

Just in case you haven't considered it yet: you might want to just derive your custom combobox from the standard combobox through inheritance. It has many advantages over a UserControl, not the least of which is that you'll inherit all the built-in designer support as well as avoid the need to re-publish the properties.

Start by adding a new class to your project with this code:

using System;
using System.Windows.Forms;

public class MyComboBox : ComboBox {
// Customize as needed...
}






Re: Windows Forms General UserControl with ComboBox

Callavin

The Control also consists of a picture box. So if i drive this from the Combox how will i show the Picture box Can we do this by deriving from Combox Only.

And to the answer of Geert, I exposed the same "Text" proprty as u have mentioned. But here the problem starts. When I update the value against Text Property, it reflects in the designer-Form also. But when i run the application that combox becomes blank.





Re: Windows Forms General UserControl with ComboBox

Geert Verhoeven

Hi Callavin,

Can you sent me your project. I'll take a look at it.

Greetz,

Geert

 

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog