How to keep the "\n" format - c#

Creating text in notepad
`
private void sSubmit_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter(#"C:\Users\Dat.txt", true);
txt.Write(sTxtSurname.Text + ", " + sTxtFirstname.Text + "\n\n");
txt.Close();
}
`
displaying the text in a textbox
`
public void ReadFile()
{
TextReader reder = File.OpenText(#"C:\Users\Dat.txt");
textBox1.Text = reder.ReadToEnd();
}
`
it wont diplay the "\n"
for example, i put my name and age
when displaying, it should seperate the name and age, but it doesnt
`
Outputs:
Jiin Taq 19
`
`
Desired Output:
Jiin Taq
19
`

Related

how to show the checked checkbox into a messagebox in C#

I was wondering how to do like the below image:
In 'Summary' (on right), under 'You purchased:', I want to list the options user has checked in 'Available Books' (on left)
private void btnPurchase_Click(object sender, EventArgs e)
{
string BOOKS;
MessageBox.Show("You Purchase :\n"
+"\t" + BOOKS + "\n" //checked checkbox shows here
+ "The selected payment method is : " + payment
+ "\nYour comment about us : " + txtKomen.Text);
}
I use the code above, and it only show the first checked checkbox, how to add another checked checkbox in message box?
Should I use an array for the BOOKS? If that is the way, how to loop it into the messagebox?
You can do it like this:
private void btnPurchase_Click(object sender, EventArgs e)
{
string[] BOOKS;
var sb = new StringBuilder();
foreach(var item in BOOKS)
{
sb.Append($"\t{item}");
sb.AppendNewLine();
}
MessageBox.Show("You Purchase :\n"
+ sb.ToString()//checked checkbox shows here
+ "The selected payment method is : " + payment
+ "\nYour comment about us : " + txtKomen.Text);
}
Though I'm not sure if .Append and .AppendNewLine() exist in StringBuilder, they might have a different name in any case Visual Studio should tell you the correct name.
private void button1_Click(object sender, EventArgs e)
{
string books = "";
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
books += itemChecked + " ";
}
MessageBox.Show("You Purchase :\n" + "\t" + books + "\n");
}
If you want to use this you have to add CheckedListBox on your form.

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 + "')");
}

c# textbox not displaying all contents

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.

I need help on displaying multiple text boxes for C#?

I am trying to make a mailing label program using WinForms where you enter your name, state, city, etc. and it click on a button and it display all the text you entered in each box and displays it on one label. I am close but when i run my program, there is no space between the words. Here is my code:
namespace Mail_Label_Program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
lblMessage.Text = txtFirst.Text + txtLast.Text;
}
private void btnExit_Click(object sender, EventArgs e)
{
//This closes the program.
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
//This clears all textbox forms.
txtFirst.Text = string.Empty;
txtLast.Text = string.Empty;
txtCity.Text = string.Empty;
txtStreet.Text = string.Empty;
txtState.Text = string.Empty;
txtZip.Text = string.Empty;
}
}
}
Replace this:
lblMessage.Text = txtFirst.Text + txtLast.Text;
With this:
lblMessage.Text = txtFirst.Text + " " + txtLast.Text;
If someone enters leading/trailing blanks you might like this:
lblMessage.Text = trim(txtFirst.Text) + " " + (txtLast.Text);
That's because you didn't add a space between them when you concatenated the string. You can use:
lblMessage.Text = String.Join(" ", txtFirst.Text, txtLast.Text);
The same Join method can be used for any number of fields:
String.Join(" ", txtFirst.Text, txtLast.Text, txtCity.Text);
Alternatively, you can use String.Format:
string.Format("Name: {0} {1}, Address: {2}", txtFirst.Text, txtLast.Text, txtCity.Text);
lblMessage.Text = txtFirst.Text + " " + txtLast.Text;
Try
lblMessage.Text = string.Format("{0} {1}", txtFirst.Text, txtLast.Text);
you can add further text like
lblMessage.Text = string.Format("{0} {1} {2} {3}...", txtFirst.Text, txtLast.Text, text2, text3...);

Public override string toString, ArrayList and Listbox output in multiplelines?

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>

Categories

Resources