c# textbox not displaying all contents - c#

I want to have a textbox that displays the word Seq (which is a column name), then lists values from mylist underneath it. So far, the values from the list show up but the word Seq doesn't
private void button7_Click(object sender, EventArgs e)
{
if (seq1)
{
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
foreach (object o in SeqIrregularities)
{
textBox1.Text = String.Join(Environment.NewLine, SeqIrregularities);
}
}
}

You're reassigning the value of textBox1.Text to your list of values, rather than appending the list of values to the textbox contents.
Try this:
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
textBox1.Text += Environment.NewLine + String.Join(Environment.NewLine, SeqIrregularities);
You also don't need to loop through your irregularities if what you're doing is creating a concatenated string of them.
Another way to do it (which may be clearer):
string irregularities = String.Join(Environment.NewLine, SeqIrregularities);
string displayString = " Seq" + Environment.NewLine + irregularities;
textBox1.Text = displayString;

change your code to this:
private void button7_Click(object sender, EventArgs e)
{
if (seq1)
{
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
foreach (object o in SeqIrregularities)
{
textBox1.Text += String.Join(Environment.NewLine, SeqIrregularities);
}
}
}
You were overwriting your text in each iteration of your foreach-statement. You have to use += instead of = in your foreach-statement.

Related

Add multiple items into array and display array in textbox

I currently have a form which takes in 3 user inputs from 2 textboxes and 1 numericUpDown.
I want to be able to get the values put in here when a button is clicked, and display the value of all 3 into a seperate text box.
The issue arises when there is multiple additions.
I tried creating an array but it still only displays the last input.
private void AddButton_Click(object sender, EventArgs e)
{
string newItem = NameTextBox.Text + "\t" + QuantityBox.Value.ToString() + "\t" + PriceBox.Text;
List<String> newItemList = new List<string>();
newItemList.Add(newItem);
for(int i = 0; i < newItemList.Count; i++)
{
BasketBox.Text = newItemList[i] + "\n";
}
}
Make your list outside the function, so u can maintain the list of inputs,
and display updated list on every click.
List<String> newItemList = new List<string>();
private void AddButton_Click(object sender, EventArgs e)
{
string newItem = NameTextBox.Text + "\t" + QuantityBox.Value.ToString() + "\t" + PriceBox.Text;
newItemList.Add(newItem);
for(int i = 0; i < newItemList.Count; i++)
{
BasketBox.Text = newItemList[i] + "\n";
}
}
You can do it like this:
private void AddButton_Click(object sender, EventArgs e)
{
string newItem = NameTextBox.Text + "\t" + QuantityBox.Value.ToString() + "\t" + PriceBox.Text;
List<String> newItemList = new List<string>();
newItemList.Add(newItem);
for(int i = 0; i < newItemList.Count; i++)
{
BasketBox.Text += newItemList[i] + "\n"; // this will add the text to your box
}
}
This will 'erase' old text in BasketBox:
BasketBox.Text = newItemList[i] + "\n"
This will add text in BasketBox:
BasketBox.Text += newItemList[i] + "\n"
There is a cool method String.Join which does the concatenation of string items from a list in one blow:
BasketBox.Text = String.Join(Environment.NewLine, newItemList);
If you declare the list outside than each time the button is clicked an item will be added to it and all items will be displayed:
List<String> newItemList = new List<string>();
private void AddButton_Click(object sender, EventArgs e)
{
string newItem = NameTextBox.Text + "\t" + QuantityBox.Value.ToString() + "\t" + PriceBox.Text;
newItemList.Add(newItem);
BasketBox.Text = String.Join(Environment.NewLine, newItemList);
}
Edit:
This of course will only work if you have set the property Multiline to true.

How to take user input from a text box and display using a list C#?

private void addButton_Click(object sender, EventArgs e)
{
List<String> names = new List<String>();
names.Add(textBox1.Text);
names.Add(textBox2.Text);
names.Add(textBox3.Text);
displayTextBox.Text = displayMembers(names);
displayTextBox.Text = string.Join(" ", names);
}
public string displayMembers(List<String> names)
{
foreach (String s in names)
{
return s.ToString();
}
return null;
}
This is what i have so far, but what i need to do is be able to clear the textbox and type in something new and it will display in displayText box as well as what was entered the first time... I am new to programming and cannot figure out how to keep adding to the displaybox. Thanks
I think you are looking for this? I am guessing you want to append whatever is typed in to the displayTextBox control.
BTW, your displayMembers() won't work like you want, it returns the very first element in the enumeration.
The code below will append all the entered names to the DisplayTextBox:
displayTextBox.Text = displayTextBox.Text +
" " + textBox1.Text + " " +textBox2.Text +" "+
textBox3.Text +" "+textBox4.Text;
Alternatively you should store a list of names like this:
declare an instance List variable:
private List<string> _names = new List<string>();
Then instead of changing the text of the displayTextBox simply add the name to the list:
_names.Add(textBox1.Text + " " +textBox2.Text +" "+
textBox3.Text +" "+textBox4.Text);
Then use the list to drive what is displayed in displayTextBox:
displayTextBox.Text = string.Join(" ", _names);
So to wrap it all together, change this:
//instance var
private List<string> _names = new List<string>();
private void addButton_Click(object sender, EventArgs e)
{
_names.Add(textBox1.Text + " " +textBox2.Text +" "+ textBox3.Text +" "+textBox4.Text);
displayTextBox.Text = string.Join(" ", _names);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}

How to create textbox at run time and take values from these text box ? in C# . Net

I need your help in making text box at run time and taking values from these text boxes that user enter. i have two button and one rich_text_box , when user click on one button it creates 3 text boxes and then user click on other button it should take value from text boxes and how in rich text box .
this is code i am using to create dynamic textbox
private void create_textbox_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Clear();
for(i=1;i<=3;i++)
{
TextBox text = new TextBox();
text.Name = "Text Box" + i.ToString();
//text.Text = "Text Box " + i.ToString();
flowLayoutPanel1.Controls.Add(text);
}
}
and this code i am using to take values from new created text boxes and displaying in rich text box .
private void get_value_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
for (i = 1; i <= 3; i++)
{
string value = text.Text + i.ToString();
richTextBox1.SelectedText = "\r\n" + value;
}
}
This should solve your problem:
private void get_value_Click(object sender, EventArgs e)
{
for (var c in flowLayoutPanel1.Controls)
{
var t = c as TextBox;
if (t != null)
{
richTextBox1.SelectedText = "\r\n" + t.Text;
}
}
}
In your method get_value_Click you aren't using any of the text boxes that were added to the flow layout panel. Something similar to Wolfgang Ziegler's answer should work but you will need to check the Type and Name in case you have other controls in the flow layout panel.
private void get_value_Click(object sender, EventArgs e)
{
for (i = 1; i <= 3; i++)
{
string value = this.flowLayoutPanel1.Controls["Text Box" + i].Text;
richTextBox1.SelectedText = "\r\n" + value;
}
}
This ought to do it.

wrapping strings around each new line in a rich text box using C#

I have 2 rich text boxes in my C# Winforms applications called, richtextbox1 and richtextbox2 I also have a button called button1. The idea is that when the end user pastes in a list of values into richtextbox1 eg,
C1
C2
C3
C4
The result of richtextbox2 should be: (this is what i want help with)
IN ('C1','C2','C3', 'C4')
This is what I have so far:
private void button1_Click(object sender, EventArgs e)
{
string strValues;
strValues = richTextBox1.Text;
//MessageBox.Show(strValues);
string strInStatement;
strInStatement = richTextBox2.Text;
List<string> idsList = new List<string>() { strValues };
string whereClause = string.Join(",", idsList).ToString();
richTextBox1.Lines = idsList.ToArray();
foreach (string value in idsList)
{
MessageBox.Show(value);
}
}
You can try this :
private void button1_Click(object sender, EventArgs e)
{
var textInEachLine = richTextBox1.Text.Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
string whereClause = string.Join("', '", textInEachLine).ToString();
MessageBox.Show(" IN ( '" + whereClause + "')");
}
This code will remove empty lines if any, and wrap text in each line with single quotes.
Try This :
private void button1_Click(object sender, EventArgs e)
{
string whereClause = String.Join("','", richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.None));
richtextbox2.Text = (" IN ( '" + whereClause + "' )");
}
Try This Code
private void button1_Click(object sender, EventArgs e)
{
string whereClause = String.Join("','", richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.None));
MessageBox.Show(" IN ( '" + whereClause + "')");
}

How do I prepend text from one textbox to another?

Basically I'm making a simple program to help take notes at my job. I have a one line textbox1, and a multiple line textbox2.
I want to be able to type whatever in textbox1, and then press "enter" and it show up in the first line in textbox2. Any help would be appreciated.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
//in form constructor, or InitializeComponent method
textBox1.Validated += DoValidateTextBox;
//in another place of your class
private void DoValidateTextBox(object sender, EvenArgs e) {
textBox2.Text = ((TextBox)sender).Text + Environment.NewLine + textBox2.Text;
}
This should work:
private void textBox1_KeyDown(object sender, KeyEventArgs e) // Keydown event in Textbox1
{
if (e.KeyCode == Keys.Enter) // Add text to TextBox2 on press Enter
{
textBox2.Text += textBox1.Text;
textBox2.Text+= "\r\n"; // Add newline
textBox1.Text = string.Empty; // Empty Textbox1
textBox1.Focus(); // Set focus on Textbox1
}
}
If you want to add text at the firstline of your textbox, then replace in the code above:
textBox2.Text = textBox1.Text + "\r\n" + textBox2.Text;
It depends on what you want the final result to be. If all you want is the first line of the second textbox to equal the first then:
void myEvent()
{
textbox2.Text = textbox1.Text;
}
If however you want whatever is in textbox1 to be appended to textbox2 every time you press a button, then you are better off using a ListView:
void myEvent()
{
myListView.Items.add(textbox1.Text);
}
If you specifically want a textbox though (with the data always appended to the first line):
void myEvent()
{
textbox2.Text = textbox1.Text + Environment.NewLine + textbox2.Text;
}

Categories

Resources