I don't know the usage of the SetBoundsCore and if any sample program it is comfortable.
I have googled but didn't get one. Is it used for retain the same value?
For example, if set the height as 100 for first time it will remain the same and if i set the height as 200 it will not change again.
I assume like this.
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width,height, specified);
}
You can look at MSDN
Basically it sets coordinates and size of your control. Since it is protected and virtual, you can only call it from your user control like you specified in your question.
Difference between public method SetBounds() and SetBoundsCore() is here:
What is the difference between SetBounds and SetBoundsCore
Related
I'm using the InputBox from the VB .dll. When I display it, I want to put it in a particular place relative to the controls it will have an impact on (out of their way). So I have this pseudocode for showing the InputBox ("selectionStart" is a Point assigned to on MouseDown):
int HeightOfInputBox = ? <- What is this value?
int XPos = selectionStart.X;
int YPos = selectionStart.Y - HeightOfInputBox;
Interaction.InputBox("Prompt", "Title", "DefaultResponse", XPos, YPos);
My question is: What is the height of an InputBox?
You do not have access to the object to get the Height or Width information. The only properties available are what you set when you initialize it. If you need the Height you will need to roll your own. This CodeProject article is an example on how to, you could just add another intializer to set the Width and Height of the InputBox.
i.e. InputBox("Prompt", "Title", "DefaultResponse", XPos, YPos, Width, Height);
you can use
Me.ActiveControl
for the current control
Hello Community,
i've a problem with the height of the System.Windows.Forms.Combobox-Control. I can't change it. I want to use that to write an own implementation (owner drawn custom control).
The following code does not work for me (It's only to try). The height is still 21px!
public class TestBox : ComboBox
{
public TestBox()
{
DropDownHeight = 15;
}
protected override Size DefaultSize
{
get
{
return new Size(15,15);
}
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, 15, 15, specified);
}
}
Please help me.
Regars,
Marco
The ComboBox height should be resized
based on the font that is assigned to
it.
So, change the combo font. see another discussion on this subject.
ComboBox's MinimumSize property is coded like this:
public override Size MinimumSize
{
get
{
return base.MinimumSize;
}
set
{
// can see that Height is not taken in consideration - is 0
base.MinimumSize = new Size(value.Width, 0);
}
}
I am trying to create a series of UserControls that all inherit from a custom UserControl object. One of the key behaviors I want to implement is the ability to dynamically resize all of the controls to fit the control size.
In order to do this, I need to get the initial width and height of the control to compare it to the resized dimensions. In my inherited controls, I can put code in the constructor after the InitializeComponent() call to grab the dimensions. Is there any way I can do this from the base object code?
Also, if there is a better approach to doing this, I am open to suggestions.
I ended up using my base control's dimensions as the fixed size for all inherited controls. This was fairly easy to do by overriding the SetBoundsCore() method:
public partial class BaseControl : UserControl
{
private int _defaultWidth;
private int _defaultHeight;
public BaseControl()
{
InitializeComponent();
_defaultWidth = this.Width;
_defaultHeight = this.Height;
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
if (this.DesignMode)
{
width = _defaultWidth;
height = _defaultHeight;
}
base.SetBoundsCore(x, y, width, height, specified);
}
}
Any controls inherited BaseControl automatically default to its fixed dimensions.
At runtime, my resize code calculates a resize ratio based on the new Width and Height vs. the _defaultWidth and _defaultHeight members.
In the user control, take advantage of the docking and anchoring properties of every control in the container. When the user control is sized or resized, the contents should adjust themselves automatically. Then in code, all you need to do is set the size of the user control.
I'm working on a custom user control. How can I prevent the HEIGHT ONLY of the control from being modified during the design-time interface.
You can override the SetBoundsCore method and disallow changes to height by changing the height value before calling the base class implementation.
private const int FixedHeightIWantToKeep = 100;
protected override void SetBoundsCore(
int x,
int y,
int width,
int height,
BoundsSpecified specified)
{
// Fixes height at 100 (or whatever fixed height is set to).
height = this.FixedHeightIWantToKeep;
base.SetBoundsCore(x, y, width, height, specified);
}
You can override the Height attribute from the Control class and then set the BrowsableAttribute to prevent it from being displayed in the properties windows
You can also take a look at Attributes and Design-Time Support
I am starting a winform application[.NET 3.5, C#], where in the the main form of the application starts at a particular specified location. Am calling the following code in the constructor for this
private void SetFormPosition()
{
this.StartPosition = FormStartPosition.Manual;
this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
}
After the application starts, I would like to keep the location of the form fixed throughout the application lifetime.
Perhaps, I could 'tap' the Location event changed but am not sure if that would be very elegant.
Please suggest.
Thanks.
I agree with others that you probably shouldn't be doing this, but if you must, read on.
You can override the SetBoundsCore method and prevent any movement. We use this to prevent vertical resizing on some UserControl implementations (such as those that contain a ComboBox or other fixed height control), but it is also responsible for the location changing.
The following should get you started:
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
x = this.Location.X;
y = this.Location.Y;
//...etc...
base.SetBoundsCore(x, y, width, height, specified);
}
You could set the FormBorderStyle to None. This has the added benefit of removing the bar at the top of the window that would give users a false sense that they should be able to move the window.
Just change this
Location = new Point(this.Width,this.Height);