I'm displaying a set of search results in a ListView. The first column holds the search term, and the second shows the number of matches.
There are tens of thousands of rows, so the ListView is in virtual mode.
I'd like to change this so that the second column shows the matches as hyperlinks, in the same way as a LinkLabel shows links; when the user clicks on the link, I'd like to receive an event that will let me open up the match elsewhere in our application.
Is this possible, and if so, how?
EDIT: I don't think I've been sufficiently clear - I want multiple hyperlinks in a single column, just as it is possible to have multiple hyperlinks in a single LinkLabel.
You can easily fake it. Ensure that the list view items you add have UseItemStyleForSubItems = false so that you can set the sub-item's ForeColor to blue. Implement the MouseMove event so you can underline the "link" and change the cursor. For example:
ListViewItem.ListViewSubItem mSelected;
private void listView1_MouseMove(object sender, MouseEventArgs e) {
var info = listView1.HitTest(e.Location);
if (info.SubItem == mSelected) return;
if (mSelected != null) mSelected.Font = listView1.Font;
mSelected = null;
listView1.Cursor = Cursors.Default;
if (info.SubItem != null && info.Item.SubItems[1] == info.SubItem) {
info.SubItem.Font = new Font(info.SubItem.Font, FontStyle.Underline);
listView1.Cursor = Cursors.Hand;
mSelected = info.SubItem;
}
}
Note that this snippet checks if the 2nd column is hovered, tweak as needed.
Use ObjectListView -- an open source wrapper around a standard ListView. It supports links directly:
This recipe documents the (very simple) process and how you can customise it.
The other answers here are great, but if you don't want to have to hack some code together, look at the DataGridView control which has support for LinkLabel equivalent columns.
Using this control, you get all the functionality of the details view in a ListView, but with more customisation per row.
You can by inheriting the ListView control override the method OnDrawSubItem.
Here is a VERY simple example of how you might do:
public class MyListView : ListView
{
private Brush m_brush;
private Pen m_pen;
public MyListView()
{
this.OwnerDraw = true;
m_brush = new SolidBrush(Color.Blue);
m_pen = new Pen(m_brush)
}
protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex != 1) {
e.DrawDefault = true;
return;
}
// Draw the item's background.
e.DrawBackground();
var textSize = e.Graphics.MeasureString(e.SubItem.Text, e.SubItem.Font);
var textY = e.Bounds.Y + ((e.Bounds.Height - textSize.Height) / 2);
int textX = e.SubItem.Bounds.Location.X;
var lineY = textY + textSize.Height;
// Do the drawing of the underlined text.
e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, m_brush, textX, textY);
e.Graphics.DrawLine(m_pen, textX, lineY, textX + textSize.Width, lineY);
}
}
You can set HotTracking to true so that when the user hovers mouse over the item it appears as link.
Related
Right, so I have 13 textboxes with corresponding labels that are assigned after a user decides the name from a different form (instantiated by the 'Add field...' button). The issue arises when the user wishes to delete a textbox with previously entered data, as this results in an empty space where the textbox and label originally were as visualized by the following image:
My question is: how do I make it so that when a user chooses to delete a textbox, the textbox-label pair(s) that follow it replace the deleted textbox AND shift the remaining textboxes accordingly.
Textbox-label pairs in designer:
I've thought about this problem intensively over the past few days, and have concluded that with my current knowledge of C# I am limited to solving this issue with a horrendously tedious amount of if-statements (talking hundreds - thousands here). Any and all help would be appreciated!
Current code on the X-button for first textbox-label pair:
private void xButton1_Click(object sender, EventArgs e)
{
label14.Text = "";
textBox1.Text = "";
if (label14.Text.Equals(""))
{
label14.Visible = false;
textBox1.Visible = false;
xButton.Visible = false;
label14.Text = "";
textBox1.Text = "";
}
if (!textBox2.Text.Equals(""))
{
label14.Text = label15.Text;
textBox1.Text = textBox2.Text;
}
if (!textBox2.Text.Equals("") && (textBox3.Text.Equals("")))
{
label15.Visible = false;
textBox2.Text = "";
textBox2.Visible = false;
xButton2.Visible = false;
}
}
One simple thing you could do is give all your "dynamic" controls (label, textbox, button) a similar value in their Tag property (in my example, I used the string "dynamic" for all the control Tags. This enables you to query for them easily.
Next, you could follow the logic that, anytime you delete some controls, you move all controls below the deleted ones up a distance equal to the height of the control being deleted plus whatever padding you have between the controls.
For example, when a user clicks the X button, since you know the value of the Bottom of the control that's being deleted, you could find all controls that had a matching Tag property whose Top is greater than the x button Bottom, and you can move them up.
Here's an example (this assumes that all your X buttons are mapped to this same click event):
private void buttonX_Click(object sender, EventArgs e)
{
// This is represents the distance between the bottom
// of one control to the top of the next control
// Normally it would be defined globally, and used when you
// lay out your controls.
const int controlPadding = 6;
var xButton = sender as Button;
if (xButton == null) return;
var minTopValue = xButton.Bottom;
var distanceToMoveUp = xButton.Height + controlPadding;
// Find all controls that have the Tag and are at the same height as the button
var controlsToDelete = Controls.Cast<Control>().Where(control =>
control.Tag != null &&
control.Tag.ToString() == "dynamic" &&
control.Top == xButton.Top)
.ToList();
// Delete the controls
controlsToDelete.ForEach(Controls.Remove);
// Get all controls with the same tag that are below the deleted controls
var controlsToMove = Controls.Cast<Control>().Where(control =>
control.Tag != null &&
control.Tag.ToString() == "dynamic" &&
control.Top > minTopValue);
// Move each control up the specified amount
foreach (var controlToMove in controlsToMove)
{
controlToMove.Top -= distanceToMoveUp;
}
}
I got it to work flawlessly for the Y coordinate, here's what I've tried:
public Point GetCellPos(MouseEventArgs e){
ListViewHitTestInfo lit = listview1.HitTest(e.Location);
Point p = new Point
{
X = listview1.Left + lit.Item.Position.X,
Y = listview1.Top + lit.Item.Position.Y
};
return p;
}
And for some reason, it won't get me the correct X coordinate. As a side note, I didn't put the listview control in a container other than the parent/form, so what could I be doing wrong? Any help would be appreciated, please.
OKAY, I solved it!! The key turned out to be in the Rectangle prop called Bound! I kept on tinkering with the X, Y coords ignoring the Rectangle which is actually what I was looking for! But anyway, I'm posting my humble snippet for those who might encounter the same problem in the future:
private void SetControlOnCell(ListView lv, Control eControl, MouseEventArgs e) {
ListViewHitTestInfo lit = lv.HitTest(e.Location);
Point p = new Point
{
X = lv.Left + lit.SubItem.Bounds.Left + 1,
Y = lv.Top + lit.SubItem.Bounds.Top
};
int w = (lit.SubItem.Bounds.Left == 0) ? lv.Columns[0].Width : lit.SubItem.Bounds.Width;
int h = lit.SubItem.Bounds.Height;
eControl.Location = p;
eControl.Size = new Size(w, h);
if (!eControl.Visible) eControl.Visible = true;
if (eControl.Font != lit.SubItem.Font) eControl.Font = lit.SubItem.Font;
eControl.Text = lit.SubItem.Text;
eControl.Focus();
}
And you use it like:
private void listview1_MouseClick(object sender, MouseEventArgs e) {
SetControlOnCell(listview1, txtEditValue, e)
}
I'm quite happy with this already as it's served the purpose of showing a "pseudo" control on top of a clicked cell (like I wanted it to). However, I'm keeping this question thread open for some better solutions others here may have and be willing to share.
--
EDIT: Ok, since no one's turned up -- thread's resolved I guess.
Okay, so the way I do this in one of my apps is I have a ContextMenuStrip that I've made for when the user right-clicks on an item in my ListView. From this, you can customize the menu itself to have controls in it if needed, or simply have it open up a dialog to edit the item properties.
Here's my click handler:
private void listView1_MouseClick(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var item = listView1.GetItemAt(e.Location.X, e.Location.Y);
if (item != null) {
menuItemSelected.Show(Cursor.Position);
menuItemSelected.Tag = item.Tag;
}
}
}
I store item-specific information in the Tag property, as this makes it very easy to use across my program.
You can replace menuItemSelected.Show() with something to display the control you want at the right location.
Note that this displays it exactly where the user right-clicks on the item, not the top-left corner of the item. If you want to do that replace Cursor.Position with item.Position.
What is the easiest way to recreate the effect where a text box displays a certain string (in italics and different font) until the user has clicked into the control and/or written his own text into the field? For an example, look at the "search" box at the top right of SO.
I have tried consuming the Paint event:
private void textEdit1_Paint(object sender, PaintEventArgs e)
{
if (textEdit1.Text.Length == 0 && !textEdit1.Focused)
{
textEdit1.Font = new Font(textEdit1.Font, FontStyle.Italic);
textEdit1.Text = "123";
}
else
{
textEdit1.Font = new Font(textEdit1.Font, FontStyle.Regular);
textEdit1.Text = string.Empty;
}
}
However, that's not working. By default, it shows no text, and if I click into it, I seem to get an infinite loop of setting the text to "123" and string.empty, until I give another control focus.
So, is that approach even the best, and if yes, what's the correct 2nd condition instead of .Focused?
Try the TextEdit.Properties.NullValuePrompt property. This property provides the text displayed grayed out when the editor doesn't have focus, and its edit value is not set to a valid value.
First of all, you shouldn't use the paint event, you should use the FocusChanged event if you want to do it by modifying the text property. However, the simplest method is not to modify the text property, but draw a string on top, like this:
private void textEdit1_Paint(object sender, PaintEventArgs e)
{
if (textEdit1.Text.Length == 0 && !textEdit1.Focused)
{
Font some_font = new Font(...parameters go here...);
Brush some_brush = Brushes.Gray; // Or whatever color you want
PointF some_location = new PointF(5,5); // Where to write the string
e.Graphics.WriteString("some text", some_font, some_brush, some_location);
}
}
So, if there is no text, and text box is not focused, draw this string. There are many overloads of the WriteString function, so you can pick which one you want.
You can use the Enter event. Set Text property to "search" for example. Use your font like others reported. Then catch the Enter event and set the Text property to string.empty.
textedit1.Text = "search";
private void textEdit1_Enter(object sender, EnterEventArgs e)
{
textedit1.text = string.empty;
}
But i think the best practice is the NullValuePrompt.
I know that richtextboxes can detect links (like http://www.yahoo.com) but is there a way for me to add links to it that looks like text but its a link? Like where you can choose the label of the link? For example instead of it appearing as http://www.yahoo.com it appears as Click here to go to yahoo
edit: forgot, im using windows forms
edit: is there something thats better to use (as in easier to format)?
Of course it is possible by invoking some WIN32 functionality into your control, but if you are looking for some standard ways, check this post out:
Create hyperlink in TextBox control
There are some discussions about different ways of integration.
greetings
Update 1:
The best thing is to follow this method:
http://msdn.microsoft.com/en-us/library/f591a55w.aspx
because the RichText box controls provides some functionality to "DetectUrls". Then you can handle the clicked links very easy:
this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked);
and you can simple create your own RichTextBox contorl by extending the base class - there you can override the methods you need, for example the DetectUrls.
Here you can find an example of adding a link in rich Textbox by linkLabel:
LinkLabel link = new LinkLabel();
link.Text = "something";
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.richTextBox1.GetPositionFromCharIndex(this.richTextBox1.TextLength);
this.richTextBox1.Controls.Add(link);
this.richTextBox1.AppendText(link.Text + " ");
this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
And here is the handler:
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
I found a way which may not be the most elegant, but it's just a few lines of code and does the job. Namely, the idea is to simulate hyperlink appearance by means of font changes, and simulate hyperlink behavior by detecting what the mouse pointer is on.
The code:
public partial class Form1 : Form
{
private Cursor defaultRichTextBoxCursor = Cursors.Default;
private const string HOT_TEXT = "click here";
private bool mouseOnHotText = false;
// ... Lines skipped (constructor, etc.)
private void Form1_Load(object sender, EventArgs e)
{
// save the right cursor for later
this.defaultRichTextBoxCursor = richTextBox1.Cursor;
// Output some sample text, some of which contains
// the trigger string (HOT_TEXT)
richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Underline);
richTextBox1.SelectionColor = Color.Blue;
// output "click here" with blue underlined font
richTextBox1.SelectedText = HOT_TEXT + "\n";
richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Regular);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectedText = "Some regular text";
}
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
int mousePointerCharIndex = richTextBox1.GetCharIndexFromPosition(e.Location);
int mousePointerLine = richTextBox1.GetLineFromCharIndex(mousePointerCharIndex);
int firstCharIndexInMousePointerLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine);
int firstCharIndexInNextLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine + 1);
if (firstCharIndexInNextLine < 0)
{
firstCharIndexInNextLine = richTextBox1.Text.Length;
}
// See where the hyperlink starts, as long as it's on the same line
// over which the mouse is
int hotTextStartIndex = richTextBox1.Find(
HOT_TEXT, firstCharIndexInMousePointerLine, firstCharIndexInNextLine, RichTextBoxFinds.NoHighlight);
if (hotTextStartIndex >= 0 &&
mousePointerCharIndex >= hotTextStartIndex && mousePointerCharIndex < hotTextStartIndex + HOT_TEXT.Length)
{
// Simulate hyperlink behavior
richTextBox1.Cursor = Cursors.Hand;
mouseOnHotText = true;
}
else
{
richTextBox1.Cursor = defaultRichTextBoxCursor;
mouseOnHotText = false;
}
toolStripStatusLabel1.Text = mousePointerCharIndex.ToString();
}
private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && mouseOnHotText)
{
// Insert your own URL here, to navigate to when "hot text" is clicked
Process.Start("http://www.google.com");
}
}
}
To improve on the code, one could create an elegant way to map multiple "hot text" strings to their own linked URLs (a Dictionary<K, V> maybe). An additional improvement would be to subclass RichTextBox to encapsulate the functionality that's in the code above.
Many moons later there is a solution for .NET (Core), as of at least .NET 6.0 (possibly earlier) - for .NET Framework (whose latest and last version is 4.8) you'll still need one of the other solutions here:
The .Rtf property now recognizes RTF-format hyperlinks; e.g., the following renders as:
This is a true RTF hyperlink: Example Link
this.richTextBox1.Rtf = #"{\rtf1 This is a true RTF hyperlink:\line {\field{\*\fldinst HYPERLINK ""https://example.org""}{\fldrslt Example Link}}} }";
The standard RichTextBox control (assuming you are using Windows Forms) exposes a rather limited set of features, so unfortunately you will need to do some Win32 interop to achieve that (along the lines of SendMessage(), CFM_LINK, EM_SETCHARFORMAT etc.).
You can find more information on how to do that in this answer here on SO.
Are there any controls that can dynamically create a group of radio buttons from a list of objects? Something similar to the CheckedBoxList control, but with mutually exclusive selection. This question points out this control doesn't exist natively for WinForms but are there any third party controls that do this?
Control vendors can't make any money with controls like that. Here's some code to get your started:
using System;
using System.Drawing;
using System.Windows.Forms;
class RadioList : ListBox {
public event EventHandler SelectedOptionChanged;
public RadioList() {
this.DrawMode = DrawMode.OwnerDrawFixed;
this.ItemHeight += 2;
}
public int SelectedOption {
// Current item with the selected radio button
get { return mSelectedOption; }
set {
if (value != mSelectedOption) {
Invalidate(GetItemRectangle(mSelectedOption));
mSelectedOption = value;
OnSelectedOptionChanged(EventArgs.Empty);
Invalidate(GetItemRectangle(value));
}
}
}
protected virtual void OnSelectedOptionChanged(EventArgs e) {
// Raise SelectOptionChanged event
EventHandler handler = this.SelectedOptionChanged;
if (handler != null) handler(this, e);
}
protected override void OnDrawItem(DrawItemEventArgs e) {
// Draw item with radio button
using (var br = new SolidBrush(this.BackColor))
e.Graphics.FillRectangle(br, e.Bounds);
if (e.Index < this.Items.Count) {
Rectangle rc = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Height, e.Bounds.Height);
ControlPaint.DrawRadioButton(e.Graphics, rc,
e.Index == SelectedOption ? ButtonState.Checked : ButtonState.Normal);
rc = new Rectangle(rc.Right, e.Bounds.Top, e.Bounds.Width - rc.Right, e.Bounds.Height);
TextRenderer.DrawText(e.Graphics, this.Items[e.Index].ToString(), this.Font, rc, this.ForeColor, TextFormatFlags.Left);
}
if ((e.State & DrawItemState.Focus) != DrawItemState.None) e.DrawFocusRectangle();
}
protected override void OnMouseUp(MouseEventArgs e) {
// Detect clicks on the radio button
int index = this.IndexFromPoint(e.Location);
if (index >= 0 && e.X < this.ItemHeight) SelectedOption = index;
base.OnMouseUp(e);
}
protected override void OnKeyDown(KeyEventArgs e) {
// Turn on option with space bar
if (e.KeyData == Keys.Space && this.SelectedIndex >= 0) SelectedOption = this.SelectedIndex;
base.OnKeyDown(e);
}
private int mSelectedOption;
}
Maybe; But this is easier and better to write yourself, though (unless somebody suggests either a free control or better yet sourcecode you can drop into your project).
A little GUI wisdom (I did not make this up but am too lazy to include references):
If a list of radio buttons will ever have > 7-10 items, use a list box.
Of course I gather either you don't have control of that or if you do, won't settle for that answer.
Add a scrollable panel to your form
in code, loop thru your list of objects. Inside the loop:
Make a new radiobutton
set the .top property to the .bottom of the previous one (or 0 if no previous)
put a copy of your object in the .Tag property (so you can tell which object was selected)
set the width so you don't get a horizontal scrollbar in your scrollable control
set the .text appropriately. You may need to truncate to avoid wrapping. If you want to go multiline for lines that wrap, you have to increase the height then, but this would require a lot of gymnastics with control.creategraphics, graphics.MeasureString, and other GDI+ features. See the Bob Powell's GDI+ FAQ.
add a handler so the checkchanged can be processed (AddHandler MyRB, addressof CC_Sub)
add it to the scrollable control
Add the CC_Sub mentioned above - can get right function signature by adding a radiobutton, putting on the handler for CheckChanged, and deleting radio button
In this sub, set a form-level variable of type of your class to the tag of the sender (you'll have to do ctypeing)
When your user clicks OK, return that variable, that is the object picked.
OK so it looks hard. So is either squeezing this out of management or doling out cash.
If you want fancier stuff, you can make a usercontrol with labels, checkboxes/radio buttons, etc. in it. You have to handle selecting/unselecting. Then add the usercontrol to a scrollable panel instead of the radiobutton. This provides almost unlimited flexibility.