Button with a CheckBox - c#

We have a requirement to create a button with a CheckBox embedded in it.
After trying various option, we found an option of having a CheckBox with an appearance of Button but that does not suffice as we need to have an event being fired when the Button is being clicked but CheckBox.Checked state will be used for other modifications.
Can someone guide me on how to proceed with this task?

If you really want to you can do something like this:
checkBox1.Parent = button1; // make it large enough
checkBox1.Location = new Point(5, (button1.Height - checkBox1.Height) /2 + 1) ;
checkBox1.TextAlign = ContentAlignment.MiddleLeft;
button1.TextAlign = ContentAlignment.MiddleRight;
Make sure to set Texts and alignments for both to prevent clashes. Also make sure to test if they act as intended..!
You can place the Button wherever you want, align it to the right or clear its Text..
Of course you can wrap it in a class, if you need it repeatedly..

Related

Best way to put two Controls on each other and manage their visibility - C#

just for example:
I have two text boxes, both should be in the same location of the form and both are in the same size.
I need to make visible one of them due to a contrition.
OK, everything it perfect:
if (condition)
{
txtBox1.Visible = false;
txtBox2.Visible = true;
}
else
{
txtBox1.Visible = true;
txtBox2.Visible = false;
}
but there is a small problem in design mode! I can not handle and select one of text boxes easily in design mode, one is send to back and one is send to front and accessing them will cause troubles!
How can I manage such situations with control ordering and design? Is my way putting two control on each other right? is there any better way?
One option is to put one TextBox in the location where you want it (let's say textbox1 will be in the right location) and put the other textbox nearby on the designer. This way you can easily select them at design time.
Then, in your Form_Load event, you just put textBox2 at the same location as textbox1:
private void Form1_Load(object sender, EventArgs e)
{
textbox2.Location = textbox1.Location;
textbox2.Visible = false;
A better question might be, why are you doing this? A textbox is a textbox, and you already have one in the position that you want. A textbox is there to collect user input. Why show and hide another one in the same place? The user won't know the difference, and your program state will presumably not care the name of the control in which the user has entered data...
There are multiple ways you can select a single control out of these two. First, by hitting the tab key until you land up on the control you want to select. To optimise the selection you need to first select a different control whose tab order is smaller than both of these.
Second, you can select any control and hit F4 to open properties window. On top of properties window there is a dropdown where you get to see the Control name. You can then choose the control of your choice from this properties window and make changes to properties.
you can do somethings like this, Here is the sample code,
txtBox1.Visible = false;
Point loc = new Point(50, 60); //new location point
txtBox2.Location = loc; //changing location of txtBox2
txtBox2.Visible = true;

How to make a listbox appear on the form at the click of a button?

I'm a university student therefore I'm not sure on everything to do with writing code. If you could provide hints or a bit of help. I have hidden the listbox via the designer. I've tried listbox1.Show under next button event handler. I've tried looking around on the web but I'm getting no where.
Now answered. Thank you
The solution depends on how you've hidden your listbox. If you did set visible property to false, just use listbox1.Visible = true;.
If you used ' Send to back' to hide it behind another control, you can use listbox1.BringToFront(); to set it into the foreground.
See https://msdn.microsoft.com/en-gb/library/system.windows.forms.control.visible.aspx and https://msdn.microsoft.com/en-gb/library/system.windows.forms.control.bringtofront.aspx
Inside Button_Click Event write:
listbox1.Visible = true;
In my opinion, the best way to show/hide controls (in WPF) is to collapse them. This allows the rest of the controls to behave as if the collapsed control does not even exists, until it is made visible, of course.
This would be done like so:
control1.Visibility = Visibility.Collapsed;
control1.Visibility = Visibility.Visible;
If you are using WinForms, controls will not have a collapse option, and the correct way would be as Almansour has said.

Tags in WinForms TextBox

I want to create a textbox on a WinForms form, where the user cannot input text directly. Instead, the content of the textbox should only be "bubbles" (with a "delete" button), showing a text value.
I'm struggeling to find the correct term for this kind of control/behaviour. It should look a bit like the "Tags"-field on StackOverflow when creating a new question.
Are there any existing controls/settings that allow such behaviour? (I have DevExpress if that helps)
Sorry for the vague question, if i knew better terms for what i'm looking for, i'd probably find something...
Instead of a textbox, the container for your bubbles should most likely be a Panel.
You can style it as needed, set the border, background color, etc.
If you don't want to manually position the "bubbles" inside it, use a FlowLayoutPanel. It will automatically put it's children controls in a flow.
Check out the properties of the control to specify how you want controls to be laid out.
The individual bubbles can also be Panels or other container controlls, so that you can add a label and a button (or image to serve as a button) to each.
You might even extend the panel class to automatically add a label and a delete button to each.
something like this (please note this is more like pseudo code. I wrote it up of the top of my head here, some adjustment may be needed)
Public Bubble : Panel {
Public Bubble(string text) {
Label title = new Label { Text = text };
Controls.Add(title);
Button delete = new Button { Text = "Delete" };
Controls.Add(delete);
//also hook up events here, ie delete.click+= whatever
}
}
You can further extend the custom class for your specific needs.
Set styles on the button and label as needed to achive the look you want.
Don't forget to hook up events such as mouse over, button click, etc.
Then just fill the FlowLayoutPanel with these custom controls and you should be good to go

How to change the properties of a textbox in C#?

I am a beginning C# noob and I am making my own wordpad/document creator, and I want to change the property of a textbox via the code. What I am trying to do is have three buttons: Left, Center, and Right, and I am trying to make them so that they change the "TextAlign" property, when they are clicked on. Does anyone have some suggestions? I hove done some research and turned up with nothing.
textBox1.TextAlign = HorizontalAlignment.Center;
textBox1.TextAlign = HorizontalAlignment.Right;
textBox1.TextAlign = HorizontalAlignment.Left;
Give a name to your TextBox such as tbContent and then add three buttons, again name them appropriately such as btnAlignRight, btnAlignCenter and btnAlignLeft.
Now, go to your .cs file and then Add event handlers for your three buttons through the designer. Shortcut: Just double click on the buttons one by one when in the designer, it will automatically generate and register the Click EventHandler for that button.
And then add the code provided by badkip in the appropriate EventHandler

c# - create invisible user control

I need to create a user control in C#.Net, which can be added to the application without being visible - just like the FolderBrowserDialog. It's a new window which I'll be using often so I think this is the right way. The window will be opened by envoking the showDialog-Method as known from the other dialog.
Any Idea?
Thanks and regards,
Daniel
Since all these "invisible" controls derive from Component class, you should start by reading the MSDN article on it: http://msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx.
simply set Visible to false or isn't this what you're asking for ?
A UserControl is by definition not a Form; I think what you really want is a Component. That said, couldn't you really just create a new Form class that has the functionality you want? Whenever you want to display it, create a new instance and call ShowDialog. Or, if you want to preserve state, add an instance as a class member to your parent form, call its Show method whenever you want to display it, and add an event handler to its FormClosing event to check:
if (e.CloseReason == CloseReason.UserClosing)
and, if so,
e.Cancel = true;
Hide();
(This last part is to prevent errors if the user closes the form and then tries to display again after it's been disposed.)
I think more information may be needed, but if your crating a custom user control, the control should have a .Visible property. The follow is an example of how a button can be located on the form but hidden from a user.
button.Visible = true; // shows the button
button.Show(); // Shows the button
button.Visible = false; // hides the button
button.Hide(); // Hides the button
While the button may still be on the form/control, it will not be interactible by the user. You can still perform programmatic control on the button, but essentially it is not a user control while it is 'hidden'. If you want there to be a sort of hidden button that the user can click you will need to do other things to obtain this but It doesn't should like that is what you want.
This show/hide thought process sounds a lot like pains and confusion leftover from classic VB. The old form methods of show and hide, etc., were confusing and often left me as a developer in a position to not know whether an object existed or if was merely invisible. And checking was only trivial if you used On Error Goto to prevent a null reference. So right off I would advise not to think in terms of visibility unless you are doing something with a web page and need to maintain space and state.
First, create a Windows form and add it to your project, assuming that is the type of project that you are describing. Decorate the form with the proper controls, and where applicable, create properties to allow public access to the control values. Also set the DialogResult property of the buttons that either "OK" or "Cancel" the form. Give it an appropriate border style of either Fixed3D or FixedDialog. Maybe also set the property for where you want the form to appear on startup -- center parent, center screen, Windows default, etc. The event handlers for both "OK" and "Cancel" should invoke this.Close(); to close the window.
From the calling point in the code, here's some hypothetical code to get you going in the right direction. Write something like this in the place where you want to invoke your Dialog.
int intResult = 0;
string strResult = null;
MyDialogForm frm = new MyDialogForm();
frm.Title = "Select an Item";
frm.SomeProperty = 0;
frm.SomeOtherProperty = true;
if (frm.ShowDialog() == DialogResult.OK)
{
intResult = frm.Result;
strResult = frm.StringResult;
}
else if (frm.ShowDialog() == DialogResult.Cancel)
{
// User clicked the cancel button. Nothing to do except maybe display a message.
MessageBox.Show("Canceled Task");
}
...
// Somewhere further on down, but within scope, simply repeat
// what you just did, but without having to reinstantiate the
// form Window. But if you make it that far within the same
// scope, this method might be too busy and may need to be
// factored down.
So in short:
Scrap show/hide -- its not a good
practice.
Save the form data without
using an invisible form to save it;
that's the class's job.
If the UI requires a lot of flipping back and
forth between windows, check your
design for other alternatives for
solving the original problem. Maybe a design pattern such as MVC is for you, depending upon the size and complexity of your application.
Sound good?
You can put that control in a Panel. Set the panel height = 0 visible = false when you dont want to show the control.
And do the vice versa when you want to show it.
Derive from Control, not UserControl, and in the constructor set Visible = false.
Also create an event handler in the constructor.
VisibleChanged += new EventHandler(SetVisibleFalse);
Create a method named SetVisibleFalse.
private void SetVisibleFalse(object sender, EventArgs e)
{
if (Visible) Visible = false;
}

Categories

Resources