How can I stop a form from closing & wordrap a singline

S

supster

I'de like to ask users if they are able to save their work before they
close using the normal "Save? Yes, No, Cancel" type of message box.

I've got this so far:

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(unSaved)
{
DialogResult result = MessageBox.Show("Do you want to save
the changes you made to" +
Path.GetFileName(currentFilePath) + "?", "Save
Changes?", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Exclamation);
if(result==DialogResult.Yes)
saveFile(currentFilePath);
if(result==DialogResult.Cancel)
{
}
if(result==DialogResult.No)
{
}
}
}
}


What can I put in the result==DialogResult.Cancel if statement to
cancel the closing process?

Also, I'de like to have a textbox that wordraps a single line of text
so it can be short in width but a bit longer in height and still be
able to show a large string because of wordwrap. I would basically
like to have a multiline textbox with wordwrap on without allowing
users to insert new lines in the text box. Too bad wordwrap does not
work when multiline is false.
 
M

Morten Wennevik

Hi supster,

You only lack 1 line. In the "if cancel" put

e.Cancel = true;
 
M

Morten Wennevik

Oh yeah, and for the wordwrap textbox. Set MultiLine and ReadOnly. Then set BackColor to SystemColors.Window or some other color of choice, or leave the disabled color as it is.
 
M

Michael Voss

Hi!

Morten said:
Oh yeah, and for the wordwrap textbox. Set MultiLine and ReadOnly. Then
set BackColor to > SystemColors.Window or some other color of choice, or
leave the disabled color as it is.

Doesn't that make the textBox readOnly ? I needed a similar textBox some
time ago and worked it out like this:

========== C#-CODE ==========

private bool internalAction = false;

private void InitializeComponent()
{
//...
// Hook up the event for TextChanged
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
//...
}


private void textBox1_TextChanged(object sender, System.EventArgs e)
{
// Get CursorPosition
int position = ((TextBox)sender).SelectionStart;

// Check if change event was fired from typing in textBox
if (!this.internalAction == true)
{
// Get the current Text
string text = ((TextBox)sender).Text;
// Find position of <return>
long index = text.IndexOf("\r\n");
if (index > -1)
{
// We will remove two characters from text, so check
// if cursor position has to be changed
if (index <= position)
{
position = position - 2;
};
// Remove "/r/n" from text, making sure
// this method will not execute twice
this.internalAction = true;
((TextBox)sender).Text = text.Replace("\r\n", "");
this.internalAction = false;
}
}
// Reset cursor position
((TextBox)sender).SelectionStart = position;
}
 
M

Morten Wennevik

Hi!


set BackColor to > SystemColors.Window or some other color of choice, or
leave the disabled color as it is.

Doesn't that make the textBox readOnly ? I needed a similar textBox some
time ago and worked it out like this:

Yes, it will make the TextBox readonly ( Setting ReadOnly usually does :p ), but that was what he wanted.
"I would basically like to have a multiline textbox with wordwrap on without allowing users to insert new lines in the text box."

Your code appears to strip line breaks from an input to the textbox, but supster used a single line of string and did not want users to be able to write anything to it.
 
S

supster

Thanks for the e.Cancel thing, that helps a lot =)

I actually wanted users to be able to enter input, but just one line.

I tried a similar method Michael, on text change I tried:
textBox.Text = textBox.Text.Replace("\r\n", "")

That didn't work, any other ideas?
 
M

Michael Voss

Hi !
supster said:
I tried a similar method Michael, on text change I tried:
textBox.Text = textBox.Text.Replace("\r\n", "")

That didn't work, any other ideas?

Your above code worked fine for me (although the cursor will not be
positioned correctly); are you using a System.Windows.Forms.TextBox ? Do you
hook up to any other event ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top