I'm creating an application in Forms for very non tech-savvy users. In doing so, I'm attempting to keep some more complicated buttons and menus hidden in the main program unless an invisible checkbox is checked- which only the QA/Dev team would need to use for troubleshooting.
I've attempted to use checkBox1.Hide() followed by checkBox1.Show on click as well as on CheckedChanged, however when the checkbox is hidden or has visibile set to false, the checkbox is unable to be checked. I've also looked at the checkbox's properties window in the Form design, but setting the bordercolor to white or the bordersize to 0 under FlatAppareance had no effect.
Any suggestions? Thanks for the help.
I agree with the comments this is not a good practice when designing a user interface, but there is a way to make an invisible button in winforms.
in your constructor or in a method set the properties of the button like so
button1.FlatStyle = FlatStyle.Flat;
button1.FlatAppearance.BorderColor = BackColor;
button1.FlatAppearance.MouseOverBackColor = BackColor;
button1.FlatAppearance.MouseDownBackColor = BackColor;
this will render a invisible to the user button that can be clicked. that is if your click event is already set up.
I have a TabControl where I want to keep the tabs to a fixed size and I want icons in the tabs. I have set TabControl.SizeMode = Fixed and TabControl.ItemSize = 100, 18. I have also set TabControl.ImageList and am assigning images to the tabs via TabPage.ImageKey.
Here is what it looks like if I comment-out assigning the ImageKey:
And here is what it looks like if I am assigning the ImageKey:
Is there some sort of "alignment" for the icons? I want them to be on the far left in the blank space, but instead they are starting where the text starts. Any suggestions?
(BTW - if I set TabControl.SizeMode = Normal, I get the tab content the way I want it, but the tabs aren't a fixed size):
I can verify the issue that you are seeing with TabControl.SizeMode = Fixed (on Windows 10). I initially seen it in the designer when configuring a TabPage with an icon. However the irritating thing is that the issue corrected itself if the designer is closed and reopened. This suggests a window style setting of some sort and there are some Tab Control Styles set in the CreateParams Property based on the SizeMode Property. However, I found no solution in attempting to apply the TCS_FORCEICONLEFT style. If the ImageIndex property is set prior to the control being shown, then the alignment is as desired. So I figured that there must be something being configured on handle creation.
If you call the form's RecreateHandle method after setting the TabPage.ImageIndex property, the form redraws and all looks good. However this cause the form to blink. Calling the Control.RecreateHandle method on the TabControl also works. This is a protected method and would necessitate using a derived TabControl to expose the method or you could use Reflection to invoke the method.
public class MyTC : TabControl
{
public void FixIcon()
{
RecreateHandle();
}
}
I have a usercontrol that I'm adding as a control of a main form dynamically. The Mainform is basically empty, except it has a large status bar on bottom.
Problem is, when I set the Dockstyle.Fill option on my usercontrol, the size of the loaded usercontrol extends beyond the statusbar (It fills the entire main form as if the status bar wasn't there).
How do I prevent this behavior? This is an example of how I dynamically load my form
logicForm = new LogicForm();
this.Controls.Add(logicForm);
logicForm.Dock = DockStyle.Fill;
I think you need to set the DockStyle to None and use the Anchor property instead.
Set the anchor to Top,Bottom,Left,Right and size your control to fill all the space up to the status bar.
You should find when you run that the user control will then resize with the form.
I just found the solution
I need to bring the form to front in order to properly dock it if I already have some other controls on the main form:
logicForm.BringToFront();
Found here: http://dotnetref.blogspot.kr/2008/08/using-dock-fill-on-control-when-you.html
-________________________-
I have a one question on my university's test about C#. Could label get a focus? As I can see on MSDN site, all Controls can get a focus, but some of them aren't selectable. So it's seems to me that the right answer is "Label could get a focus, but couldn't be selected". Also Label has a Focus() method. Please, help me understand. Thanx.
Yes there is a Focus() method on Label and yes it is absolutely right it works; but behave differently. let me try to explain
A Label can be associated with some one input control, for instance a label for a user name text field, so there is concept of Associated Control with the label. AssociatedControlID on msdn
So you can associate an input control with a label and whenever label is selected the control passed to the associated input control.
Example here click on Email or Password labels in login box and see what happened, similarly if you call focus method on the label the focus will passed to the associated control.
From the documentation:
A control can be selected and receive
input focus if all the following are
true: the Selectable value of
ControlStyles is set to true, it is
contained in another control, and all
its parent controls are both visible
and enabled.
Since a Label control is not selectable, it cannot receive input focus, even if it inherits a Focus() method from Control. Therefore, the answer is no.
It's easy to findo out if a control's ca get focus. Just read the
.CanFocus
property which is inherited from the Control class.
The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable. (see MSDN documentation)
Panel
GroupBox
PictureBox
ProgressBar
Splitter
Label
LinkLabel (when there is no link present in the control)
Also:
The Focus method returns true if the
control successfully received input
focus. The control can have the input
focus while not displaying any visual
cues of having the focus. This
behavior is primarily observed by the
nonselectable controls listed below,
or any controls derived from them.
A control can be selected and receive
input focus if all the following are
true: the Selectable value of
ControlStyles is set to true, it is
contained in another control, and all
its parent controls are both visible
and enabled.
If you need a Label-like control that you can focus, just use a TextBox and make it readonly. Set a few other properties (styles, not selectable etc.) and you're done.
You will see that there is a read only property called CanFocus on a label, if you have a look at this property while debugging you will see it is false.
Every control that inherits from Control has the focus method, but that does not mean that it can be focused.
Label does gets the focus but it escalates it to the input field specified in its "for" attribute. Like:
<label for="firstname">First Name</label><input type="text" name="firstname" />
In this scenario if you click on the label it will throw the focus to the input field "firstname" associated with it.
This is a year old, however I had a similar issue as the Op. In my case it was a user control that had a single label (docked at fill) on it (it has other functions behind the scenes - it is a calendar control and pops up a date picker - not the standard one - in either a panel (popunder) or a form (popup)).
The issue there was that UserControls are really intended as containers and resist focus (pushing it off to child controls) - as the label is the only child control, it stops the UserControl getting focus. Using readonly TextBox is a poor substitute as it lacks vertical alignment and must be multiline to size the height.
The reason I add the following as an answer here is because it IS possible (sorry guys who said here it is not) and I found this post and many like it that were little help when I looked. Anyway, the way to do it is to override the Label and set the SetStyle - also the OnPaint to draw a focus rectangle (I manually drew mine as DrawFocusRectangle didn't seem to do anything) - so as below:
internal class SelectableLabel: Label
{
public SelectableLabel():base()
{
SetStyle(ControlStyles.Selectable, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//ControlPaint.DrawFocusRectangle(e.Graphics, ClientRectangle);
if (!Focused) return;
if (BorderStyle == BorderStyle.Fixed3D)
{
e.Graphics.DrawLines(Pens.CadetBlue, new[] { new Point(1, Height - 1), new Point(1, 1), new Point(Width - 1, 1) });
e.Graphics.DrawLines(Pens.Aquamarine, new[] { new Point(2, Height - 1), new Point(Width - 1, Height - 1), new Point(Width - 1, 2) });
}
else
{
e.Graphics.DrawRectangle(Pens.Aquamarine, 0, 0, Width - 1 , Height - 1 );
}
}
}
I am not concerning myself on whether it is accademically (purist view) right to do so, but that there are valid reasosn to allow an output control (like label) to sometimes gain focus.
I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?