while i am trying to implement font menu item in notepad ,
im not getting text changed , is there any mistake in my code
this is my code
private void fontMenuItem_Click(object sender, EventArgs e)
{
FontDialog objFontForm = new FontDialog();
objFontForm.ShowDialog();
}
Here's a really simple example taken from DotNetPearls
//Create FontDialog instance
FontDialog fontDialog1 = new FontDialog();
// Show the dialog.
DialogResult result = fontDialog1.ShowDialog();
// See if OK was pressed.
if (result == DialogResult.OK)
{
// Get Font.
Font font = fontDialog1.Font;
// Set TextBox properties.
this.textBox1.Text = string.Format("Font is: {0}", font.Name);
this.textBox1.Font = font;
}
Related
I''m devoloping a multi tabbed notepad application. How do I perform a save all function on all the tabs on the application without opening a SaveFileDialog after I save the tabs. The method shown below works but it opens a SaveFileDialog for all the tabs.
string strfilename;
RichTextBox rtb = null;
private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
foreach (TabPage page in pages)
{
if (rtb != null)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
rtb.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
}
}
I tried it like this as well but only last saved tab gets saved
private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
{
TabControl.TabPageCollection pages = tabControl1.TabPages;
foreach (TabPage page in pages)
{
rtb = page.Controls[0] as RichTextBox;
if (rtb != null)
{
rtb.SaveFile(strfilename, RichTextBoxStreamType.PlainText);
}
}
}
This is my individual save function
public void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sv = new SaveFileDialog();
sv.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
if (sv.ShowDialog() == DialogResult.OK)
GetRichTextBox().SaveFile(sv.FileName, RichTextBoxStreamType.PlainText);
this.Text = sv.FileName;
strfilename = sv.FileName;
autosave(sv.FileName);
}
Though this is old,
You can save the file name for the tab with it's Name,
e.g.
Tabpage tb = new TabPage();
RichTextBox rtb = new RichTextBox();
tb.Name = Path.GetFullPath(ofd.FileName); // <--- 'ofd' is the OpenFileDialog
tb.Controls.Add(rtb);
tb.Text = Path.GetFileName(ofd.FileName); // <--- This sets the tab's text to the file's name, rather than the full path.
tabControl1.TabPages.Add(tb);
You can put that snippet of code into the part where you actually open a file.
So, when the user (Or you) presses the Save All button, this snippet of code should be used:
foreach (TabPage tb in tabControl1.TabPages)
{
if (File.Exists(tb.Name))
{
File.WriteAllText(tb.Name, ((RichTextBox)tb.Controls[0]).Text); // <--- You can optionally save the RTF!
}
else if (!File.Exists(tb.Name))
{
saveFileDialog(sender, e);
}
}
saveFileDialog(sender, e) is the Link to your 'Save As' dialog, so that the user may save the individual files that either were deleted and don't exist anymore, or files that never existed all together. (Like if the user presses 'New', and it creates a new tab with a RichTextBox in it, the name would not exist as a file.)
For your 'New Tab' button,
TabPage tb = new TabPage();
RichTextBox rtb = new RichTextBox();
tb.Name = "New.Txt";
tb.Text = "New.Txt";
tb.Controls.Add(rtb);
tabControl1.TabPages.Add(tb);
Optionally, you can create an integer to watch how many 'New.Txt' are created, and instead of it displaying 'New.Txt', it would display 'New*5.Txt' if there were already four tabs before it. (I won't go too far into that.)
If you have any questions, feel free to ask.
I'm using Visual Studio, WPF, C#, XAML.
My program has Input and Output buttons.
Input uses OpenFileDialog.
Output uses SaveFileDialog.
I need each button to remember its last used directory, but RestoreDirectory = true causes both button's last directory to be the same. Each does not remember it's own directory separate.
// Input Button
//
private void btnInput_Click(object sender, RoutedEventArgs e)
{
// Open 'Select File' Window
Microsoft.Win32.OpenFileDialog selectFile = new Microsoft.Win32.OpenFileDialog();
// Remember Last Dir
selectFile.RestoreDirectory = true;
// Show Window
Nullable<bool> result = selectFile.ShowDialog();
// Display Path
if (result == true)
{
tbxInput.Text = selectFile.FileName;
}
}
// Output Button
//
private void btnOutput_Click(object sender, RoutedEventArgs e)
{
// Open 'Save File' Window
Microsoft.Win32.SaveFileDialog saveFile = new Microsoft.Win32.SaveFileDialog();
// Remember Last Dir
saveFile.RestoreDirectory = true;
// Show Window
Nullable<bool> result = saveFile.ShowDialog();
// Display Path
if (result == true)
{
tbxOutput.Text = saveFile.FileName;
}
}
I found this code to print
// The PrintDialog will print the document
// by handling the document's PrintPage event.
private void document_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
// Insert code to render the page here.
// This code will be called when the control is drawn.
// The following code will render a simple
// message on the printed document.
string text = "In document_PrintPage method.";
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 35, System.Drawing.FontStyle.Regular);
// Draw the content.
e.Graphics.DrawString(text, printFont,
System.Drawing.Brushes.Black, 10, 10);
}
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();
private void printButton_Click(object sender, EventArgs e)
{
PrintDialog PrintDialog1 = new PrintDialog();
// Allow the user to choose the page range he or she would
// like to print.
PrintDialog1.AllowSomePages = true;
// Show the help button.
PrintDialog1.ShowHelp = true;
// Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
PrintDialog1.Document = docToPrint;
DialogResult result = PrintDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print();
}
}
I execute it and the result of the printing is an empty page, my question is where can i put the data to print? and how can i make the printed data as rows each row has label and value.
You create the PrintDocument as a private member:
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();
But that makes it hard to attach the event handler at the right moment. I suggest using the constructor:
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint;
//= new System.Drawing.Printing.PrintDocument();
public Form1() // the Form ctor
{
InitializeComponents();
docToPrint = new System.Drawing.Printing.PrintDocument();
docToPrint.PrintPage += document_PrintPage; // the missing piece
}
I'm trying to make a program that prints a textbox full text. When I'm generating it to a pdf to see the page before I print it some of the text is missing.
This is my code:
private void Print_Click(object sender, EventArgs e)
{
PrintDialog PD = new PrintDialog();
PrintDocument PDoc = new PrintDocument();
PD.Document = PDoc;
PDoc.PrintPage += new PrintPageEventHandler(PDoc_PrintPage);
DialogResult result = PD.ShowDialog();
if (result == DialogResult.OK)
{
PDoc.Print();
}
}
void PDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graph = e.Graphics;
graph.DrawString(textBox1.Text,
new Font("Arial", 12), new SolidBrush(Color.Black), 10, 10);
}
This is the text (Just for a test):
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
this is the textbox screenshot:
This is what I get:
as you can see some of the text is missing. I'm not really familiar with printing so I have no Idea how to solve this problem.
It would appear your problem is that you want the text to wrap when printing. Have a look at this previous thread: Automatically Word-Wrapping Text To A Print Page?
I want to print the contents of a simple TextBox. After I click the print button, PrintDialog is shown.
I found a lot of info but they all use RichTextBoxes. Is there an easy way to do something like this?
This print contents of textbox named textbox1
PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
public Form1()
{
InitializeComponent();
document.PrintPage += new PrintPageEventHandler(document_PrintPage);
}
void document_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(textBox1.Text, new Font("Arial", 20, FontStyle.Regular), Brushes.Black, 20, 20);
}
private void btnPrint_Click(object sender, EventArgs e)
{
dialog.Document = document;
if (dialog.ShowDialog() == DialogResult.OK)
{
document.Print();
}
}
Take a look at this: http://answers.yahoo.com/question/index?qid=20081230163003AA4xOaT,
and this: How to print the contents of a TextBox
Also, there is a tutorial on printing in C#: http://www.dreamincode.net/forums/topic/44330-printing-in-c%23/
If, after this, you still cannot print TextBox content from some reason, you can always create a new RichTextBox object and assign your TextBox's Text to its text. Then proceed with printing using the RichTextBox.