How to copy the first Character of a Label with a button? - c#

I have a label that I want to only copy the first character. The label reads 1000. I have my copy button set up like this
var textCopy = label.Text;
Clipboard.SetText(textCopy);
Can I add something to the label.Text part that only allows the first character to be copied?

string is an array of characters, so just get the first index of the string like this:
Clipboard.SetText(textCopy[0]);

Use Substring:
var textCopy = label.Text;
Clipboard.SetText(textCopy.Substring(0,1));
msdn

Related

Change Particular line of Multiline textbox in C#

I am unable to Change the specific string of a multiline TextBox.
suppose first line of multiline textbox is "Hello" & second line is "Bye".But when i trying to change the value of second line like below.
textBox1.Lines[1] = "Good bye";
When I saw the result using Debug mode it was not "Good bye".
I also read this MSDN article & this stackoverflow question but can't get the desired answer.
As MSDN states (the link you provided):
By default, the collection of lines is a read-only copy of the lines in the TextBox.
To get a writable collection of lines, use code
similar to the following: textBox1.Lines = new string[] { "abcd" };
So, you have to "take" Lines collection, change it, and then return to TextBox. That can be achieved like this:
var lines = TextBox1.Lines;
lines[1] = "GoodBye";
TextBox1.Lines = lines;
Alternatively, you can replace text, like Wolle suggested
First you need assign textBox1.Lines array in variable
string[] lines = textBox1.Lines;
Change Array Value
lines[1] = "Good bye";
Reassign array to text box
textBox1.Lines=lines;
According to MSDN
By default, the collection of lines is a read-only copy of the lines
in the TextBox. To get a writable collection of lines need to assign
new string array
Working with TextBox lines via Lines property are extremely ineffective. Working with lines via Text property is a little better, but ineffective too.
Here the snippet, that allows you to replace one line inside TextBox without rewriting entire content:
public static bool ReplaceLine(TextBox box, int lineNumber, string text)
{
int first = box.GetFirstCharIndexFromLine(lineNumber);
if (first < 0)
return false;
int last = box.GetFirstCharIndexFromLine(lineNumber + 1);
box.Select(first,
last < 0 ? int.MaxValue : last - first - Environment.NewLine.Length);
box.SelectedText = text;
return true;
}
You could try to replace the text of second line like this:
var lines = textBox.Text.Split(new[] { '\r', '\n' }).Where(x => x.Length > 0);
textBox.Text = textBox.Text.Replace(lines.ElementAt(1), "Good bye");

How to pick out specific parts of a text box?

I am trying to pick out specific letters/numbers from a text box, because each means something. After that I am trying to display in a label what it means.
So if I have a number AB-123456, I need to first pick out AB something like:
If (textBox.Text.Substring(0,2) == "AB") {
//Display to a label
}
First off, this doesn't work and I also tried substring(0,1) but also was receiving errors when I used my clear button to clear the text box.
After that I still need to pull the rest of the numbers. The next one I need to pull and define is 123, then 4 by itself, 5 by itself, and six by itself.
How do I go about pulling each of these individually if substring isnt working?
Try this:
if (textBox.Text.StartsWith("AB"))
{
//Display to a label
}
Use this if you don't want to have to check the Length of the text first. Also, you can include a StringComparison argument if you want to ignore case.
string input = textBox.Text;
// check the length before substring
If (input.Length >= 2 && input.Substring(0,2) == "AB") {
//Display to a label
}
or use regex:
string txt="AB-1234562323";
string re="AB-(\\d+)"; // Integer Number 1
Regex r = new Regex(re,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)// match found
{
// get the number
String number=m.Groups[1].ToString();
}

Clip string inside of Array

Lets say I have a String Array full of items such as:
string[] letters = new string[4] {"A1","B1","C1","D1"};
Later, I want to set the contents of a textbox to the first value in the array:
Letter.Content = letters[0];
Is there a way to 'clip' the number out of the String in the Array? For example, in my above code, currently the Letter textbox would be set to 'A1'. What I want however is to set it to just 'A'.
Depends on if the strings's length is always two and the digit is at the second position. Then it's simple:
Letter.Content = letters[0][0];
If you don't know the length but you want to take all letters from the left until there is a non-letter you could use string.Concat + LINQ:
Letter.Content = string.Concat(letters[0].TakeWhile(Char.IsLetter));
or you could do it the old fashion way using SubString Method
Letter.Content = letters[0].Substring(0,1);

How to affect formatting of dynamically created label text in c#

I think this should be a pretty easy question to answer but I can't seem to figure it out.
I am adding text to labels from a sqldatasource in c#. All of that works, but I want to be able to format the text. I want to 1) be able to change the format to 0.00 (instead of a string of decimals) and I would also like to be able to add words before the text. I assume I need to somehow use the string.format command but can't figure out how to work it in. Any help would be greatly appreciated. Here's my code below:
DataView dvSql = (DataView)DeskSummary.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drvSql in dvSql)
{
Desk.Text = drvSql["Deskname"].ToString();
MarginLabel.Text = drvSql["margin"].ToString();
CurrentCI.Text = drvSql["comp_index_primarycomp"].ToString();
WalMartCurrentCI.Text = drvSql["comp_index_walmart"].ToString();
ForecastMargin.Text = drvSql["margin_forecast"].ToString();
WalMartForecastCI.Text = drvSql["comp_index_walmart_forecast"].ToString();
ForecastCI.Text = drvSql["comp_index_primarycomp_forecast"].ToString();
}
You can pass the format argument to the ToString() method like so:
MarginLabel.Text = drvSql["margin"].ToString("0.00");
However, as you said you wanted to prepend some text. Therefore, I recommend:
MarginLabel.Text = String.Format("Prepended text {0:0.00}", drvSql["margin"]);
Note: I just picked one of your labels; I'm not sure which ones get special formatting treatment.
use the
string.Format("This is a before text {"0"},your param)
// you can add as many variables and {""} string literals as you need just make sure that you separate the variables with a ","
Here is the code
string stringNumber = "5123.34214513";
decimal decimalNumber = Decimal.Parse(stringNumber);
string output = String.Format("Your text: {0:0.00}", decimalNumber);
Console.WriteLine(output); //Your text: 5123.34
This works if the column is of type string
String.Format() will do what you need for prepending/appending text values,
string.Format("prepend text {"0"} append text", paramString)
But if you want to actually format the value you are getting back from SQL, then you would need to use String.Format() on that value as well as possibly some RegEx expressions and/or .ToUpperCase or .ToLowercase for your capitalization... something like.
var capitalizedString = paramString.subStr(0,1).ToUppercase + paramString.subStr(1, paramstring.Length);
string.Format("Prepended text {"0"} plus appended text", capitalizedString);

Why these lines cannot operate in an string

txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
i want to find the last index of "," in my text and then remove that , but it is not working. Any Idea? txtBeautified is a richtextbox.
Are you retrieving the result of the operation?
value = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
If you are changing the value of the text box, you need to assign the result back to the text box:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
Explanation: Strings cannot be changed. Functions that operate on strings do not change the strings, but return new strings. Therefore, the Remove function returns a string representing the result. To make use of this string, you will need to assign it to a variable/property or pass it into another function call.
Remove is a function. call should be:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1)
Keep in mind that a string is immutable, so the Remove function returns you a new string. You'd need to reassign that new string back to the text box, like:
txtBeautified.Text = txtBeautified.Text.Remove(txtBeautified.Text.LastIndexOf(","), 1);

Categories

Resources