My code:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text = infos;
}
timer4.Stop();
}
I want to insert all of 10 src values in RichTextBox, while my code do it only once.
You can use AppendText
Replace
richTextBox1.Text = infos;
with
richTextBox1.AppendText(infos);
OR
richTextBox1.Text += infos + Environment.NewLine;
This line is wrong.
richTextBox1.Text = infos;
This is right.
richTextBox1.AppendText= infos;
What your code is doing is setting the text to equal each infos, 10 times over.
So I'm guessing your output would be the last infos variable? What you might want to do instead is this:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text += infos; // the "+=" will add each infos to the textbox
}
timer4.Stop();
}
As you can see, if you use the += instead of just =, it will add each iteration to the whole, instead of just overriding the whole value each time.
Related
How can I call this array that initializes on a button click event:
private void button1_Click(object sender, EventArgs e)
{
int[] n = textBox1.Text.Split(' ').Select(int.Parse).ToArray();
richTextBox1.Text += "Entered values: ";
foreach (int num in n)
{
richTextBox1.Text += num + " ";
}
richTextBox1.Text += "\n";
}
to other parts of an array, say another click event.
I have tried declaring the array in the form class but that requires the array to have a pre-defined size which is problematic for other parts of the code.
EDIT: Solved! Thanks to the guys at stackoverflow. Solutions and comments were very helpful :D
You can declare the array in the Form's class without specifying its dimensions simply like this:
int[] n = null; //choose better name, and comment the use of the variable.
The rest of the methods (such as click event handlers) can use it like this:
private void someOtherButton_Click(object sender, EventArgs e)
{
if(n != null && n.Length > 0)
{
//do something with the array
}
}
You have to make the array to a field (You can initialize the array with the size 0, if that's a problem for your program you have to overthink the rest of your code). It would look like this then:
private int[] n = new int[0];
private void button1_Click(object sender, EventArgs e)
{
n = textBox1.Text.Split(' ').Select(int.Parse).ToArray();
By the way, I'd strongly suggest not to call the array 'n' but a meaningful name (e.g. _splittedTb1Content).
Use Generics collection type instead :
private void button1_Click(object sender, EventArgs e)
{
List<int> n= textBox1.Text.Split(' ').Select(int.Parse).ToList();
richTextBox1.Text += "Entered values: ";
foreach (int num in n)
{
richTextBox1.Text += num + " ";
}
richTextBox1.Text += "\n";
}
You can declare list n in your form class:
List<int> n;
I also recommend use stringBuilder inside your "foreach" to improve performance for longer list. Use following code if you are processing a longer list.
private void button1_Click(object sender, EventArgs e)
{
List<int> n= textBox1.Text.Split(' ').Select(int.Parse).ToList();
var sBuilder = new StringBuilder();
sBuilder.Append("Entered values: ");
foreach (int num in n)
{
sBuilder.Append(num + " ");
}
sBuilder.AppendLine();
richTextBox1.Text += sBuilder.ToString();
}
I'm in a bit of a pickle at the moment, I've created a bit of code that creates 4 textboxes and adds them to a table layout at run time (code below) but I'm struggling with getting text from it, I tried getting the value from it as you would string s = TxtBox1.Text.ToString(); but it just gets a null reference, then I tried txt.Text.ToString();and this just gets the text from the last text box that was created.
private void button2_Click(object sender, EventArgs e)
{
int counter;
for (counter = 1; counter <= 4; counter++)
{
// Output counter every fifth iteration
if (counter % 1 == 0)
{
AddNewTextBox();
}
}
}
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
}
I've looked all over for the answers to this and as of yet found nothing if anybody has any ideas I would be grateful.
Thanks
this code picks textbox1 from tableLayoutPanel1, cast it from Control to TextBox and takes Text property:
string s = ((TextBox)tableLayoutPanel1.Controls["TxtBox1"]).Text;
if you need them all, then iterate over textboxes:
string[] t = new string[4];
for(int i=0; i<4; i++)
t[i] = ((TextBox)tableLayoutPanel1.Controls["TxtBox"+(i+1).ToString()]).Text;
You can try
var asTexts = tableLayoutPanel1.Controls
.OfType<TextBox>()
.Where(control => control.Name.StartsWith("TxtBox"))
.Select(control => control.Text);
That will enumerate the Text value for all child controls of tableLayoutPanel1 where their type is TextBox and their name starts with "TxtBox".
You can optionally relax the filters removing the OfType line (that excludes any non TextBox control) or the Where line (that allow only the control which name matches your example).
Ensure to have
Using System.Linq;
at the beginning of the file.
Regards,
Daniele.
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
txt.KeyPress += txt_KeyPress;
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//the sender is now the textbox, so that you can access it
System.Windows.Forms.TextBox textbox = sender as System.Windows.Forms.TextBox;
var textOfTextBox = textbox.Text;
doSomethingWithTextFromTextBox(textOfTextBox);
}
I want to auto format a text entered in a textbox like so:
If a user enters 2 characters, like 38, it automatically adds a space. so, if I type 384052
The end result will be: 38 30 52.
I tried doing that, but it's ofr some reason right to left and it's all screwed up.. what I'm doing wrong?
static int Count = 0;
private void packetTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
Count++;
if (Count % 2 == 0)
{
packetTextBox.Text += " ";
}
}
Thanks!
It's much nicer if you just let the user type and then modify the contents when the user leaves the TextBox.
You can do that by reacting not to the KeyPress event, but to the TextChanged event.
private void packetTextBox_TextChanged(object sender, EventArgs e)
{
string oldValue = (sender as TextBox).Text.Trim();
string newValue = "";
// IF there are more than 2 characters in oldValue:
// Move 2 chars from oldValue to newValue, and add a space to newValue
// Remove the first 2 chars from oldValue
// ELSE
// Just append oldValue to newValue
// Make oldValue empty
// REPEAT as long as oldValue is not empty
(sender as TextBox).Text = newValue;
}
On TextChanged event:
int space = 0;
string finalString ="";
for (i = 0; i < txtbox.lenght; i++)
{
finalString = finalString + string[i];
space++;
if (space = 3 )
{
finalString = finalString + " ";
space = 0;
}
}
I used
int amount;
private void textBox1_TextChanged(object sender, EventArgs e)
{
amount++;
if (amount == 2)
{
textBox1.Text += " ";
textBox1.Select(textBox1.Text.Length, 0);
amount = 0;
}
}
Try this..
on TextChanged event
textBoxX3.Text = Convert.ToInt64(textBoxX3.Text.Replace(",", "")).ToString("N0");
textBoxX3.SelectionStart = textBoxX3.Text.Length + 1;
How to change character casing in TextBox? I need that 1 line character been Upper and second line character benn Lower
isv.CharacterCasing = CharacterCasing.Upper;
isv.Text = "Upper"
isv.CharacterCasing = CharacterCasing.Lower;
isv.Text = "Lower"
As Mark said, it's difficult to understand exactly what you need, but I think it's something like
string[] lines = isv.Text.Split('\n');
string finalText = string.Empty;
for (int i = 0; i < lines.length; i++)
finalText += i%2==0 ? lines[i].ToUpper() : lines[i].ToLower() + + Environment.NewLine;
isv.Text = finalText;
Keep in mind I wrote the code without the compiler :)
You can use TextBox.Lines property I guess.
something like:
private void button1_Click(object sender, EventArgs e)
{
string result = string.Empty;
result += textBox1.Lines[0].ToUpper() + Environment.NewLine;
result += textBox1.Lines[1].ToLower();
textBox1.Text = result;
}
isv.Text = isv.Text.Split(Environment.NewLine)[0].ToUpper() + isv.Text.Split(Environment.NewLine)[1].ToLower();
here is my code
private void Allocation_Matrix_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
int Total_Row_Check;
Total_Row_Check = getRow();
Textbox1.Text = Convert.ToString(Total_Row_Check);
if (Total_Row_Check >= Convert.ToInt32(Total_Processes.Text))
{
MessageBox.Show("rows cannot exceed from total number of processess");
}
}
}
public int getRow()
{
int Row = 0;
string[] arLines;
int i;
arLines = Allocation_Matrix.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (i = 0; i < arLines.Length; i++)
{
Row++;
}
return Row;
}
i want to update TextBox1 as i hit ENTER in richtextbox from keyboard...but Textbox keeps show only FirstRow and shows One(1)
How about using RichTextBox.GetLineFromCharIndex Method? It returns, the zero-based line number in which the character index is located.
Is Allocation_Matrix your RichTextBox?
Note that RichTextBox.Text returns the text with line feed ("\n") for the line breaks, where most all other Windows controls use a carriage return, line feed ("\r\n").
So if you are calling Split on the string returned by the Text property, it won't contain any Evironment.NewLine.
try Allocation_Matrix.Text.Lines.Count() method
try this
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}