How do I create an invisible checkbox that remains clickable? - c#

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.

Related

removing an object from a control box

I want to change the forecolor of radio buttons texts when the Group box Enabled property is set to false. The disabled color is so absurdly dark that you can't read the text in this state. I tried many things (like overriding the system default disabled color mode) without success.
I have no choice but to use this kind of controls, so I'm looking for a workaround. My idea was to superpose a lightly greyed label on the text, but this cause errors when initializing the control box.
I tried the following code to get the label out of the parent control box and avoid this problem:
RadioButtonLabel.Parent= Main_menu.Activeform;
I have no more errors, but the label now basically disappear in runtime.
Any solution?
instead of using dark color, try using this: (this is just an idea code)
if(groupbox.enable == false)
{
radiobutton.text = ""; //it means show nothing
}
else
{
radiobutton.text = "Whatever you like";
}

Creating popup without possibility to tab to a background element

I am trying to create a popup but when it is open it is still possible to use the tab key to switch the focus to an element in the background (e.g. to a button and use space to press is). The only way I found until now is to check on every lostFocus event (which also fires for every element contained in the Border element) and check if the focus is now in a element inside the Border. If not I manually set the focus.
Is there a nicer way to keep the focus within the Border (or a Grid,...)
I'm working on a Windows 8 App.
Do you mean that using a Modal Dialog with Form.ShowDialog(Owner) still allows you to focus the parent components with Tab?
Can you give a sample of your code call?
Form2 form = new Form2(); //Make an instantiation of your Form
form.ShowDialog(); //ShowDialog()!!! NOT form.Show()!!! Or anything else :/
A few ideas:
Set Enabled to False on the background visual tree, though that might change the way things look if you still want to show them partly
Set IsHitTestVisible to False to disable pointer input
Use RenderTargetBitmap.Render() if targeting Windows 8.1 to render the content of the background to an image and simply replace all that visual tree with an image of it

Problems Making DevExpress SimpleButton Visible At Runtime

I am working on a C# WinForms application that uses some DevExpress controls. I am struggling to figure out why I cannot make a hidden SimpleButton visible at runtime by setting its' Visible property to true. I've attempted to give the control focus, refresh the control, refresh the form to no avail. One thing that I have noticed in the debugger is that after the statement btnAddJob.Visible = true, the Visible property is still false. Any ideas?
public AddPredefinedJobsForm(WorkOrder workOrder)
: this()
{
currentWorkOrder = workOrder;
// Here I am just getting the position to display the button
btnAddJob.Location = new Point(btnNewJob.Location.X, btnNewJob.Location.Y);
// Hiding the button that my hidden button will replace below
btnNewJob.Visible = false;
// Give my hidden button focus
btnAddJob.Focus();
// Make my hidden button Visible
btnAddJob.Visible = true;
// Refresh the button
btnAddJob.Refresh();
// Refresh the entire form
this.Refresh();
}
If you have the button inside of a container control (like a Panel), you'll need to set the container's visibility to True in order for its child controls to be visible.

Could Label get a focus?

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.

Set a disabled TextBox's ForeColor to be the same as its BackColor in C#

How do I set a disabled TextBox's current text color to be the same as its current background color in C#?
Simply doing txtLala.ForeColor = txtLala.BackColor does not seems to work.
This works:
txtLala.Text = "Red";
txtLala.BackColor = System.Drawing.Color.Red;
txtLala.ForeColor = txtLala.BackColor;
txtLala.ReadOnly = true;
Try setting the color, before the readonly. And also check how you are setting the color!
EDIT
Try this
txtLala.Attributes.Add("style","background-color:Red;color:Red");
If you are trying to make it invisible, you know you can set it as
txtLala.Visible = False;
EDIT II
I finally tried
txtLala.Enabled = false;
... you see that grey shadow color! I don't think you can mess with that, it looks to be a browser property setting.
Why not set as ReadOnly or Visible = False?
Maybe you have a good reason for Enabled = false
But you should note:
Use the Enabled property to specify or determine whether a control is functional. When set to false, the control appears dimmed, preventing any input from being entered in the control.
Note The ability to enable or disable functionality is always available. However, dimming and locking the control only works in Microsoft Internet Explorer version 4 and later.
This property propagates down the control hierarchy. Therefore, disabling a container control will disable all child controls within that container.
Note Not all controls support this property. See the indivual controls for details.
It seems to only work for TextBox that is read only. If it is disabled (.Enabled = false). It does not seems to work.
If this is a readonly textbox, you need to explicitly set your BackColor first, then your statement will work.
txtLala.BackColor = System.Drawing.SystemColors.Info;
txtLala.ForeColor = txtLala.BackColor;
Ref: http://bytes.com/groups/net-c/233961-read-only-textbox
Then again, if it's readonly, a label might be better. If you're trying to hide it, perhaps setting .Visible = false would be better still.
Edit: This seems to be a common question on the web. With respect to winforms: This site suggests dropping the box into a frame and setting Enabled = false on the frame, not the textbox. Once you do that, you may be able to maintain control of the forecolor.

Categories