My Question is to how to extend a TextBox such that it may start behaving like RichTextBox?
There can be various properties that RichTextBox may add: appearance mainly.
Should I use this kind of method where I extend the TextBox class and create a basic TextBox which would contain several other textboxes which would behave like a big container node containing small specialized nodes?
For starters, to have texts with alternate color after '+', I ve used this way:
class CustomTextBox : TextBox
{
List<TextBox> _textboxes = new List<TextBox>();
string _Text="";
List<Color> colorlist = new List<Color>();
public override string Text
{
get{return this._Text;}
set{this._Text = value;}
}
public CustomTextBox()
{
foreach(Color color in (Color[]) Enum.GetValues(typeOf(Color)))
{
colorlist.add(color);
}
this.KeyUp+= new KeyUpEventHandler(TextChangedCheck);
}
int i=0;
private void TextChangedCheck(object sender, KeyUpEventArgs e)
{
if(e.KeyData == Keys.Add)
{
TextBox Temp = new TextBox();
Temp.Text = this.Text;
this.Text = "";
Temp.ForeColor = colorlist[i];
i++;
this._textboxes.Add(Temp);
this.Controls.Add(_textboxes[i]);
e.Handled = true;
}
}
}
EDIT:
The MAIN purpose of this question is to extend a TextBox using its own properties to have a RTB like behavior and not using Graphics or related.
I'd have to guess what you really want to achieve here.
But if you want a Control with formatted text (FT) and really really really don't want a RichTextBox, I think you shouldn't create a new, embedded TextBox for every piece of FT.
Instead you should write the FT yourself, maybe on a panel with DrawString and use only one Textbox for text entry. Interesting project, really, once one accepts the challenge. Of course you must think your format through and also consider all sorts of interface questions..
Last week I have avoided a RTF for a tiny help system by using a ListView; it formats by line only, using the first character to indicate the format from a list of a dozen or so.. Works fine, but only one format per line.
Related
How do I make bold a variable amount of items in a listbox? I've seen solutions like this one, but it appears to only work if I know exactly which items should be bold before runtime. Here's my specific case:
I have a listbox with a list of strings read from a file. I have a search bar that, when typed in, will automatically move items matching that string to the top of the listbox. Unfortunately, being at the top isn't enough of an indicator of "search result," so I also want to make those items bold. Before runtime, I do know all the items I want to be bold will be at the top of the list, but I have no idea how many items that will be. Additionally, when the user erases the search bar's contents, the list will be reordered to its initial order and the bold items should be made not bold.
How do I go back and forth between bold/not bold for specific listbox items at runtime?
Here is my code for the search and display functionality:
private void txtSearch_TextChanged(object sender, EventArgs e)
{
string searchTerm = txtSearch.Text.Trim();
if(searchTerm.Trim() == "") // If the search box is blank, just repopulate the list box with everything
{
listBoxAllTags.DataSource = fullTagList;
return;
}
searchedTagList = new List<UmfTag>();
foreach(UmfTag tag in fullTagList)
{
if(tag.ToString().ToLower().Contains(searchTerm.ToLower()))
{
searchedTagList.Add(tag);
}
}
// Reorder the list box to put the searched tags on top. To do this, we'll create two lists:
// one with the searched for tags and one without. Then we'll add the two lists together.
List<UmfTag> tempList = new List<UmfTag>(searchedTagList);
tempList.AddRange(fullTagList.Except(searchedTagList));
listBoxAllTags.DataSource = new List<UmfTag>(tempList);
}
I was able to solve my own problem. I indeed used the solution present in this question, but I altered it like so:
private void listBoxAllTags_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
FontStyle fontStyle = FontStyle.Regular;
if(e.Index < searchedTagList.Count)
{
fontStyle = FontStyle.Bold;
}
if(listBoxAllTags.Items.Count > 0) // Without this, I receive errors
{
e.Graphics.DrawString(listBoxAllTags.Items[e.Index].ToString(), new Font("Arial", 8, fontStyle), Brushes.Black, e.Bounds);
}
e.DrawFocusRectangle();
}
The second if statement (checking if the count is greater than 0) is required. Without it, I received "index[-1]" errors because my program first starts with empty listboxes, and the DrawString method couldn't draw the string for the empty listBox.Items[] array.
I have a series of 5 text boxes on a winforms form which are used to collect data as strings. I intend to use these boxes in multiple ways from a method to clear the contents of each, to a method which takes the data from each to check before exporting to a text file.
In order to do this I planned to create a List<Textbox> which would essentially be a list of all 5 boxes so I could later use code such as
foreach(Texbox box in *texboxList*)
{
box.Clear()
}
etc.
My only idea so far is to create a seperate method which adds all of the boxes to a list and then somehow pass the result of the method as a parameter to the relevant methods such as those that clear the boxes. The code I currently have is displayed below.
public List<TextBox> Clear_entered_data()
{
List<TextBox> textBoxes = new List<TextBox>();
textBoxes.Add(tbox1);
textBoxes.Add(tbox2);
textBoxes.Add(tbox3);
textBoxes.Add(tbox4);
textBoxes.Add(tbox5);
return textBoxes;
}
This is the code I'm using to generate the list of textboxes to use. I think the problem I'm having is understanding how this can be passed to other methods through parameters. The method I'd like to make use of the list is shown below here as I have it so far.
private void Clear_button_Click(object sender, EventArgs e, List<TextBox> textBoxes)
{
DialogResult Clear_validation = MessageBox.Show("Are you sure you would like to clear all data from the form?","Clear data?", MessageBoxButtons.YesNo);
if(Clear_validation == DialogResult.Yes)
{
foreach (TextBox box in textBoxes)
{
box.Clear();
}
}
}
With the code above I get the error upon running :
'Error 1 No overload for 'Clear_button_Click' matches delegate 'System.EventHandler''
But I've had no luck, please try and explain whats going wrong and help me find more appropriate solution!
Thanks
create a private class member that of list of textboxes List<TextBox> _textBoxes;
then after you call InitializeComponents(because before that your textboxex doesn't exists if you used the IDE to create them) place your code to add the textboxes into the list
_textBoxes = new List<TextBox>();
_textBoxes.Add(tbox1);
_textBoxes.Add(tbox2);
_textBoxes.Add(tbox3);
_textBoxes.Add(tbox4);
_textBoxes.Add(tbox5);
now you can use in your methods the _textBoxes that contains all your created TextBox
your public List Clear_entered_data() became
public List<TextBox> ClearTextBoxes() {
foreach(var textBox in _textBoxes){
textBox.Clear();
}
}
If you used a naming convention, you could just iterate over the form's control collection like this - in my example, the names all start with "SpecialTextBox":
foreach(Control c in someFormReference.Controls)
{
if (c.GetType() == typeof(TextBox) && c.Name.StartsWith("SpecialTextBox"))
{
// do your thing here
}
}
I am attempting to implement a Windows Forms control in C# that resembles a textbox. When the user types 3 or more characters, a search will be performed against a datasource. There will be multiple fields returned (see the class structure below as one possible definition).
public class MyStructure
{
public int Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 {get; set; }
}
My requirements are to display an autocomplete list containing multiple columns (Note: this can be a string that contains padded fields from the list that are concatenated together). When the user either types all characters, hits the down arrow to select an item, or hits the enter key the value in the textbox will take the ValueMember of the list (where the DisplayMember of the list would be the whole data source). Every keystroke that the user enters that is not an up or down arrow or the enter key will perform another search and refresh the list.
I have seen how to implement a textbox with a single column in an auto-suggest, but cannot find a relatively "simple" example of how to do this for multiple columns. Should the control be a textbox or a combobox that is somehow styled to resemble a textbox (if this is possible) or a user control?
Should the event to monitor keystrokes be the TextEntered or the KeyPress event? Can I reset the AutoCompleteStringCollection without having the contents entered affected (I keep losing my input or my place in the input in any attempts)?
Can anyone provide examples of how to do this in framework 4.0 or above or point me to an example?
EDIT 1:
After much searching, I have found that essentially I need to implement a ContextMenuStrip on the TextBox (anything else and other controls below the user control will be overlapped). My problem is that I cannot determine how to handle the Key press events such as Tab and Enter. In addition, I need to handle if the user continues typing (in this event, I want to switch focus back to the textbox and add the key). Below is my code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
ContextMenuStrip menuStrip;
string szMenuItem = string.Empty;
// This would actually be a call to a web service
List<MStarDeal> deals = DealInfo.Where(i => i.Value1.StartsWith(textBox1.Text.ToUpper()) || i.Value2.StartsWith(textBox1.Text.ToUpper()) || i.Value3.StartsWith(textBox1.Text.ToUpper()))
.Select(i => i).ToList();
if (textBox1.Text.Length >= 3 && !bSelected)
{
menuStrip = new System.Windows.Forms.ContextMenuStrip();
foreach (MStarDeal item in deals)
{
szMenuItem = item.Value1.PadRight(15) + item.Value2.PadRight(20) + item.Value3.PadRight(80);
ToolStripItem tsItem = new ToolStripMenuItem();
tsItem.Text = szMenuItem;
tsItem.Name = item.Value1;
tsItem.MouseUp += tsItem_MouseUp;
menuStrip.Items.Add(tsItem);
}
textBox1.ContextMenuStrip = menuStrip;
textBox1.ContextMenuStrip.Show(textBox1, new Point(0, 20));
}
else if (bSelected)
{
bSelected = false;
}
}
void tsItem_MouseUp(object sender, EventArgs e)
{
bSelected = true;
textBox1.Text = ((ToolStripMenuItem)sender).Name;
}
Thanks,
Lee
I think I understand your question. How about using the TextChanged() event instead of KeyPress? As far as the columns, a flowLayoutPanel will render columns if you set it up to flow in the right direction and make its size appropriate to the width of the two columns combined.
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.
I have created a WindowsFormControlLibrary porject. It works fine, I can drop it on the forms,call its methods,etc ...
but now as a property of it, I am passing the name of a Label to it. and I want this custom control to be able to use that label name and for example change its font to bold .
so the question is that if I have a WinForm and I have a Label on that form and my custom control on that form, then how can I tell my custom control to do something with that label which I am passing its name to it?
Instead of sending in the name of the label, send in a reference to the actual label and then the custom control can both read the name if it needs to and change the label's font and other properties.
Be careful though, it can quickly get messy to keep track of what's happening if various forms and controls change controls on other forms etc.
Edit: Added code to do what you ask for in the comments
Code isn't tested so it might not be completely correct, but something similar to this should work.
foreach (Control c in Parent.Controls)
{
if (c is Label)
{
Label l = (Label)c;
// do stuff to label l
}
}
First, if you wish to access a Control from your UserControl, you will need to use the FindForm() method.
Second, you will be required to expose your TextBox control, for example, through a property of your form.
Then, you would need to know the type of this Form returned by this FindForm() method.
Once you know it, you need to type-cast this result to the correct type.
So, here a sample untested pseudo-code to give you the idea:
public partial class MyMainForm {
private TextBox textBox1;
public MyMainForm() {
textBox1 = new Textbox();
textBox1.Name = #"textBox1";
textBox1.Location = new Point(10, 10);
textBox1.Size = new Size(150, 23);
this.Controls.Add(textBox1);
}
public Font MyTextBoxFont {
get {
return textBox1.Font;
} set {
if (value == null) return;
textbox1.Font = value;
}
}
}
Then, assuming you have dropped your control on your form, your UserControl could have a property like so:
public partial class MyUserControl {
private Form GetContainerForm {
get {
return this.FindForm();
}
}
// And later on, where you need to set your TextBox's font:
private void SetContainerInputFieldFont(Font f) {
if (GetContainerForm == null) return; // Or throw, depending on what you need to do.
((MyMainForm)GetContainerForm).MyTextBoxFont = f
}
}
cool :) I just added a get set public property of type Label... it automatically lists all the label on the form.