I just want to write a simple text file. Is there some simple code for this?
It doesn’t get any simpler than this. In production code always make sure that you handle exceptions. using System.IO; …. // filePath has the complete path to the file StreamWriter writer = new StreamWriter(filePath); writer.Write(fileNewText); writer.Close();
I just want to read a text file into a string. Is there some simple code for this?
The following code reads a text file into a string object. It doesn’t get any simpler than this. In production code always make sure that you handle exceptions. using System.IO; …. // filePath should contain the complete path to a file. StreamReader stream = new StreamReader(filePath); fileText = stream.ReadToEnd(); stream.Close();
I have a delete key shortcut for my main menu. When my textbox is being edited, pressing delete activates this menu items instead of being processed by the TextBox. How can I get my TextBox to handle this delete
Subclass the TextBox and override its PreProcessMessage method. public class MyTextBox : TextBox { const int WM_KEYDOWN = 0x100; const int WM_KEYUP = 0x101; public override bool PreProcessMessage( ref Message msg ) { Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode; if((msg.Msg == WM_KEYDOWN || msg.Msg == WM_KEYUP) && keyCode == Keys.Delete) { return false; } return base.PreProcessMessage(ref msg); } }
When I right-click a tree node, it does not become selected. How can I make it be selected on a right-click
Handle the treeview’s mousedown event, and if it is the right-click, then explicitly set focus to th enode under the click. private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { treeView1.SelectedNode = treeView1.GetNodeAt (e.X ,e.Y ); } }
If I have a button with &Next as the text, the N-accelerator key does not appear until I press Alt. Why
This behavior has nothing to do with .Net and is expected Windows behavior beginning with Win2000. Windows will only show accelerator keys after the alt is pressed. There is a control panel setting that you can use to change this behavior. Look on the Effects tab under Control Panel|Display to see a checkbox labeled ‘Hide keyboard navigation indicators until I use the Alt key’.