C# How to allow NEW LINE and WHITE SPACES in textBox [duplicate] - c#

This question already has answers here:
C# - Textbox Newline Problems
(6 answers)
Closed 7 years ago.
I am trying to write a string in a C# textBox by pressing a button. When I press the button my string should be added in the textBox. However, the string is added, but the "\n" is completely ignored. How can I allow it?
This is a very simple example that I have written only to understand where the problem is, but I noticed that the same thing happens with white spaces when I am trying to redirect my console output to a textBox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String s = "first line";
s += "\n";
s += "second line";
Console.Out.WriteLine(s);
textBox1.AppendText(s);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
how i call it:
class Program
{
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

Use \r\n or Environment.NewLine

Related

How to import a value from another Form in C# [duplicate]

This question already has answers here:
Passing Values from one Form to another Form in a Button click
(4 answers)
Closed 2 years ago.
Okay, I saw a couple of answers to similar questions but nothing was quite right, so I thought I would write down my own problem. I have a couple of Forms that are supposed to be connected.
private void btnAdd_Click(object sender, EventArgs e)
{
AuthorForm authorForm = new AuthorForm();
authorForm.Show();
}
This is the code from the Form2 that opens once I want to create a new list of Authors. With that, the third Form "AuthorForm" pops up and that's where I want to enter a name to add to a listBox that is in Form2. I don't know how to pass the string from the TextBox to the other form.
I'm sorry if this doesn't make much sense. I'm pretty bad at trying to explain what I need but if you have questions, I'll try to explain better.
Hi and welcome to StackOverflow. There are several ways to achieve this but I believe the easiest would be to use a property in your AuthorForm as follows:
// Inside AuthorForm create the following...
public string AuthorName { get; private set; }
Having done that, you can set this property either in text changed, or when the user clicks to commit the entered text, you then read this property from the Form2.
// This goes inside the text changed off your name text box.
this.AuthorName = AuthorTextBox.Text; // Assuming the name of the textox is AuthorTextBox
Inside Form2 you call read the property as follows.
private void btnAdd_Click(object sender, EventArgs e)
{
AuthorForm authorForm = new AuthorForm();
authorForm.ShowDialog();
// Read the entered text from the property
var userInput = authorForm.AuthorName;
}
You can proceed to make use of the text the user entered after this.
If i'm not mistaken you want to change Form2 instance after authorForm.Show(); statment
You can initial public variable for example name in Form2 and then send all the form2 instance to AuthorForm with constructor (use this keyword) , now you can change variable from form2 in authorForm
see :
AuthorForm authorForm = new AuthorForm();
authorForm.Show(this);
AuthorForm :
Form2 form2;
public AuthorForm(Form2 form2)
{
this.form2 = form2;
}
public void DoSomething()
{
form2.name = "test";
}
If I got it right, you want to get values from from AuthorForm.
You can do that by adding a property to the AuthorForm, like that:
public string AuthorName { get; set; }
And use authorForm.ShowDialog() so all the opened forms will freeze until you use the Close() method in the form you opened as a dialog. And then you can continue the form.
For example:
AuthorForm.cs:
public void BtnClose_Click(object sender, EventArgs e)
{
AuthorName = "rom_totach";
Close();
}
MainForm.cs:
public void BtnOpenAuthorForm(object sender, EventArgs e)
{
AuthorForm form = new AuthorForm();
form.ShowDialog();
MessageBox.Show(form.AuthorName);
}
I hope I helped!

winforms: Label showing systems.collections.genericlist '1 instead if data [duplicate]

This question already has answers here:
Convert a list to a string in C#
(14 answers)
Closed 4 years ago.
I've referred to this link (thread 1) but I get an error. For my example, I only have one list in one class. The class name is Savestate. I have 2 forms.
Form1 contain a textbox where the string that I saved inside will be transferred to the list when I press button1. Button1 will also open
Form2 where there is a label that should reflect the string in the textbox. However, the label will reflect systems.collection...
Below is my code.
Savestate: class name
public static List<string> number = new List<string>();
Form1
private void button1_click(object sender, System.EventArgs e)
{
Savestate.number.Add(textbox1.Text);
Formscollection.Form1.Hide(); //Form 1 and Form 2 saved in another class called formscollection
Formscollection.Form2.Show();
}
Form2 (Show the systems.collections..)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = Savestate. number.ToString();
}
I tried another code based on other forums but I received an error
Form2 (got error: cannot implicitly convert type void to string)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text = Console.WriteLine(item)
}
}
Hope to get help. Thanks.
You should display items of the list.
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = string.Join(", ", Savestate.number);
}
Or approach you already tried, but remove Console.WriteLine
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text += item;
}
}
you're trying to get a vlaue of the list. you can only get the last value of the LIST. and in the second code example you did wrong. Because Console.WriteLine doesnt work in win forms. Try to use this code:
foreach (string item in Savestate.number)
{
label1.Text += item;
}

Passing ComboBox values to a method on another form c# [duplicate]

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 6 years ago.
I have two issues with a method I am calling.
I have frmForm1 & frmForm2.
frmForm1 contains a method as below...
public frmForm1()
{
InitializeComponent();
}
//This method receives the prog name and WOtype names from frmForm2
int progID;
string programName;
public void GetIDandValue(string valName, int ID, string addWOValue)
{
if (valName == "progName")
{
progID = ID;
programName = addWOValue;
}
}
private void button1_Click(object sender, EventArgs e)
{
frmForm2 loadfrmForm2 = new frmForm2();
loadfrmForm2.Show();
}
Then from frmForm2 (which is opened from a btn click on frmForm1), I am trying to send values back to the method on frmForm1 so they can be used.
private void button1_Click(object sender, EventArgs e)
{
selectedValueID = Int32.Parse(comboBox1.ValueMember);
selectedValueName = comboBox1.DisplayMember;
string valToSend = "progName";
frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
this.Hide();
}
And finally, here's how combobox1 is being initialised...
comboBox1.DisplayMember = dsAddWO.Tables[0].Columns[1].ToString();
comboBox1.ValueMember = dsAddWO.Tables[0].Columns[0].ToString();
comboBox1.DataSource = dsAddWO.Tables[0];
comboBox1.Enabled = true;
Problem 1) combobox1 ValueMember and Displaymember are returning the column headers as values when i try to populate the variables (although the correct data is actually displaying in the combobox on the form).
Problem 2) I cant seem to call the GetIDandValue method from frmForm2, intellisense just doesnt see it.
No doubt im doing something incredibly stupid. Can anyone enlighten me?
You can pass the progID and programName to the constructor of frmForm2
Dose those codes are coded on form2 ?
private void button1_Click(object sender, EventArgs e)
{
selectedValueID = Int32.Parse(comboBox1.ValueMember);
selectedValueName = comboBox1.DisplayMember;
string valToSend = "progName";
frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
this.Hide();
}
If so, u'd better get a structor of from1, it seems u don't. like u did with form2: frmForm2 loadfrmForm2 = new frmForm2(); and then call to the method frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName); Totally Like This From1 frmForm1 =new From1();frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);

C# Windows Forms Application Read Textbox Line by Line

I want to make a program that has a multiline textbox, and the program will read it line by line.
All I need is to get the line into a string, and after I'm finished with that line, it moves on.
How can I do that?
Is there a built in function, like there is the getline() function in c++ and c?
Should I use a normal textbox or a richtextbox?
TextBoxBase.Lines property is what you're looking for.
Per request, here's a sample:
Code:
namespace SomeApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// For each line in the rich text box...
for (int i = 0; i < richTextBox.Lines.Length; i++)
{
// Show a message box with its contents.
MessageBox.Show(richTextBox.Lines[i]);
}
}
}
}
Result:

Can you change an inactive Form's text? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wondering if it is possible to change an inactive Form's text? The Form is Inactive because I have a MessageBox shown.
I have tried to do something like this:
private void ChangeFormText()
{
Form1 f = new Form1();
f.Text = "This doesn't work...";
}
But that doesn't work. I tried this:
private void ChangeFormText()
{
this.Text = "This still doesn't work...";
}
And this doesn't work also. I have also tried this:
Form1 form = null;
public void ChangeFormText()
{
form.Text = "And this won't work!";
}
But that throws an error.
This still doesn't work:
this.Text = "NOTHING WORKS";
Is there any way to change the Inactive Form's Text?
When the MessageBox is shown - any code in the Form won't run. And you can't put code into a MessageBox (as far as I know).
But what you can do is use a BackgroundWorker which works asynchronously.
This works:
public partial class Form1 : Form
{
BackgroundWorker w = new BackgroundWorker();
public Form1()
{
InitializeComponent();
w.DoWork += new DoWorkEventHandler(w_DoWork);
}
void w_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000);
Invoke(new Action(doit));
}
void doit()
{
Text = "Changed";
}
private void button1_Click(object sender, EventArgs e)
{
w.RunWorkerAsync();
MessageBox.Show("Random Text");
}
}
This is something you don't have to take care of. The title bar of a window already paints with distinctive colors, any Windows user is familiar with it. But you can, you have events for this:
private void Form1_Deactivate(object sender, EventArgs e) {
this.Text = "I miss you, come back soon";
}
private void Form1_Activated(object sender, EventArgs e) {
this.Text = "I'm back! What can I do to help you today?";
}
Unfortunately, not even the best intentions is going to stop that from being repetitive and annoying. Don't tell the user what he already knows and expects. Only tell him about the surprises.
If you want to change Text before calling MessageBox.Show(...), just do it normally. If you want to change Text after calling MessageBox.Show(...) you can use BeginInvoke to show the message box like this:
BeginInvoke((Action)(() => { MessageBox.Show("OK"); }));
Text = "????";

Categories