Delete Selected Text in Richtextbox - c#

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.

Related

How to add and remove values from ComboBox to CheckedListBox

What I need:
I need a ComboBox and a CheckedListBox with exact same values.
I have a Button to add values and delete values.
Here is my Add Button:
private void button5_Click(object sender, EventArgs e)
{
checkedListBox1.Items.Add(comboBox1.Text);
comboBox1.Items.Add(comboBox1.Text);
comboBox1.Text = "";
}
and my Delete Button:
private void button6_Click(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
I would like to be able to delete the entries in the CheckedListBox without having to select it first, I only need it to be selected into the comboBox1.
Since you're adding the same strings, you can use the IndexOf() method to get the index where the current string is located in your CheckedListBox and the RemoveAt() to remove it.
Verify that the ComboBox.SelectedItem is not null. You can use the GetItemText() method to get the string currently selected. If the SelectedItem is null, you get back a empty string.
private void button6_Click(object sender, EventArgs e)
{
string currentItem = comboBox1.GetItemText(comboBox1.SelectedItem);
if (!string.IsNullOrEmpty(currentItem))
{
checkedListBox1.Items.RemoveAt(checkedListBox1.Items.IndexOf(currentItem));
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
}
Method2:
If the Items it the two controls are located at the same index, you can instead use the ComboBox.SelectedIndex to RemoveAt() both:
private void button6_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
checkedListBox1.Items.RemoveAt(comboBox1.SelectedIndex);
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
}
}

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);

Get the text value of the button that was clicked

I am trying to get the text value from a button that was clicked. In my head, it looks something like this:
private void button2_Click(object sender, EventArgs e)
{
string s = thisbutton.text
}
The object which fired the event is sender, so:
private void button2_Click(object sender, EventArgs e)
{
string s = (sender as Button).Text;
}
Just cast the sender Object to a Button Object and access the text attribute :
protected void btn_Click (object sender, EventArgs e){
Button btn = sender as Button;
string s= btn.Text
}
Should be like this:
private void button2_Click(object sender, EventArgs e)
{
string s = this.button2.Text;
}
In every build in event handler there are 2 parameters sender and e.Sender takes reference to that object which fires the event.The second parameter e holds some information about the event(such as the location of pointer and other of this kind)
You need only bring it to Button type and get what information you want
try and apply this example in your button event
private void button_click(object sender, EventArgs e)
{
var getValue = ((Button)sender).Text; //this will get the value of the text using sender
}
This was asked some time ago and the platform in my case may be a little different to what the OP used but I arrived at the same question for GTK.
I am developing in Xaramin / Visual Studio in OSX using GTK2+, and for me the original accepted answer is close, but yields an error that .Text doesn't exist. In my case, it needs to be Label. This works for me:
protected void Button_Clicked(object sender, EventArgs e)
{
Button btn = sender as Button;
lblWhichButton.Text = btn.Label;
if (btn.Label == "<<<" )
i--;
else
i++;
lblCounter.Text = "" + i;
}

WPF change Password property of PasswordBox in PasswordChange event

I'm trying to do the following:
private void PasswordBox_A_PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox_A.Password = PasswordBox_A.Password.Replace("\n", "");
}
but the thing is This code is executed infinitely. I just want to make sure that the user doesn't write the string "\n", do yow know other method or maybe how make this code work
Thanks.
A user should not be able to enter \n in the password box. But anyway, to avoid infinite calling, you could try something like this:
private void PasswordBox_A_PasswordChanged(object sender, RoutedEventArgs e)
{
if (PasswordBox_A.Password.Contains("\n"))
PasswordBox_A.Password = PasswordBox_A.Password.Replace("\n", "");
}

How to insert hyperlinks in ListView

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");
}

Categories