How to change the properties of a textbox in C#? - 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

Related

Hide button text behind button image

Is there any method to hide a button text behind button image like bring to front or send to back option?
I only need to hide or show button image only as I have a code that coverts the original text CloseButton.text = "&Close"; to CloseButton.Text = "&Cancel";
to perform another command so I can't use CloseButton.Text = "";.
Tried this link - WinForms button with image and text but my button size is too small that it would only show the text and not the image no matter how I mix and match TextAlign and ImageAlign.
Any help is much appreciated. Thanks in advance.
Sample Button Size below:
Check this
Place Textbox in Button and set textbox.visible=false method
Is there any method to hide a button text behind button image like
bring to front or send to back option?
There is no such built in but you can simply clear out the text on click event of the controls. Example: if you have radio buttons for send to back then on click of that clear out the control text saying controlId.Text = string.Empty
As #Rotem posted in the comments.
Have your code behind use the Tag property rather than Text. Easiest way out is using properties for what they were made for.
Instead of using CloseButton.text = "&Close"; I changed it to CloseButton.Tag = "&Close"; and made my code worked around it to have the same function without placing an actual Text in my Buttons. Credit this asnwer to #Rotem. Thanks.

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.

Button with a CheckBox

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..

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 background color of button in windows phone application?

I am developing window phone 7 application in C# & silverlight 4. I am new to the silverlight.I have two buttons in my application for different purposes. I want to change the color of the button dynamically when the button gets clicked. So I am using the following code
IncomeButton.Background = new SolidColorBrush(Colors.Red);
But it is not working. Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.
Changing the styling of a button from it's own click event comes with a catch. Peter elaborates here.
Why can't I change the Background of my Button on a Click event? - Peter Torr's Blog
You can simply execute IncomeButton.UpdateLayout() after changing button's color.
I also ran into that "simple" problem. Instead of using Peter's "button styling change" thing, I simply placed a rectangle below the button and changed it's color with the Rectangle.Fill property in the Click event of the button. Works fine for me.

Categories

Resources