How to add space in textbox until max length reached? - winforms

i want to add a space ( ) in text box until the maximum length reached (1000)
and i want to write text in the textbox concat the space so the text is first and after the text finished the space start until max length of textbox reached for example.
like below :
textbox1.text = "mytext" + " ";
but i want the space just to fill the textbox until max length (1000) reached.
and the other thing i want is if the text in the textbox is bigger than the max length
then remove the excess (the text after 1000)
please help

You can use the string.PadRight() method.
textbox1.Text = textbox1.Text.PadRight(textbox1.MaxLength, ' ');

First check if the length is greater than the maximum permitted, if it is then use Substring to cut it down to size. If the length is less than max, then you can use PadRight to pad the text...
string text = textbox1.Text;//get the text to manipulate
int max = 1000;
if(text.Length > max)//If the current text length is greater than max
text = text.Substring(0, max);//trim the text to the maximum allowed
else
text = text.PadRight(max, ' ');//pad extra spaces up until the correct length
//text will now be the same length as max (with spaces if required)
textbox1.Text = text;//set the new text value back to the TextBox
NOTE: Because you have asked about trimming the text to the maximum length, then I have assumed you have not used the MaxLength property of the TextBox - as this would already prevent adding more than the limit, I would recommend using that instead then you don't have to worry about trimming yourself, you could then just do:
textbox1.Text = textbox1.Text.PadRight(textbox1.MaxLength, ' ');

I think that's all you wanted right?
public String writeMax(String myText, int maxLenght)
{
int count = myText.Length;
String temp = "";
if(count >= maxLength)
{
temp = myText.substring(0, maxLength);
}
else
{
for(int i = 0; i < maxLength - count; i++)
{
temp += " ";
}
temp = myText + temp;
}
return temp;
}

Related

unable to get list of webelements text box value using getText(); and getAttribute();

I have made List of stored webelements, in that list some of element will get text box value through get getText(); and some will get through getAttribute();.here I made a for loop mentioning getAttribute(); to that list of webelement and get textbox values of each webelement, but here I am getting getAttribute(); text box values rest of elements which has getText(); I am unable to get those textbox values.
Is there any IF condition which has to satisfy both methods i.e gettext(); and getAttribute(); and get textbox values ,if I use only method I am getting for that method text box values rest of elements showing white spaces and printing HERE IS THE CONSOLE O/P null.
List<WebElement> e=new ArrayList<WebElement>();
e.add(sign.empchequedatetext);
e.addAll(sign.wagestlistvalues);
e.addAll(sign.taxesvalue);
e.addAll(sign.additionalincomelistvalues);
e.addAll(sign.otherdeductionslistvalues);
e.add(sign.netpayvalue);
int calculatevaluessize= e.size();
System.out.println("the total value size: "+calculatevaluessize);
System.out.println("taxes size is: " + taxesvalues+"||"+wages+"||"+add+"||"+deduc);
List<WebElement> emplist = s.getOptions();
int empsize = emplist.size();
System.out.println("emp size is: " + empsize);
try {
for (int i = 0; i < empsize; i++) {
WebElement emp = emplist.get(i);
emp.click();
String empname = emp.getText();
excel.setCellData(path, "Sheet2", i + 1, 0, empname);
Thread.sleep(1000);
for (int j = 0; j < calculatevaluessize; j++) {
WebElement taxesvalue = e.get(j);
Thread.sleep(1000);
String values = taxesvalue.getAttribute("value");
System.out.println("the calculate pay values are: "+values);
excel.setCellData(path, "Sheet2", i + 1, j + 1, values);
}
plz mention any if condition in my second for loop,i have tried but what type of condition i have to place
You can create a simple method to return one thing or the other:
private String getRowText(WebElement taxesvalue){
if (taxesvalues.getText() == null){
return taxesvalue.getAttribute("value");
}
return taxesvalues.getText();
}
This method should replace your taxesvalue.getAttribute("value");

Word Square Actionscript 3.0

Using the letters of the word the user inputs, I want to print a square by shifting the letters of the word to the left one position on each line.For example, since COMPUTERS has nine letters, its square will be nine characters across by nine characters down. In each row, each character will be shifted to the end. However, the following program only does one iteration. Please advise. Thanks!
btnDetermine.addEventListener(MouseEvent.CLICK, displayVowels);
function displayVowels(e: MouseEvent): void {
var str1:String;
var str2:String = "";
var i:Number;
str1 = String(txtinString.text);
i=0;
for (i=0; i<str1.length;i++){
str2 = str2 + str1.charAt(i);
}
str2 = str2 + str1.charAt();
lblString.text += str1.charAt(i) + "\r" + str2 ;
}
function displayVowels(e:MouseEvent):void {
//defines your original string
var myString:String = String(txtinString.text);
//the total character length of the original text, determine how many lines to print
var tLength:int = myString.length;
//clears out the textfield in case theres already something on it
lblString.text = "";
//print the first line
lblString.appendText(myString);
//this loops through every line except line1, we printed line1 above
//thus tLength - 1
for (var line:uint = 0; line < tLength - 1; line++) {
//line break
lblString.appendText("\n");
//shift the characters
myString = shiftChar(myString);
//print the result into the textfield
//use appendText instead of textfield += "new text'
lblString.appendText(myString);
}
}
function shiftChar(_myString:String):String {
//save the char at the front, add it to the back later
var offset:String = _myString.charAt(0);
//slice the original string so that the first char is now removed
_myString = _myString.slice(1, _myString.length);
//add the first char of the original string to the back
_myString = _myString + offset;
//return the final results
return _myString;
}

C# Problems importing a file to an array and put each character in it's own index

I have a problem importing a text file to an array and put each character in it's own index?
Here is my code:
class Program
{
static void Main(string[] args)
{
string content = File.ReadAllText("Labyrint.txt", Encoding.UTF8);
// Use ToCharArray to convert string to array.
char[] array = content.ToCharArray();
Console.WriteLine(content);
// Loop through array.
for (int i = 0; i < array.Length; i++)
{
// Get character from array.
char letter = array[i];
// Display each letter.
Console.Write("Letter: " + letter);
// Console.WriteLine(letter);
}
}
}
The problem is it only display some of the characters? Now if I change the code a little to this:
class Program
{
static void Main(string[] args)
{
string content = File.ReadAllText("Labyrint.txt", Encoding.UTF8);
// Use ToCharArray to convert string to array.
char[] array = content.ToCharArray();
Console.WriteLine(content);
// Loop through array.
for (int i = 0; i < array.Length; i++)
{
// Get character from array.
char letter = array[i];
// Display each letter.
Console.Write("Letter: " + letter);
//Console.WriteLine(letter);
}
}
}
Then it display all the charaters in the array, but when I try to find out what index each character are in and add the 'i' to this code Console.Write("Letter: " + letter); so the code looks like this:
class Program
{
static void Main(string[] args)
{
string content = File.ReadAllText("Labyrint.txt", Encoding.UTF8);
// Use ToCharArray to convert string to array.
char[] array = content.ToCharArray();
Console.WriteLine(content);
// Loop through array.
for (int i = 0; i < array.Length; i++)
{
// Get character from array.
char letter = array[i];
// Display each letter.
Console.Write(" Letter: " + letter + " " + i);
//Console.WriteLine(letter);
}
}
}
The result go crazy and look like this:
Output Vindue
The text file is the one below, and I placed it in the debug folder in my project so I didn't had to provide a path.
TextFile
I tried the following code and it seems to work, if this still doesn't work it's likely you're using the wrong encoding type and the file has a different encoding than UTF-8.
If that is the case then open the file in an editor like Notepad++ and see what the encoding for the file is
EDIT
The reason it looks like it was starting at line 191 instead of 0 was you were exceeding the buffer height of the console, so below I've added code to set the buffer height to it's maximum. depending on the application you might want to use a smaller value.
class Program{
static void Main(string[] args){
//Increase Console Buffer Height
Console.BufferHeight = Int16.MaxValue - 1;
string content = File.ReadAllText(#"Labyrint.txt", Encoding.UTF8);
// Use ToCharArray to convert string to array.
char[] array = content.ToCharArray();
// Loop through array
for (int i =0; i<array.Length; i++) {
string letter = arrray[i].ToString()
.Replace("\r","\\r").Replace("\n", "\\n")
.Replace("\t","\\t").Replace(" ","<space>");
// Display each letter
Console.WriteLine("Letter {0}: {1}", i, letter);
}
Console.ReadLine();
}
}

How to iterate backwards through an array?

I want to take each value from the end of the array and iterate backwards to the middle of the array, adding each value to a string:
for (int x = testArray.length - 1; x > testArray.length/2; x--){
b+=testArray[x];
}
It doesn't seem to work because when I print b it is an empty space. Is it possible to iterate backwards through an array and add the values to a string. They are all String values.
One thing to note is that if the array's length is an odd number, then your code wont work, since the length divided by 2 would be a decimal. Convert the middle index to an int. You should be using StringBuilder too if you're using quite a bit of concatenations.
String[] names = {", and Henry, ", ", David", "Bob"};
StringBuilder allNames = new StringBuilder();
for (int i = names.length - 1; i >= (int) names.length/2; i--)
{
allNames.append(names[i]);
}
System.out.println(allNames);
String b = "";
for (int x = testArray.length - 1; x > testArray.length/2; x--){
b+=testArray[x];
}
System.out.println(b);

How to change already entered lines in a RichTextBox?

How does one change already appended or entered lines on the RichTextBox control?
I want to programmaticly insert a Timestamp in front of each line of input. TextBox1.Lines[] does not allow changes. I attempted to set my own array to Lines[] but didn't seems to work.
Use the RichTextBox.GetFirstCharIndexFromLine() method to find out where to insert the text. For example:
int prev = richTextBox1.SelectionStart;
int cnt = richTextBox1.Lines.Length;
for (int line = 0; line < cnt; line++) {
richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(line);
richTextBox1.SelectionLength = 0;
richTextBox1.SelectedText = DateTime.Now.ToString() + ": ";
}
richTextBox1.SelectionStart = prev;

Resources