vb.net Adding DateTimePicker control to Derived TextBox class - vb.net

and thanks in advance.
I have created a class that is derived from TextBox and I want to add a DateTimePicker object to its right (ie outside of the dimensions of the control, on the owner control)
.
That means that I need to find the Controls collection that contains the derived Textbox class.
I don't want to use a UserControl because I have a chain of inherited/derived Textbox classes that have required validation etc.
At what point is Parent established in a control's creation?
I thought that I could access the Textbox's Parent in the New constructor, but it is Nothing. I thought I could use the OnPaint handler but that doesn't fire.

Related

VB.NET Exposing child control properties

For a custom control I'm making, the datagridview is pretty much only going to be a customized version of the original. However, so that other controls I have made can interact with it, I have made it inherit my MetroControl class.
Since I cannot add multiple "Inherits", I have placed a DataGridView and docked it inside the control. From here I can then influence and theme it. Most of the properties are handled by my theming (such as the "RowHeadersStyle" etc..
I need to push forward the "Columns" property of the DataGridView so that the user can interact with the same column setup screen that they would as if they were using the normal DataGridView. Is there any way I can simply forward this property (as it would make it much easier for both me and the user)?
Yes, you can add a property on your UserControl that just returns the Columns property on the DataGridView (Assuming that your DataGridView is stored in field named dataGridView):
public DataGridViewColumnCollection Columns {
get { return dataGridView.Columns; }
}
That's it

Base class to handle derived class's controls and events

I'm working on 4 winform forms that have 5 common combobox controls. The logic for filling and handling the combobox control events is identical on all 4 of the winform forms. The code is interrelated...so for example, combobox 1 controls filling combobox 2 and fills a few text boxes, combobox 2 affects combobox3...etc.
The reason I'd like to combine the code is that others have changed one or two of the forms without updating the others...so the code is becoming a mess. Unfortunately, the controls aren't located near each other...so I can't make some sort of composite control.
I was thinking of adding the common code in a base class that inherits Form, and then have the 4 forms inherit from the base class but I ran into issues accessing the events in the base class and the combobox code accesses other controls on the forms that aren't in the base class.
What is the best way to accomplish this (within the constraints of not being able to completely rewrite this in MVC, or ripping these forms completely apart, etc.)? Should I use a base class? If so, how do you access the controls in the derived class from the base class and how do you get the base class to respond to the event handlers?
I was thinking I could call MyBase.EventMethod for the derived class's event handler?

Can one move controls around in a form class derived from another form?

I have created a forms which are derived from another form thus:-
Public Class MyForm
' ...etc
End Class
Public Class MyDerivedForm
Inherits MyBaseForm
' ...etc
End Class
Public Class MyOtherDerivedForm
Inherits MyBaseForm
' ...etc
End Class
This works quite nicely and I can add controls to the derived form using the form designer. But I'd like to move some of the inherited controls around a bit on MyDerivedForm without disturbing MyBaseForm or MyOtherDerivedForm. I can't see any way of doing this on the forms designer.
Is it possible to do this (preferably with the designer but with code if necessary)?
This is not a typical VB.NET problem so not so sure what's going on here. You'll get the lock icon on the inherited controls and a grayed-out Properties window for an inherited control when the Modifiers property of the control in the base class is Private. The Winforms designer observes the accessibility of the base class member. Private members can't be messed with. The default value for Modifiers is Friend in VB.NET, Private in C#.
Make it Friend to allow the derived form class to modify the control properties. If the base form class lives in another assembly then Friend isn't good enough, you'll need Public.
The Anchor property can be an issue, but only if you anchor to the right or bottom. The control has a knack for ending up in the wrong spot when the derived form has a different size from the base form if the control is anchored that way. Simply avoided by not anchoring at the right/bottom in the base class and changing the anchor in the derived class.
In my VB.NET WinForms application I have inherited forms and I can just grab the inherited controls in the designer and move them about as I would with non-inherited controls on the form.
However I have noticed two things in the past that stop this. If you change the position of the controls on the base form, or change some of the positioning properties (such as anchor or docking), then this can (but not always) move your inherited controls. Also I couldn't move some of my inherited controls in an earlier version of .NET (2.0 I believe), but I never figured out the cause for that so I had to resort to changing the locations via the property grid.

Accessing to control inside of usercontrol

I am making an usercontrol with only one textbox so basically I expect that properties of textbox have to be applied to whole usercontrol.
That means (for example) that I would like from outside of usercontrol (in main program which contain those usercontrol) to get value of those (intern) textbox's selectionstart property.
But I can't find a way to do this except in creating a new public property of my UC.
I suppose that here should be some better way because textbox have many properties which may be needed to read (or maybe write) from main program and making another public property in UC for every internal property of textbox don't seems like "way to go" for me.
Any advice on how to get properties of internal control in UC from main program?
you may inherit your usercontrol from textbox control,liKe:
Class MyTextbox
inherits TextBox
End class
and then try to override, overload and access the events and properties you want.
you can check the following links,
1- For textbox inheritance example
2- Answered Question in Satckoverflow
Will not a regular FindControl check solve this?

Databound User Controls in .NET

I am looking for some information on how to properly implement data binding on a user created control.
My UserControl contains a Textbox, a Button, and a MonthCalendar. I am able to databind to the Textbox inside of my user control, however, I want to implement the databinding on the UserControl itself, and not reference to textbox inside the control.
I have attempted to set a Property as follows:
<System.ComponentModel.Bindable(True)> _
Public Property BoundDate() As DateTime
Get
Return _currentSelectedDate
End Get
Set(ByVal value As DateTime)
SetDateTime(value, True)
End Set
End Property
However, when I add a binding source to the control, the field does not populate with the data, it remains blank. Do I need to do something to make the data appear afterwards?
Can anyone direct me to a good tutorial, or if possible, explain it here.
The project is written in VB.NET.
EDIT: I am implementing the DefaultBindingPropertyAttribute
What is the textbox bound to at this point? I'd suggest the following:
In the user control load event, declaratively bind the text box to a private member variable, e.g. private _boundDate as DateTime
Have you setter in your boundDate property update _boundDate
This looks like a pretty good read, although I haven't looked at it myself