Odd Newline Behaviour in c# WPF - c#

I am migrating from Windows Forms in VB to WPF C# and I wrote the following code....
namespace TestWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
OutputBox.Document.Blocks.Clear();
}
private void AcceptButton_Click(object sender, RoutedEventArgs e)
{
OutputBox.AppendText(String.Format("You clicked the button.{0}",Environment.NewLine));
}
}
}
Where OutputBox is a RichTextBox.
Upon running the program and clicking the button a few times I get output like the following....
You clicked the button.You clicked the button.
You clicked the button.
You clicked the button.
Why does the 1st newline not appear, and then it inserts blank lines rather than just moving on to the next line.
If I change the code to
.AppendText(String.format("You clicked the button.{0}OK{0}",environment.NewLine
I get output
You clicked the button.
OKYou clicked the button.
OK
You clicked the button.
OK
What should my code be like to get output....
You clicked the button.
You clicked the button.
You clicked the button.

Instead of Environment.NewLine use "\r". Although I can't find any link now, I think there's a bug in how RichTextBox handles Environment.NewLine

Related

How to display a popup behind a popup?

I was just curious to know about creating a popup which generates another popup BEHIND it, whenever a button is clicked.
Let me explain more:
I have a button Bu in the main window.
When I click this button, a popup say Pop_a with a button Bu_a should appear.
When I click button Bu_a a popup Pop_b should be opened, but behind Pop_a.
In Pop_a, within the button click handler, call this.Activate() after the line that shows the Pop_b window.
It should look something like this.
public partial class Pop_a : Window
{
public Pop_a()
{
InitializeComponent();
}
private void Bu_a_Click(object sender, RoutedEventArgs e)
{
var pop_b = new Pop_b();
pop_b.Show();
this.Activate();
}
}
This will put Pop_a back on top of all other windows, after Pop_b has been shown.
If you call this.Activate() before calling pop_b.Show(), Pop_a will be put on top, and then Pop_b will be shown, which puts Pop_b on top, so the order of these method calls matters. Show new window, then Activate the window you want on top.

C# WPF Application Hiding button on click

I have been asked to create a C# WPF application in VS 2013 and scenario is as under.
At the start of the application, a form must appear with two buttons on it. On the top button, the text “Button 1” must be written and on the bottom button the text “Button 2” must be written. For the first time, when any of the button 1 or button 2 is clicked then it will disappear and the other button will stay unchanged. For example, user clicks button 1 for the first time then button 1 will disappear. Button 2 will be unchanged. Now there is only one button on the window that is button 2. On clicking button 2 it will disappear and button 1 will reappear on the window. Now if the user clicks button 1 then it will disappear again and button 2 will reappear. This process may go on indefinitely until the application is terminated by clicking the close button.
I have created all the things but button1.Visible=true; cannot be entered VS is generating warnings that" sytem.windows.control.button does not contains a defination for Visible.
I need help please
In WPF we have Control.Visibility which needs to be set as Visibility.Visible or as required.
So you need to do it like this to hide:
button1.Visibility=Visibility.Collapsed
Under the Button Click Method you want this:
private void button1_Click(object sender, RoutedEventArgs e)
{
//Hides Current Button
button1.Visibility = Visibility.Collapsed;
//Checks and Shows Button 2
if (button2.Visibility == Visibility.Collapsed)
{
button2.Visibility = Visibility.Visible;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//Hides Current Button
button2.Visibility = Visibility.Collapsed;
//Checks and Shows Button 1
if (button1.Visibility == Visibility.Collapsed)
{
button1.Visibility = Visibility.Visible;
}
}
In order to obtain these methods you should declare a Click="" Method in XAML
<Button Name="button1" Click="button1_Click"><Button>
<Button Name="button2" Click="button2_Click"><Button>

C# form validation with tooltip

I am coding a C# Forms application and I have a question about validating a form.
I have a textbox called textBoxCodeToSearchFor that I want to validate when I press a button. If the textbox does not have any text in it, I need a tooltip to be shown.
Here is my code:
private void textBoxCodeToSearchFor_Validating(object sender, CancelEventArgs e)
{
if (String.IsNullOrEmpty(textBoxCodeToSearchFor.Text))
{
e.Cancel = true;
toolTip.Show("Please enter the code to search for", textBoxCodeToSearchFor);
}
}
On the button click I have the following code:
bool validated = this.Validate();
The tooltip is then shown, however, I cannot close the form when pressing a cancel button.
How can I shown a tooltip for a textbox if the textbox does not validate, but still close out of the form is wanted?
Thanks in advance.

Click listener not firing

I'm not able to get a basic click listener to work.
I've created a base button with the (Name) of button1.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("button1 was clicked");
}
When I run the application and click my button it doesn't fire. I'm struggling to figure this out because I'm following the guide outlined here: http://msdn.microsoft.com/en-us/library/dfty2w4e.aspx
It appears you're missing this line button1.Click += new EventHandler(button1_Click);, you should add this in the forms constructor.
If you go through the UI designer you can just drag the button onto the UI, then double click it and it will create the listener for you; everything you have there, plus the line above in the designer file, minus the MessageBox.Show(). This is the route I would recommend for creating all of your standard listeners.
If you click on the button on your form in the designer and view properties and select the events section, do you see a value next to the event "Click"? You should see the value "button1_click". If you don't see this, then that is your problem. You should be able to click the dropdown and select the button1_click event.

Message box causes loss of focus

I have created a tool bar which has three controls. First one being a text box , an OK button and a Clear button. Essentially I am using this toolbar to search some text. When there are no results found , I pop up a message box informing the user that no results were found. But when the user clicks "OK" button of the message box, the text box looses focus and the focus passes to the next control which is the "OK" button. What should I do to avoid the text box to loose focus. I am using c#.
You can't. Clicking on the Ok button forces it to have control (and therefore the textbox loses control).
You can, however, do this on your click event:
MessageBox.Show("asdf");
textBox1.Focus();
EDIT
In response to your comment, I don't think that there's an easy way to return focus to the last control once another control has received focus, and the search and clear buttons will have to receive focus when clicked. You can do this:
private Control _last;
private void textBox1_Leave(object sender, EventArgs e)
{
_last = (Control) sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("asdf");
_last.Focus();
}
It's not very clear your question, but you can make your control take the focus like this:
textBox1.Focus();

Categories