How to insert hyperlinks in ListView - c#

I am trying to make Link Web / link info directory in C# where a user can save his links.
The following is my code:
private void Form1_Load(object sender, EventArgs e)
{
Int i=0;
listView1.View = View.Details;
listView1.GridLines = true;
listView1.Columns.Add("Links",250,HorizontalAlignment.Center );
listView1.Columns.Add("Name", 250, HorizontalAlignment.Center);
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Add(textbox1.text);
listview1.Items[i].subitems.add("textbox2.text")
}
textbox2 contains the hyperlink but when I insert it displays as text, not as a hyperlink.

Use ObjectListView -- an open source wrapper around a standard ListView. It supports links directly:
Also have a look at the DataGridView control which has support for LinkLabel.
Using this control, you get all the functionality of the details view in a ListView, but with more customisation per row.
Also you can set one property to true --> listView1.HotTracking = true; You code will look like this:
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Add(textbox1.Text);
listView1.HotTracking = true;
listView1.Items[i].SubItems.Add("hyperlynk2.text");
}

Related

Click Event not fired in User Control when multiple instances are created

I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}

c# reset a textbox.text property at runtime to use default

In my application i have a lot of support for different languages(WinForms).
Initially i have set the text in the button to say "Start" in a bunch of different languages.
On a click-event the text changes to "⚫".
And then i have another button that stops the event on click.
Is it possible to revert the "running" (⚫) text to the original text?
textbox.text.ResetText() just clears it.
private void btnStartTest_Click(object sender, EventArgs e)
{
btnStartTest.Text="⚫";
}
private void btnStopTest_Click(object sender, EventArgs e)
{
//reset the text to what it used to be.
}
Solution:
private string languageString;
private void btnStartTest_Click(object sender, EventArgs e)
{
languageString = btnStartTest.Text;
btnStartTest.Text="⚫";
}
private void btnStopTest_Click(object sender, EventArgs e)
{
btnStartTest.Text = languageString;
//reset the text to what it used to be.
}
If you use the internationalization mechanism of WinForms that uses resource files to store the property values of controls for different languages, you can use this source code to reset the button to its initial state using the current UI language:
ComponentResourceManager resources = new ComponentResourceManager(typeof(MyFormClass));
resources.ApplyResources(buttonStart, buttonStart.Name);

using C# solution for Listview label

I want to use this solution to convert URLs to link in a listview label.
private string ConvertUrlsToLinks(string text)
{
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return r.Replace(text, "$1").Replace("href=\"www", "href=\"http://www");
}
I have tried this in the listview databound but its not working.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label ProjectPostLabel = (Label)e.Item.FindControl("ProjectPostLabel");
ProjectPostLabel = ConvertUrlsToLinks({0});
}
Thank you
According to your code you are using ASP.NET Web Forms, not MVC.
You must use your ProjectPostLabel instance with Text property - no need to create a new label that is not assign to anywhere.
From your event you must retrieve Url property, not the label control. I have used NorthwindEmployee class with URL property in my ListView. You must cast it to your own class that is used in the list view.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL);
}
And you must remember that only the last item from your list view will be displayed in the label (unless you expect that behavior). If you want to list of URLs from the list you can write this:
protected void Page_Load(object sender, EventArgs e)
{
ProjectPostLabel.Text = string.Empty;
}
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
}

Delete Selected Text in Richtextbox

I have a richtextbox and i want to delete not cut the selected when the user presses a button.
I have used
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("DELETE");
}
This works but i want to know another way to do it.
I have tried
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText.Replace(richTextBox1.SelectedText, "");
}
This doesn't perform any action.
Pls what can i do?
Just do this:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = "";
}
Your code doesn't work because the string is immutable, you can't change the richTextBox1.SelectedText that way. All the methods (Replace, Insert, ...) performed on a string will create a new string. This new string will be used to initialize your string variable if you need.
The following line of code works for me:
SendKeys.Send("{DELETE}");
Click Link to visit the Official documentation on SendKeys methods.

TextBox shown in the wrong place after a scroll down

i have a from c# and i want to show text Box after a click in a check box.
but when i scroll down, and check , the text Box is shown in the wrong place !!!
the text Boxes must be in the same level with check Boxes.
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Visible = true;
}
and changing the Location of the text Box don't give good results !
thanks for help.
You are running into an awkward quirk of the Panel control, it only scrolls controls that are visible. When you make it visible in your code, it will have the wrong Location property if you've used the scrollbar. You will need to make the correction yourself. Make it look like this:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
if (!textBox1.Visible) {
textBox1.Location = new Point(textBox1.Left + panel1.AutoScrollPosition.X,
textBox1.Top + panel1.AutoScrollPosition.Y);
textBox1.Visible = true;
}
}
A better alternative is to use the Enabled property instead, also much less disorienting for the user. Set it to False in the designer and then:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
}

Categories