Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, April 16, 2016 2:31 PM
Hello,
I am a beginner, switching over from Console applications to WPF. When checking code in a console application, you could simply write to the console to see that the code was working properly. Awesome! But now, in WPF, I want to know that my code is iterating through an array properly, but don't have the luxury of sending it to the console. Here are the specifics...
This is the code I use in a Console Application to show that I am iterating through my array.
static void Main(string[] args)
{
string[] names = new string[] { "Washington", "Lincoln", "Reagan" };
foreach (var name in names)
{
Console.WriteLine(name);
}
Console.ReadKey();
}
Great! I know my code is working. Using WPF, I am wanting to send that same code to a text box to have it displayed, so I know that the code is working properly. From there, the big picture is to use a random object to display just one name of the array when a button is pressed. But here is the code I have so far, to send that same list to a text box in WPF, rather than console window in a console application.
public MainWindow()
{
InitializeComponent();
string[] names = new string[] { "Washington", "Lincoln", "Reagan" };
foreach (var name in names)
{
textBox.Text = name;
}
}
When I run the code, only the last index of the array is presented. I've looked into TextWrapping, VerticalContentAlignment, etc. but there is nothing more there than the last index when I run this code. Why am I not getting each value as I do with the Console Application? I can't seem to find the answer to such a simple question online. (Also, if anyone knows of a definitive site to go to for beginners of WPF, I sure would like to know). Thank you.
vb199
All replies (2)
Saturday, April 16, 2016 5:41 PM ✅Answered
The simplest way is using the same Console class. It will display the information to Output window of Visual Studio. (Show it using View menu). However, it also displays some other information too. Instead of Console you can also use the Debug class.
Instead of Console.ReadKey, study and use “breakpoints”, which can be set by <F9> key. When the program stops, you can investigate the variables using Watch window and other useful features of the Debugger.
If you want to display the array to textbox, then try this:
textBox.Text = string.Join( Environment.NewLine, names );
or this:
textBox.Clear();
foreach( var name in names )
{
textBox.AppendText( name + Environment.NewLine );
}
Saturday, April 16, 2016 6:11 PM
Awesome! Thank you. And thank you for the suggestion regarding breakpoints. I'm getting on it.
vb199