Keyword Box Form C# - c#

I want to make keyword box like this, and is Richtextbox able to do it?
I only need to enter a keyword that ends with a comma, then it will automatically become a keyword box like that
I could ask ChatGPT, but it's too popular right now too much traffic and I can't enter it now
Thank You

Your difficulty here is how this label should be implemented. It is recommended that you use custom tags.
Let's split the control first. The whole control can be regarded as a selected label interface.
There are many small tabs in the selected tab interface, which are composed of one by one.
One: We focus on implementing the label control in the selected label interface
The label control can inherit UserControl and then add a label control to the UserControl interface to simulate the close button.
public partial class TagControl : BaseRadiusUserControl
Then write a defined close event
[Description("Close Event "), Category("Customize ")]
public event EventHandler CloseClick;
Click X on the label control to trigger the close event
private void close_Click(object sender, EventArgs e)
{
CloseClick?.Invoke(this,e);
}
Then write an assignment method to adjust the size of the control when the text value is greater than 2.
public void SetText(string text)
{
this.Content = text;
if (text.Length > 1)
{
this.Width = 60 + (text.Length - 1) * 20;
}
}
In this way, the small label control is realized!
Two: The selected tab interface uses the flowlaoutpanel control

Related

How to show search and clear icon into textbox at right end in winforms in c#

I wanted to show my search and clear(cross mark) icon into textbox that i had designed to search my treeview. When user enter into textbox and writes some text into it i want my cross icon to be visible and when nothing is written inside of it i wanted to show search icon.
What i have done so far is something like this.
but i wanted it to be like this when nothing entered.
and when text is entered i wanted to be like this ->
also when user clicks cross mark i wanted textbox to get empty.
Thankyou!
If you can live with the icon sitting above the text you can do this:
Add a Panel IconPanel and nest it in the TextBox textBox1:
Panel IconPanel = new Panel();
IconPanel.Parent = textBox1;
IconPanel.Size = new Size(32, textBox1.ClientSize.Height); // use your numbers!
IconPanel.Location = new Point(textBox1.Width - IconPanel.Width, 0);
IconPanel.BackgroundImageLayout = ImageLayout.Stretch;
Add your icons to the project resources and code the TextChanged event like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
IconPanel.BackgroundImage = textBox1.Text.Length == 0 ?
global::yourProjectName.Properties.Resources.searchIcon :
global::yourProjectName.Properties.Resources.clearIcon;
}
Initialize the IconPanel.BackgroundImage depending on your intial Textbox content!

How can I save a dynamically created user control if it contains some buttons in c#?

I am new to C#. I am using windows forms and I have Form1 which contains 2 buttons ( one to create user control at run time and the other creates buttons on user control at run time).
This code creates user control and FlowLayoutPanel (to organize button position) if you click add_UserControl button. And then it creates buttons on FlowLayoutPanel if you click Add_Buttons button and it is all done at run time.
Now in Form1 let's say I created user control and FlowLayoutPanel and then created 5 buttons , how can I save the properties/details of this user control with its FlowLayoutPanel and 5 buttons in SQL database so I can use them later when I run the program? I have been thinking about an idea and I reached the internet but no luck.
Any idea? Please help me. Thank you
public partial class Form1 : Form
{
FlowLayoutPanel FLP = new FlowLayoutPanel();
UserControl uc = new UserControl();
private void add_UserControl_Click(object sender, EventArgs e)
{
uc.Height = 700;
uc.Width = 900;
uc.BackColor = Color.Black;
Controls.Add(uc); //add UserControl on Form1
FLP.Height = 600;
FLP.Width = 800;
FLP.BackColor = Color.DimGray;
uc.Controls.Add(FLP); // add FlowLayoutPanel to UserControl
}
private void Add_Buttons_Click(object sender, EventArgs e)
{
//####### add buttons to FlowLayoutPanel ############
Button dynamicButton = new Button();
dynamicButton.Height = 50;
dynamicButton.Width = 200;
dynamicButton.BackColor = Color.Green;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Text = "";
FLP.Controls.Add(dynamicButton);
}
}
OK, First you need to create a class that will represent one of the buttons with the properties you need.
class MyButton
{
public string ButtonText {get;set;}
}
Everytime you click and create a button, you actually create an object of this class and add it to a collection or list. Then you would have some other code watching over the collection, and every time it gets a new entry, it creates a new button and sets its Button text to the text property. when a member of list is gone, it removes the button.
If you need more properties to be remembered (color, size, font, ...) you add them to the class as well. If you need for other controls, as well, .... you can always create common parent controls.
Simple.
If you want to be able to reload it, you could define the MyButton class as serializable and store it in xml file, and upon build, reload it.
You should watch into WPF and it's MVVM pattern. It's pretty much similar to it. Also have a look into command pattern, usefull pattern when it commes to this.
You can remember the FlowLayoutsPanels in one SQL table and in another table you could save the buttons which belong to these FlowLayoutPanels.
On Form Load or Application Load, you could check if there are already FlowLayoutPanels and correspending Buttons do exist in the SQL db and if yes then create them, else do nothing.

How to auto center textbox inside user control, when user control is resized?

This issue is concerning C# WinForms ... I have custom user control which basically has 3 things: A label, a text box, and a list box, all arranged vertically like this:
I have written a small Custom ControlDesigner class to prevent the user from changing the height of this user control at design time.
I also have set the textbox and listbox to anchor to left/right locations, so that when the user changes the width of the user control, these two internal controls resize beautifully.
I want to have the Label is the horizontal center as well, but anchoring that to left/right doesn't seem to work. I'm thinking I might have to written some sort of custom resize function inside the custom control designer class, which automatically places the label in the middle when the user control is resized ?
Also note that I want to label to still remain in the center when the label text is changed.
So how do I do this ?
Here is my custom control designer class till now:
public class DropDownTextBoxDesigner : System.Windows.Forms.Design.ControlDesigner
{
public override SelectionRules SelectionRules
{
get
{
SelectionRules retVal =
SelectionRules.Visible |
SelectionRules.Moveable |
SelectionRules.LeftSizeable |
SelectionRules.RightSizeable;
return retVal;
}
}
}
Write a SizeChanged of the User Control event like so:
private void UserControl1_SizeChanged(object sender, EventArgs e)
{
textBox1.Left = Width / 2 - textBox1.Width / 2;
textBox1.Top = Height / 2 - textBox1.Height / 2;
}
In the resize event, can you just set the left property of the label to center it in the container?
Label.Left = (Container.Width / 2) - (Label.Width / 2);

Making text popup in a C# forms application on mouseover [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
create custom tooltip C#
Does anyone know of a way to make a box 'popup' when the user cursors over a certain item?
For example, I want to have a PictureBox on a C# forms application and when the user cursors over it, a box of text will pop up.
I'm aware of ToolTip however I was thinking of something more customisable; in my mind I'm thinking of the kind of popup boxes you see in World of WarCraft when you cursor over an item in your inventory (obviously it doesn't have to be THAT flashy, but at least one where the text colour, background colour, text etc. are all modifiable).
You can use a ToolStripControlHost to host a control (for instance a panel) and add the content you want. Then you add that control to a ToolStripDropDown using the Items collection, and use the Show(Control,Point) method to show the control.
Thought I'd add an example
public class Form1 {
public Form1() {
ToolStripDropDown customToolTip = new ToolStripDropDown();
customToolTip.Items.Add(new CustomPopupControl("Hello", "world"));
MouseMove += (o, e) => {
Point location = e.Location;
location.Offset(0, 16);
customToolTip.Show(this, location);
};
}
class CustomPopupControl : ToolStripControlHost {
public CustomPopupControl(string title, string message)
: base(new Panel()) {
Label titleLabel = new Label();
titleLabel.BackColor = SystemColors.Control;
titleLabel.Text = title;
titleLabel.Dock = DockStyle.Top;
Label messageLabel = new Label();
messageLabel.BackColor = SystemColors.ControlLightLight;
messageLabel.Text = message;
messageLabel.Dock = DockStyle.Fill;
Control.MinimumSize = new Size(90, 64);
Control.Controls.Add(messageLabel);
Control.Controls.Add(titleLabel);
}
}
}
I mean if it a button or an image button you can add something like MouseHover action and then show your message
private void button1_MouseHover(object sender, System.EventArgs e)
{
MessageBox.Show("yourmessage");
}
you need to customize the tooltip. refer to
http://www.codeproject.com/Articles/98967/A-ToolTip-with-Title-Multiline-Contents-and-Image
There are some other articles there, but this one works fine for me.
You may need to add code for your requirement.

Winform menustrip and hiding tabs

Hello I'm thinking of creating a tabcontrol which the tabpages will be filtered by the clicks in the menustrip.
For ex.
My menustrip is in form 1
and my tabcontrol is in form 2
My tabcontrol consist of 7 tabs and I want only 1 tab will be shown at a time.
for example If I click the name in the menustrip it will open/show a new form and the tabcontrol will only show the names tab.
I wonder if its possible because making diff forms for each list seems to be very long.
thanks for reading this.
Problem is, the TabPage control has no Visible property (well, it has, but it does nothing). So you can't hide and show tabs at will. You'll have to remove the tabs that should not be visible.
You could make a form (named TabbedForm) with code like this:
private readonly int _index;
public TabbedForm(int index)
{
this._index = index;
InitializeComponent();
}
private void form_Load(object sender, EventArgs e)
{
for (int index = this.tabControl1.TabPages.Count - 1; index >= 0; index--)
{
if (index != this._index)
this.tabControl1.TabPages.Remove(this.tabControl1.TabPages[index]);
}
}
With each menu button (Clicked event) in your main form you can open a TabbedForm with a different index.
Yes, this will work pretty fine. But I think, you must use the default tab view control for this and that must not create the problem either in you case too.

Categories

Resources