What is windows form c# writeline equivalent - c#

I have created an order form that includes a 16 length string array. Depending on the customer selection, I need the info from the array to appear in a text box e.g order summary. I can't figure it out. Here is sample code from one radio button and one check box. If these were selected how do I get the selection to display in a box? Please note I already have the "cost" part of it working correctly.
//Handle CPU Box Radio Btn
if (rdInteli3.Checked)
{
cost += 100.00;
item [0] = "Intel i3";
}
//Handle Hard Drive Check Box
if (ckHardDrive1Tb.Checked)
{
cost += 200.00;
item[11] = "1 TB Hard Drive";
}
I've tried this. Didn't work.
for (int i = 0; i < 16; i++)
{
txtSummary.Text = item[i];
}
Thanks

You'll have a multi-line TextBox control. You can append text to the control by calling its AppendText method. You code would look like this:
txtSummary.Clear();
for (int i = 0; i < 16; i++)
{
txtSummary.AppendText(item[i]);
}
You may wish to include new lines each time you add an item. In that case change the code like so:
txtSummary.AppendText(item[i]);
txtSummary.AppendText(Environment.NewLine);
or perhaps:
txtSummary.AppendText(item[i] + Environment.NewLine);
An alternative form is to use concatenation on the Text property:
txtSummary.Clear();
for (int i = 0; i < 16; i++)
{
txtSummary.Text += item[i] + Environment.NewLine;
}
And yet another option would be to build the text outside the control, for instance using a StringBuilder instance, and then assigning it all to the Text property in one go.

I guess that you want to use MessageBox:
MessageBox.Show("Hello World");

What is windows form c# writeline equivalent
Its the same Console.WriteLine but since there is no console you will see the output in Output window.
I need the info from the array to appear in a text box
You need to build a string , better if you use StringBuilder. Append your data there and then assign the result to your TextBox.Text property.
If your data is in array item then , its better if you use string.Join like:
txtSummary.Text = string.Join(Environment.NewLine, item);
If you want to use StringBuilder then you can do:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < item.Length; i++)
{
sb.Append(item[i]);
}
txtSummary.Text = sb.ToString();
Using string builder, how do I had space between the selection and
maybe a comma?
You can do:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < item.Length; i++)
{
sb.Append(item[i]);
sb.Append(" ,");
}
txtSummary.Text = sb.ToString().Trim(',',' ');
Or better
txtSummary.Text = string.Join(" ,", item);

You can use StringBuilder or String.Format to build a complete string that you want to show to the user. String.Format uses similar semantics as WriteLine.
Then assign this string to a MessageBox, a label or a textfield on your form or whereever you want it.

You can use that:
using System.Diagnostics;
Debud.WriteLine("This is test string");
You can check the result at the Output Window

Related

how to copy received data from string into 1d char array c# forms

So im just learning C# and trying to use arrays, im getting input from user via a forms app and wish to copy it to an array called prevPos, in the format below
receiving data (string):
string1: "hello"
string2: "123"
//counting how many lines and using that to determine position associated with each
recieved
char[] prevPos;
prevPos = textBox_ReceievedData.Text.ToCharArray();
//count how many lines of receieved data in textbox
for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
{
System.Console.WriteLine("charArray " +prevPos[i]);
}
right now if i wish to call it, i would get this, i do not want this fomat:
prevPos[1]=h
prevPos[2]=e
prevPos[3]=l
etc.
I want this output:
prevPos[1]=hello
prevPos[2]=123
please replace with the below and try
for (int i = 0; i < textBox_ReceievedData.Lines.Length; i++)
{
System.Console.WriteLine("Each Line " +textBox_ReceievedData.Lines[i]);
}
prevPos = textBox_ReceievedData.Text.Split('\n');
This will give you an array of all text separated by the new line character \n.
If that is the output you desire, then a string array (not a char array) is what you want. The Lines() property already gives you that, though:
string[] prevPos = textBox_ReceievedData.Lines;
for(int i=0; i< prevPos.Length; i++)
{
System.Console.WriteLine(prevPos[i]);
}

Use generated string as names of textBoxes?

I'm trying to put the contents of a ton of textBoxes into a database and to make the code shorter i'm trying to use generated strings as names of the textboxes, but how can i apply this?
for (int i = 0; i <= 9; i++)
{
for (int i2 = 0; i2 <= 7; i2++)
{
string tbName = "textBox" + i.ToString() + i2.ToString();
[buttonName].Text="someting";
}
}
If this would work i could reduce my repetitive code a lot. How can i use the contents of a string as the name of a textBox?
You can use FindControl method to get the instance of the textbox and then use it :
TextBox txtBox = FindControl(tbName) as TextBox;
if(txtBox !=null)
txtBox.Text="someting";
But if the textBoxes are nested in the other controls, then you will need to recursively look in to every control to find the needed text box control explained at following:
https://msdn.microsoft.com/en-us/library/y81z8326.aspx

add an empty line inside text file

So let me give you an example of how my file could look:
Hello
my
name
is
Now, for example, I want to go through the different lines of this file with System.IO.File.ReadAllLines(); and then check with a loop whether the current line has the word "my" in it (so the second line in this case).
As my next step, I want to add a new line right after "my", so it looks like this:
Hello
my
name
is
I approached this with 2 methods now. I was hoping File.Append(); would offer a method where I could append anything after it has found the string I am looking for, but obviously it only offers methods to append strings to the end of files.
My second approach was to read in all the lines with string[] test=System.IO.File.ReadAllLines();
and then iterate through all the lines, checking each line with
for (int i = 0; i < (test.Length - 1); i++)
{
if(test[i].Contains("my"))
{
test[i] = test[i] + Environment.NewLine;
}
}
and then write all this back in the file with System.IO.File.WriteAllLines();
The problem I am facing here is the fact that this command does not really add a real new line to the file, as I've checked test.Length before and after, and both time I got 4 as a result.
Another option is to add the lines to a List which would give you the Insert() method:
*Only use this for relatively small files.
Something like:
string path = #"c:\some\path\file.txt";
List<String> lines = new List<string>(System.IO.File.ReadAllLines(path));
for (int i = 0; i < lines.Count; i++)
{
if (lines[i].Contains("my"))
{
if (i < lines.Count -1)
{
lines.Insert(i + 1, "");
}
else
{
lines.Add("");
}
}
}
System.IO.File.WriteAllLines(path, lines.ToArray());
First, I suggest you use StringBuilder. It's best to use it when you're adding many strings, since strings are immutable and thus each string is created again when you do +=, or simply assigning a new one to an array slot.
This code will do what you're looking for, and it treats the no new line edge case:
var filePath = //your file path
var test = File.ReadAllLines(filePath);
var sb = new StringBuilder();
for (int i = 0; i < (test.Length - 1); i++)
{
sb.Append(test[i]);
sb.Append(Environment.NewLine);
if (test[i].Contains("my"))
{
// This adds that extra new line
sb.Append(Environment.NewLine);
}
}
sb.Append(test[test.Length-1]);
File.WriteAllText(filePath, sb.ToString());
[TestMethod]
public void InsertLines()
{
var test = File.ReadAllLines(#"c:\SUService.log");
var list = new List<string>();
for (int i = 0; i < (test.Length - 1); i++)
{
list.Add(test[i]);
if (test[i].Contains("my"))
{
list.Add(Environment.NewLine);
}
}
File.WriteAllLines(#"c:\SUService.log", list);
}
Not for the above question: But in general if you want to add a new line after a line you just have to add a new line character -> so if you want to add an empty line after a line Just add 2 newline characters
File.WriteAllText(filePath,"\n\n");
Here the the writer will jump 2 times and add the upcoming contents in the send line

Get value from multiple TextBox elements

I have a few different TextBox elements named as followed "e0", "e1", "e2", "e3". I know how many there are and I just want to be able to loop through them and grab their values rather than typing each one out manually.
I'm assuming I'll be doing something like this, I just don't know how to access the element.
for(int i= 0; i < 4; ++i) {
string name = "e" + i;
// How do I use my newly formed textbox name to access the textbox
// element in winforms?
}
I would advise against this approach since it's prone to errors. What if you want to rename them, what if you'll forget about this and add other controls with name e...?
Instead i would collect them in a container control like Panel.
Then you can use LINQ to find the relevant TextBoxes:
var myTextBoxes = myPanel.Controls.OfType<TextBox>();
Enumerable.OfType will filter and cast the controls accordingly. If you want to filter them more, you could use Enumerable.Where, for example:
var myTextBoxes = myPanel.Controls
.OfType<TextBox>()
.Where(txt => txt.Name.ToLower().StartsWith("e"));
Now you can iterate those TextBoxes, for example:
foreach(TextBox txt in myTextBoxes)
{
String text = txt.Text;
// do something amazing
}
Edit:
The TextBoxes are on multiple TabPages. Also, the names are a little
more logical ...
This approach works also when the controls are on multiple tabpages, for example:
var myTextBoxes = from tp in tabControl1.TabPages.Cast<TabPage>()
from panel in tp.Controls.OfType<Panel>()
where panel.Name.StartsWith("TextBoxGroup")
from txt in panel.Controls.OfType<TextBox>()
where txt.Name.StartsWith("e")
select txt;
(note that i've added another condition that the panels names' must start with TextBoxGroup, just to show that you can also combine the conditions)
Of course the way to detect the relevant controls can be changed as desired(f.e. with RegularExpression).
You can use parent of your controls like this(Assuming you have placed all controls in the form, so I have used this)
for(int i= 0; i < 4; ++i) {
string name = "e" + i;
TextBox txtBox=this.Controls[name] as TextBox;
Console.Writeline(txtBox.Text);
}
Try this :
this.Controls.Find()
for(int i= 0; i < 4; ++i) {
string name = "e" + i;
TextBox txtBox = this.Controls.Find(name) as TextBox;
}
Or this :
this.Controls["name"]
for(int i= 0; i < 4; ++i) {
string name = "e" + i;
TextBox txtBox = this.Controls[name] as TextBox;
}

in wpf, how loop output the data in textbox?

The following code can only display the last number:
for (i=0; i<3; i++)
textbox.text({0},i);
But I want to output numbers in textbox like this;
0
1
2
How can I do with it? thx.
Create a StringBuilder, like this more or less (pseudocode):
StringBuilder sb = new StringBuilder();
for (i=0; i<3; i++)
sb.AppendLine(string.Format({0},i));
textbox.Text = sb.ToString();
Just note: if you are using WPF avoid direct access to the control properties and operate on ViewModel instead, if not, you loose notable part of the benefits brought to you with that technology.
Linq way
var result=Enumerable.Range(0,3).Select(i=>i.ToString()).Aggregate((init, next)=> init+ Environment.NewLine+next);
textBox.Text = result;

Categories

Resources