|
I write a class(public class MyTableControlA : System.ComponentModel.Component, IExtenderProvider,ISupportInitialize) that have a DataSet and DataAdapter properties.
I have also a MainForm that i drag and drop and initialize DataSet and DataAdapters on it.
I drag and drop myclass on a form, it DataSource property can brow all DataSets on it contain form.
but i want to that myclass can see and use each DataSet and DataAdapset objects that placed on the mainform. i mainform i fill all datasets and i want to use filled datasets in each other form by my class.
notice than i want to do this visually.
|
|
|
|
|
Then expose your DataSet and DataAdapter as public or internal properties on your form and access them from your child controls or from other forms:
MainForm form = this.Parent as MainForm;
if (form != null)
{
form.DataAdapter.Fill(form.DataSource);
} If you want to access these from other forms, then make sure those forms have a reference to the form instance that declares the DataSet and DataAdapter and that those fields or properties (properties are recommended since they give you more control via encapsulation) are accessible (public or internal, or protected if a form derives from the form that has those properties or fields).
It's all a simple matter of object-oriented programming. Expose objects with member accessibility that provides other objects access to what they need and denies access to what they shouldn't have.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
I have a component that contain a collection of a struct.
I drag and drop this component on a form. than i chang it collection, i want when i close form at design time, my collection content save automatically with form(into form's code or resource).
I use this attribute :"[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]".
but this attribute can save only simle data type such "int" or "string" or ...
and not work for collection of a struct or class. my collection code is here :
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Data ;
namespace DesignerSerializationVisibilityTest
{
// The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility
// attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.
// The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
// for the class object. Content persistence will not work for structs without a custom TypeConverter.
public class ContentSerializationExampleControl : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
int _x ;
public int X
{
get{return _x;}
set{_x=value;}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DimensionData Dimensions
{
get
{
return new DimensionData(this);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public FieldClollectionClass ss
{
get
{
FieldClollectionClass dd=new FieldClollectionClass() ;
FieldColumn fc=new FieldColumn() ;
fc.DisplayName="AAAAAAAAAAA" ;
fc.FieldName="BBBBBBBBBBBBBB" ;
dd.Add(fc) ;
return dd ;
}
}
#region FiledColumnClass
public class FieldColumn
{
private string m_fieldName ;
[ReadOnly(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string FieldName
{
set
{
m_fieldName=value ;
}
get
{
return m_fieldName ;
}
}
public void SetFieldName(string f)
{
m_fieldName=f ;
}
private string m_displayName ;
public string DisplayName
{
get
{
return m_displayName ;
}
set
{
m_displayName=value ;
}
}
private DataTable m_columnDataTable ;
public DataTable ColumnDataTable
{
get
{
return m_columnDataTable ;
}
set
{
m_columnDataTable=value ;
}
}
private bool m_isKey ;
public bool IsKey
{
get
{
return m_isKey ;
}
set
{
m_isKey=value ;
}
}
private string m_childTableValue ;
//[DefaultValue("code")]
public string ChildTableValue
{
get
{
return m_childTableValue ;
}
set
{
m_childTableValue=value ;
}
}
private string m_childTableName ;
//[DefaultValue("name")]
public string ChildTableName
{
get
{
return m_childTableName ;
}
set
{
m_childTableName=value ;
}
}
private int m_width ;
public int Width
{
get
{
return m_width ;
}
set
{
m_width=value ;
}
}
private bool m_visible ;
//[DefaultValue(true)]
public bool Visible
{
get
{
return m_visible ;
}
set
{
m_visible=value ;
}
}
}
#endregion
[TypeConverterAttribute(typeof(System.ComponentModel.ExpandableObjectConverter))]
public class FieldClollectionClass : CollectionBase
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public FieldColumn this[int index]
{
get
{
return( (FieldColumn) List[index] );
}
set
{
List[index] = value;
}
}
public int Add( FieldColumn value )
{
return( List.Add( value ) );
}
public int IndexOf( FieldColumn value )
{
return( List.IndexOf( value ) );
}
public void Insert( int index, FieldColumn value )
{
List.Insert( index, value );
}
public void Remove( FieldColumn value )
{
List.Remove( value );
}
public bool Contains( FieldColumn value )
{
// If value is not of type FieldColumn, this will return false.
return( List.Contains( value ) );
}
}
public ContentSerializationExampleControl()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
}
[TypeConverterAttribute(typeof(System.ComponentModel.ExpandableObjectConverter))]
// This attribute indicates that the public properties of this object should be listed in the property grid.
public class DimensionData
{
private Control owner;
// This class reads and writes the Location and Size properties from the Control which it is initialized to.
internal DimensionData(Control owner)
{
this.owner = owner;
}
public Point Location
{
get
{
return owner.Location;
}
set
{
owner.Location = value;
}
}
public Size FormSize
{
get
{
return owner.Size;
}
set
{
owner.Size = value;
}
}
}
}
|
|
|
|
|
As I told you before, you may have to implement your own CodeDomSerializer derivative if the DesignerSerializationVisibilityAttribute doesn't suite your needs.
I also gave you a link to an article that describes such an implementation. I suggest you read it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a class. I can serialize my class by XmlSerializer.
but when i add a DataTable property in the class, XmlSerializer submit an error in this code:
XmlSerializer serializer = new XmlSerializer(typeof(MyCoulumnClollectionClass));
====>>>>
public class SCol
{
[XmlElement(ElementName = "TaxRate")]
public string FieldName
{
get
{
return m_ ;
}
set
{
m_=value ;
}
}
public string DisplayName ;
//````````````````
public DataTable m_columnTable ;
private string m_ ;
[XmlElement]
public string AAAA ;
}
public class MyCoulumnClollectionClass : CollectionBase
{
public SCol this[ int index ]
{
get
{
return( (SCol) List[index] );
}
set
{
List[index] = value;
}
}
public int Add( SCol value )
{
return( List.Add( value ) );
}
public int IndexOf( SCol value )
{
return( List.IndexOf( value ) );
}
public void Insert( int index, SCol value )
{
List.Insert( index, value );
}
public void Remove( SCol value )
{
List.Remove( value );
}
public bool Contains( SCol value )
{
return( List.Contains( value ) );
}
}
|
|
|
|
|
You might want to tell us what the error is.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
when i add this property to the class :
public DataTable m_columnTable ;
this error in submit:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll
Additional information: There was an error reflecting type 'MyCoulumnClollectionClass'.
error occurein this line :
XmlSerializer serializer = <br />
new XmlSerializer(typeof(MyCoulumnClollectionClass));
when i remove "m_columnTable" , it is ok.
|
|
|
|
|
NEVER use public fields - this is the worst programming concept you could do, except perhaps in the case of private structs used only internally to your code.
If you don't want the DataTable serialized, making the field private (and any public properties that access it, attribute them with the XmlIgnoreAttribute ) will take care of that automatically, since XML Serialization only serializes public and private fields (unless you implement IXmlSerializable , which is undocumented and requires that you know how it works internally).
A DataTable should be serialized as part of a DataSet , so if you want this DataTable serialized, define the field as private (besides, public properties or fields should never start with m_, that goes against naming guidelines), reflect the field with a public property attribute with [XmlIgnore] , then make a public property declared as DataSet that instantiates a new DataSet , puts the DataTable into the DataSet.Tables collection, and returns it. You don't have to use it (and can attribute it with [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] ) - it's only for serialization.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
how can I call the default constructor of a class from an overloaded one? Example:
public class MainForm : System.Windows.Forms.Form {<br />
public MainForm() {<br />
InitializeComponent();<br />
PrepareSomeStuff();<br />
SomeMoreStuff();<br />
}<br />
<br />
public MainForm(string Somestring) {<br />
mSomestring = Somestring;<br />
InitializeComponent();<br />
PrepareSomeStuff();<br />
SomeMoreStuff();<br />
}<br />
}
Any ideas are welcome.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
|
Thats what I was looking for. But still one question: Is the code of the default ctor executed before or after execution of the code in the overload.
Nevermind. I go figure out myself
Thanks for your reply.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
You can use the base keyword. Here is an example:
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("Hello from base");
}
}
public class DerivedClass : BaseClass
{
public DerivedClass() : base()
{
Console.WriteLine("Hello from derived");
}
public DerivedClass(string s) : base()
{
Console.WriteLine("Hello from derived");
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
I actually knew this and I was not looking for a way of doing this in a base/derived class hierarchy. Sorry if I couldn't make myself understood in my initial post. My quest was rather how to call the default ctor of the same class from an overload. Thanks anyway for replying.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
The way I've always done this kind of thing is to create an initialise method and call that. So your code would look like:
public class MainForm : System.Windows.Forms.Form
{
public MainForm()
{
InitialiseCommon();
}
public MainForm(string Somestring)
{
mSomestring = Somestring;
InitialiseCommon();
}
private void InitialiseCommon()
{
InitializeComponent();
PrepareSomeStuff();
SomeMoreStuff();
}
}
At least this was the way I was taught to do it in my C++ lectures years and years ago when I was at university. I've just updated the idea for C# and it works nicely.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
You can just stick all of your initilization code within your base class and in your derived class call base within the constructor. This way, if your InitialiseCommon method changed names you would only have to change it in your base class, not in every class that it is called. See my post for an example.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Nick Parker wrote:
You can just stick all of your initilization code within your base class and in your derived class call base within the constructor
My understanding was that it was common functionality within the same class, no inheritance involved (although the example given was possibly misleading in this respect as it was inheriting from the Form class, it wasn't part of what was needed.)
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
I didn't catch that he didn't want to use inheritance.
- Nick Parker My Blog | My Articles
|
|
|
|
|
I've done this as well. Was just wondering whether there is a cleaner way of doing it in C#.
Thanks for your reply.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
It's really a matter of consistency vs. performance. Putting all the common code in your default ctor and calling that first will ensure that your code is always code and you only need to change it in one place. If you use public ClassName(string name) : this() , it results in two whole extra IL instructions!
Get the idea?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
I am trying to do two things with the RTF control that I thought would be simple.
Firstly when the user presses the return key, the contents of the textbox is sent to be displayed, however this causes the text insertion cursor to move down one line, and stay there. I can't seem to get it move, even using Clear(); and ResetText();
if (e.KeyCode == Keys.Return)
{
if (this.rtfCompose.Text.Length != 0)
{
// Clear
this.rtfCompose.Clear();
this.rtfCompose.ResetText();
}
}
However that doesnt move the cursor back up to 0,0.
Also, when the user presses Return, the text gets sent to another RTF control for display. However, once the text goes beyond the size of the control, everything that's added cannot be seen unless the user scrolls. How would it be possible to scroll the existing text up and keep the new text (Added to the bottom) in view?
Thanks!
|
|
|
|
|
You might want to mention in which event your checking for the return key. This should actually be processed in the control's ProcessDialogKey method - which fires before the rich textbox recieves it. This means you have to extend the RichTextBox class and override that method. Don't forget to read the documentation about it, however, instead of just using it. There's things you need to understand about it, including when you override a control method you typically call it on the base class at some point (especially if it's not a message, character, whatever that you want to handle - the base class may need to process it).
As far as scrolling to the bottom, this is actually quite easy but you need to P/Invoke SendMessage and SetScrollPos , which are documented in MSDN[^]. You first send WM_VSCROLL (0x0115) using SendMessage to the other RichTextBox , using it's Handle property (the HWND for that window), along with the scroll information as defined in the WM_VSCROLL documentation. You may also need to call SetScrollPos to set the scroll position for the RichTextBox .
One final approach is to use ScrollToCaret , a public method defined by TextBoxBase (the base class for TextBox and RichTextBox ). Several people have had problems getting this to work and have had to use the approach above; as such, this has been covered here in the forums in the past, so if you'd like to read more about these threads, click "Search comments" immediately above the message board and try searching (I recommend using the keyword "WM_VSCROLL").
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
> You might want to mention in which event your checking for the return key.
I'm using the KeyDown event.
Overriding ProcessDialogKey is something I will research into. However P/Invoking and more advanced things like that are pretty new to me, so it'll take some learning about the basics before I'll be able to do that.
Thanks!
|
|
|
|
|
Hi all~
I would like to ask
I have written a dll by MFC
I would like to written a C# library as interface to use this MFC dll
How to achieve this result?
Is there anything need to change in the MFC code?
Thanks
|
|
|
|
|
Either go through all the problem of adding class factories to your unmanaged MFC DLL in order to use the CallingConvention.ThisCall calling convention with the DllImportAttribute (see a previous post here[^] for the gory details), or - much more easily - write an assembly using Managed C++. In a mixed-mode MC++ assembly, you still have access to all the "unmanaged" C/C++ APIs, including your MFC classes. Wrap those in managed (__gc ) classes and compile your MC++ DLL with the /clr switch. This will produce an assembly that can be used by other managed projects, since all managed code compiles down to Intermediate Language (IL).
Microsoft MVP, Visual C#
My Articles
|
|
|
|