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.
Related
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 = "";
}
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 + "')");
}
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.
somehow i seem to be blind this morning ;)
i have the following code[1] which does read
the object collection out of an ListBox.
with the string b i can "retrieve" the strings
in b there are filenames and directory paths stored
that i want to backup with xcopy code [2].
Code:
private void btnBenutz_Click(object sender, EventArgs e)
{
lblAusgabe2.Text = "";
ListBox.ObjectCollection a = listBox1.Items;
foreach (string x in a)
{
b = x;
lblAusgabe2.Text += "\n" + b;
}
}
More code:
Process.Start("XCOPY.EXE", "/E /I /Y" + b + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\UserData\\");
how can i use b as an array which i probably have to ? otherwise only the first item will always been read out? Also the process start i have to use outside of the btnBenutz... so some variable has to be initialized in the public partial class Form2 : Form
Define b as List<string>. You also use a better name like fileNameList:
private List<string> fileNameList; // a class field, not a local variable
Then add the file names to the list:
private void btnBenutz_Click(object sender, EventArgs e)
{
lblAusgabe2.Text = "";
ListBox.ObjectCollection a = listBox1.Items;
foreach (string x in a)
{
fileNames.Add(x);
lblAusgabe2.Text += Environment.NewLine + x; // Why are you doing this?
}
}
Then in another place, run the xopy command for each file:
foreach(string fileName in fileNameList)
{
Process.Start("XCOPY.EXE", "/E /I /Y " + fileName + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\UserData\\");
}
if that's what you are trying to achieve!
private void btnBenutz_Click(object sender, EventArgs e)
{
var sb = new StringBuilder();
foreach (string x in listBox1.Items)
{
sb.Append("\n" + x);
}
// then use sb.ToString() somewhere...
}
As you commented you want to call it from other places also like another button click then
Do something like this :
1) Declare list of string at class level
List<string> fileNameList ;
2) Create a function with some meaningfull name let's say StartXcopy like below
public void StartXcopy()
{
ListBox.ObjectCollection a = listBox1.Items;
fileNameList = new List<string>();
foreach (string x in a)
{
fileNameList.Add(x);
lblAusgabe2.Text += "\n" + x;
}
foreach (string filename in fileNameList)
{
System.Diagnostics.Process.Start("XCOPY.EXE", "/E /I /Y" + filename + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\UserData\\");
}
}
3) Then call this function from where you want, like below in button click
private void btnBenutz_Click(object sender, EventArgs e)
{
lblAusgabe2.Text = "";
StartXcopy();
}
Note : Here i am assuming you are always iterating through listBox1 items.
I've been struggling with this C# problem all night.
I have a override ToString(), which is working fine, and I can put my data out in a ListBox. But as the data is very long, with a bunch of classes, the output becomes long.
I wanted to be able to break my ListBox output into multiplelines.
Here is the override in the class file:
//ToString
public override string ToString()
{
return "Name " + firstName + lastName + ". Nationality " + nationality + ". Lives in " + address + " " + zipCode + " " + city + " " + country + "."//
+ " Height is " + height + " meters. Hair color is " + hairColor + " and eye color is " + eyeColor + ". Specialmarkings: "//
+ specialMark + ". Is associated with " + association + ". Codename is " + codeName + "Photo (filename): " + photo;
}
Here is the index code:
public partial class Index : System.Web.UI.Page
{
static ArrayList personarraylist;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
personarraylist = new ArrayList();
}
}
protected void ButtonCreate_Click(object sender, EventArgs e)
{
//create new object
Person p = new Person(TextBox1FirstName.Text, TextBox2LastName.Text, TextBox3Nation.Text, TextBox4Address.Text, //
TextBox5City.Text, TextBox7Country.Text, //
TextBox10HairColor.Text, TextBox11EyeColor.Text, TextBox12SpecialMark.Text, TextBox13Asso.Text, TextBox14Codename.Text, TextBox15Photo.Text, //
Convert.ToDouble(TextBox9Height.Text), Convert.ToInt32(TextBox6ZipCode.Text), Convert.ToInt32(TextBox8Pass.Text));
//add object to arraylist
personarraylist.Add(p);
}
protected void ButtonShow_Click(object sender, EventArgs e)
{
//clear list box
ListBox1.Items.Clear();
//loop through Arraylist
for (int i = 0; i < personarraylist.Count; i++)
{
ListBox1.Items.Add(personarraylist[i].ToString());
ListBox1.Items.Add("");
TextBox1.Text = "";
}
}
}
Is it possible to break the output in multiplelines in a ListBox?
I was trying to inject some html breaktags in the override return, but these get stripped, yeah this is a webapplication.
Thanks in advance for your time.
PS I am a newbie in C# (Student), so be kind ;)
UPDATE:
Hi again all, thx for the help, I already tried with Environment.Newline and the other solutions, but these seem to be overlooked when displaying the text in a ListBox. I can see the breakpoints in the codebehind, but in the browser the listbox still just keeps it all in one line. So I decided to use a TextBox instead, which breaks the text automaticly and where I point out.
//loop through Arraylist
for (int i = 0; i < personarraylist.Count; i++)
{
TextBox1.Text += personarraylist[i].ToString();
}
Again thx for the help :-)
You can use Environment.NewLine or simply "\n" to create multiple lines of text.
If that doesn't work, you can try using the DataList control:
<asp:DataList id="myDataList" runat="server">
<ItemTemplate>
Line 1
<br />
Line 2
</ItemTemplate>
</asp:DataList>