printPreview landscape mode inC# - c#

How can I set the print Preview and print code to landscape orientation ?
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(700, 600);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Visible = false;
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage_1);

This did the trick
this.printDocument1.DefaultPageSettings.Landscape = true;

Won't this
var doc = new PrintDocument();
doc.DefaultPageSettings.Landscape = true;
do the trick?
It will probably take care of the print preview issue as well.

This works for me
private void button_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printDocument1.DefaultPageSettings.Landscape = true;
printPreviewDialog1.ShowDialog();
}

Related

I am making a chat app in c# wpf and it is doing something strange

So basically i just add a label to a panel once i get a message and im tracking the y axis of the previous message and im adding 35 to it every time a new message is detected and i use that variable as the y axis for each label, it worked fine so far but after i added a scroll bar it is acting wierd, the numbers are as i expected, but the messages are put so far apart its hard to see. This is my code:
using Networking;
using System.Net;
using System.Threading;
using System.ComponentModel;
namespace socket
{
public partial class Form1 : Form
{
string name = string.Empty;
BackgroundWorker threader = new BackgroundWorker();
bool connected;
Connector client;
int CurrentMsgY;
public Form1()
{
Form prompt = new Form();
prompt.MaximumSize = new Size(500, 200);
prompt.MinimumSize = new Size(500, 200);
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = "Enter a nickname";
Label textLabel = new Label() { Left = 50, Top = 20, Text = "Nickname: " };
TextBox inputBox = new TextBox() { Left = 50, Top = 40, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox);
prompt.ShowDialog();
name = inputBox.Text;
InitializeComponent();
connected = false;
client = new Connector(Dns.GetHostByName(Dns.GetHostName()).AddressList[1]);
threader.DoWork += RecvMsg;
threader.WorkerReportsProgress = true;
threader.ProgressChanged += OnChange;
}
void OnChange(object sender, ProgressChangedEventArgs e)
{
displayMsg((string)e.UserState);
groupBox1.VerticalScroll.Value = groupBox1.VerticalScroll.Maximum;
}
void RecvMsg(object sender, DoWorkEventArgs e)
{
while (connected)
{
string msg = client.receive(1024);
if (msg == null) return;
Console.WriteLine(msg);
threader.ReportProgress(percentProgress: 0, userState: msg);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Connecting to host");
client.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1], 1111);
client.send("{\"name\": \"" + name + "\"}");
connected = true;
CurrentMsgY = 0;
groupBox1.AutoScroll = false;
groupBox1.VerticalScroll.Enabled = true;
groupBox1.VerticalScroll.Visible = true;
groupBox1.HorizontalScroll.Visible = false;
groupBox1.HorizontalScroll.Enabled = false;
groupBox1.AutoScroll = true;
threader.RunWorkerAsync();
if (client.receive(1024) == "test")
{
Console.WriteLine("connected succesfully");
}
}
private void label1_Click(object sender, EventArgs e)
{
}
void displayMsg(string msg)
{
Label msgLabel = new Label()
{
Text = msg,
Parent = groupBox1,
Location = new System.Drawing.Point(10, CurrentMsgY),
//Size = new System.Drawing.Size(new System.Drawing.Point(0, 15)),
Font = new Font("Segoe UF", 12f, FontStyle.Bold, GraphicsUnit.Point),
AutoSize = false,
Size = new Size(groupBox1.Width, 40)
};
CurrentMsgY += 35;
label1.Text = Convert.ToString(CurrentMsgY);
msgLabel.Show();
}
private void Send_Click(object sender, EventArgs e)
{
if(textBox1.Text == "disconnect__=230") client.send("disconnect__=23");
else if(textBox1.Text != "disconnect__=230") client.send(textBox1.Text);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
client.send("disconnect__=230");
connected = false;
threader.WorkerSupportsCancellation = true;
threader.CancelAsync();
client.Disconnect();
}
}
}

Close previous windows form when clicked other button

i try to close windows form in mdiparent when i click other button, the result is when i click other button, it still appear from the back of new window. so how can i handle this?
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
Detail aa = new Detail();
aa.MdiParent = this;
aa.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}
You're making new instance of form and then closing it. That way you're not closing existing window but creating new (invisible) one and closing it. You should find existing window in collection of MdiChildren and then close it. Something like this:
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
var detailForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(Detail));
detailForm?.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
var selectForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(SelectIC));
selectForm?.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}

Create Button at runtime and save it permanently in c#

I have this code to create a button:
private void CreateButton(){
Button createButton = new Button();
// Set Button properties
createButton.Height = 40;
createButton.Width = 300;
createButton.BackColor = Color.Red;
createButton.ForeColor = Color.Blue;
createButton.Location = new Point(20, 150);
createButton.Text = "I am Button";
createButton.Name = "Button";
createButton.Font = new Font("Georgia", 16);
createButton.Click += new EventHandler(create_Button_Click);
this.Controls.Add(createButton);
}
private void create_Button_Click(object sender, EventArgs e)
{
MessageBox.Show("button is clicked");
}
and i want to save it permanently in the project. what is the best way to do this?

WebBrowser cant open Instagram c#

when i put Instagram's address in my WebBrowser object, on loading of program, it just shows a blank white page, and nothing else, where is problem?!
first i think it should be cause WebBrowser using IE, but IE(ver.11) loads Instagram successfully .
UPDATE:
this is the code which i use:
this.webB.Location = new System.Drawing.Point(93, 17);
this.webB.MinimumSize = new System.Drawing.Size(20, 20);
this.webB.Name = "webB";
this.webB.Size = new System.Drawing.Size(642, 324);
this.webB.TabIndex = 1;
this.webB.Url = new System.Uri("https://www.instagram.com/", System.UriKind.Absolute);
this.webB.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webB_DocumentCompleted);
this.webB.ScriptErrorsSuppressed = true;
Try this one :). This code is useful for loading urls(ex:Instagram) and download the source code of the appropriate page.
protected void LoadUrl(object sender, EventArgs e)
{
string url = txtUrl.Text.Trim();
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 768;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
string sourcecode=browser.documentText;
}

Set print orientation to landscape

i already can create a print to print a file in my windows forms. However, whenever i add this code:
printDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
I can't see the Orientation of the page become LandScape, it is still Portrait.
How do I make it LandScape as default? So, whenever i click PrintPreview or PrintFile, the Orientation of the page will become LandScape, not Portrait.
Here is the code:
private void PrintPreview(object sender, EventArgs e)
{
PrintPreviewDialog _PrintPreview = new PrintPreviewDialog();
_PrintPreview.Document = printDocument1;
((Form)_PrintPreview).WindowState = FormWindowState.Maximized;
_PrintPreview.ShowDialog();
}
private void PrintFile(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Test Page Print";
printDocument1.Print();
}
}
try setting the Landscape of PrintDocument as follows,
printDocument1.DefaultPageSettings.Landscape = true;
The pdfprinting.net library is excellent for implementing the print functionality and be productive. Here is the simple code snippet to set print orientation to landscape(pdfPrint.IsLandscape = true;)
var pdfPrint = new PdfPrint("demoCompany", "demoKey");
string pdfFile = #"c:\test\test.pdf";
pdfPrint.IsLandscape = true;
int numberOfPages = pdfPrint.GetNumberOfPages(pdfFile);
var status = pdfPrint.Print(pdfFile);
if (status == PdfPrint.Status.OK) {
// check the result status of the Print method
// your code here
}
// if you have pdf document in byte array that is also supported
byte[] pdfContent = YourCustomMethodWhichReturnsPdfDocumentAsByteArray();
status = pdfPrint.Print(pdfFile);

Categories

Resources