|
If I bodge up your code (fix the errors, use substitutes for external bits I don't have) it works ok in preview for me:
private void FrmMain_Shown(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += Pd_PrintPage;
PrintPreviewDialog pdi = new PrintPreviewDialog { Document = pd };
pdi.ShowDialog();
}
private void Pd_PrintPage(object sender, PrintPageEventArgs pe)
{
int X = 0;
int Y = 0;
RectangleF rect2 = panel2.ClientRectangle;
float image_draw_wd = panel2.Width;
float image_draw_ht = panel2.Height;
Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.FormatFlags = StringFormatFlags.LineLimit;
stringFormat.Trimming = StringTrimming.Word;
SizeF sz = pe.Graphics.MeasureString("A line of text to display", Font, (int)image_draw_wd, stringFormat);
double angle = 0;
double angleRadians = Math.Atan2(image_draw_ht, image_draw_wd);
angle = angleRadians * 180.0 / Math.PI;
pe.Graphics.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
pe.Graphics.RotateTransform((float)angle);
pe.Graphics.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));
X = 10;
Y = 10;
RectangleF dr_rect = new RectangleF(new Point(X, Y), new Size((int) image_draw_wd, ((int)sz.Height) + 10));
pe.Graphics.DrawString("A line of text to display", Font, myBrush, dr_rect, stringFormat);
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i already mention in preview its ok
|
|
|
|
|
Yes, but your code doesn't compile, so that has to be taken with a pinch of salt!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
And if I change my code:
private void FrmMain_Shown(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += Pd_PrintPage;
PrintDialog pdi = new PrintDialog { Document = pd };
pdi.ShowDialog();
pd.Print();
It prints fine to PDF, XPS, OneNote, ...
So the code seems OK as far as I can see.
So ... what happens if you fix the compiler errors, and print to - say - OneNote?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
private void Pd_PrintPage(object sender, PrintPageEventArgs pe)
{
string watermarkText = "Your Name © 2015, All Rights Reserved";
Font watermarkFont = new Font("Microsoft Sans Serif", 18, FontStyle.Bold);
int X = 0;
int Y = 0;
RectangleF rect2 = panel2.ClientRectangle;
float image_draw_wd = panel2.Width;
float image_draw_ht = panel2.Height;
Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.FormatFlags = StringFormatFlags.LineLimit;
stringFormat.Trimming = StringTrimming.Word;
SizeF sz = pe.Graphics.MeasureString(watermarkText, watermarkFont, (int)image_draw_wd, stringFormat);
double angle = 0;
double angleRadians = Math.Atan2(image_draw_ht, image_draw_wd);
angle = angleRadians * 180.0 / Math.PI;
pe.Graphics.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
pe.Graphics.RotateTransform((float)angle);
pe.Graphics.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));
X = 10;
X = (int)(panel2.Width - sz.Width) / 2;
Y = 10;
Y = (int)(panel2.Height - sz.Height) / 2;
RectangleF dr_rect = new RectangleF(new Point(X, Y), new Size((int)image_draw_wd, ((int)sz.Height) + 10));
pe.Graphics.DrawString(watermarkText, watermarkFont, myBrush, dr_rect, stringFormat);
}
preview , pdf n all looks same n fine why its not print on actual printer
my model is
HP deskjet gt 5811
|
|
|
|
|
At a guess, driver problems.
I can't test that - I have a Samsung colour laser as I got fed up with inkjets never working when I needed them ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
ok thank u so much for your support
|
|
|
|
|
No problem - sorry I can't do more!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
I have a dialogue window. In that I have a label called "Filter By". Its of Combo box style.
I am trying to type a letter “e”. It should bring up the next item in the drop down that begins with “E” but instead closes the window.
Any suggestions on how to rectify this?
|
|
|
|
|
What kind of app? Windows Forms, WPF, ASP.NET ... ?
Also, without seeing any code behind the window, it's going to be quite difficult to tell you what's going on. Just dropping a ComboBox on a form and running it isn't going to do what you described at all, so there's got to be some code on the form that's not behaving like you expect.
|
|
|
|
|
|
As you haven't provided any real detail, the most detailed answer I can give is that you'll need to do a SELECT using your value.
|
|
|
|
|
"SELECT"? He didn't say it was a SQL based database. You're making a lot of assumptions here
|
|
|
|
|
This is true. It could be a find for all I know. Well spotted.
|
|
|
|
|
It's easy:
commandText = "SELECT user from UserTable where id = @username"
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("@username", tbUser.Text);
|
|
|
|
|
It's not hard, it's easy - once you learn how to do it.
Just to add to what Richard gave you, the whole code is along the lines of:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
{
cmd.Parameters.AddWithValue("@ID", myTextBox.Text);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int age = (int) reader["Age"];
string desc = (string) reader["Description"];
Console.WriteLine($"{age}\n{desc}");
}
}
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Answering a question like this is harder.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Step one: Add new LINQ to SQL item from project Item in to your project and call it DXD (or any convinient name you like)
Step two: drag and Drop the Employee TABLE from Server Explorer in your IDE to your LINQ to SQL DataContext object.
Step Three: Add data source by selecting Object as data source
Step Four: Go to your form and drag and drop Salary field from your data source object to your form
Step Five: Set the Data Source of your EmployeeDatabinding object as EmployeeDatabinding.DataSource = DXD.Employee
...
it is too long try another approach
|
|
|
|
|
And what happens if the database is not SQL Server? What if the textbox is an ASP.NET Core one? You are making a lot of assumptions with your answer.
|
|
|
|
|
Pete O'Hanlon wrote: And what happens if the database is not SQL Server? What if the textbox is an ASP.NET Core one? You are making a lot of assumptions with your answer. Same can be said from OG's answer, which I still find very valuable.
The assumption for SQL Server is a valid one; if it is another database a similar technique is used and the example given remains valid with minimal change.
I'd argue that it should program against the interface, and use the CreateCommand method from the connection, taking the value of the textbox as an argument.
using (SqlConnection con = new SqlConnection(strConnect))
using (IDbCommand cmd = con.CreateCommand())
{
cmd.CommandText = "bla";
con.Open(); And yes, even that would be improved by using the DBProviderFactory, getting the connectionstring and provider from the Settings.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
There are as many ways to do this as there are people answering this question. Most of them depend on what mechanism you're using to access your database - Entity Framework - ADO - LINQ to SQL, or whatever. I personally prefer ADO, and I've written a reasonably generic DAL object to do it.
Generic DAL using ADO[^]
It can process stored proc calls or straight-up queries, and can map the returned data set to any object.
Beyond that, your question is so vague that there's nothing more I can suggest
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
How big is the "database" (table)? Sometimes, it's simply more efficient to load the entire table into memory and reference it (i.e. for "look-ups") during data entry. No fancy querying required.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
private void CheckContactNumber()
{
string checkContactNum = "SELECT COUNT(*) FROM Employee WHERE ContactNumber = " + addContactNum.Text + " "; //01234567890
OleDbCommand cmd = new OleDbCommand(checkContactNum, conn);
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
//if (dr.Read() && addContactNum.Text != "")
if (dr.Read())
{
int count = (int)dr[0];
if(count>0)
{
err += "Contact number is already listed in the database\r\n";
errorContactNum.Visible = true;
uniqueContactNumber = false;
}
}
conn.Close();
}
|
|
|
|
|
t's not hard, it's easy - once you learn how to do it.
Just to add to what Richard gave you, the whole code is along the lines of:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
{
cmd.Parameters.AddWithValue("@ID", myTextBox.Text);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int age = (int) reader["Age"];
string desc = (string) reader["Description"];
Console.WriteLine($"{age}\n{desc}");
}
}
}
}
|
|
|
|
|
I have c# windows form application which displays the real time data from a board. I want to plot this data in a point graph in c#. Help me with this. I have given my code below which displays the data from board.
namespace flowboard_v2
{
public partial class flow_click : Form
{
public flow_click()
{
InitializeComponent();
}
[DllImport("C:\\Users\\JivaSci\\Desktop\\fluigent\\bin\\SDK\\FRP\\C++\\Cpp_Windows\\frp_c_64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern UInt64 frp_initialization(ushort serial);
[DllImport("C:\\Users\\JivaSci\\Desktop\\fluigent\\bin\\SDK\\FRP\\C++\\Cpp_Windows\\frp_c_64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern Int64 frp_get_serial(UInt64 handle, ref ushort serial, ref ushort version);
[DllImport("C:\\Users\\JivaSci\\Desktop\\fluigent\\bin\\SDK\\FRP\\C++\\Cpp_Windows\\frp_c_64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern Int64 frp_close(UInt64 handle);
[DllImport("C:\\Users\\JivaSci\\Desktop\\fluigent\\bin\\SDK\\FRP\\C++\\Cpp_Windows\\frp_c_64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern Int64 frp_read_flow(UInt64 handle, byte index, ref byte timestamp, ref float flow);
[DllImport("C:\\Users\\JivaSci\\Desktop\\fluigent\\bin\\SDK\\FRP\\C++\\Cpp_Windows\\frp_c_64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern Int64 frp_data_FU(UInt64 handle, byte index, ref byte cal, ref byte res, ref byte artcod, ref byte sf, ref byte unit, ref byte tb);
public void Test()
{
/* Flow board variable declaration */
UInt64 frpHandle = 0;
ushort Serial = 0;
ushort Version = 0;
/* Flow-rate acquisition variables */
byte sensor_index = 0; // sensor index coresponds to flow-unit port on the flowboard from 0 to 7
byte TimeCheck = 0;
float flow_rate = 0;
uint loop = 0;
frpHandle = frp_initialization(0);
RTBox.Text+=("\n FRP session initialized");
frp_get_serial(frpHandle, ref Serial, ref Version);
RTBox.Text += ("\n FLOWBOARD SN:" + Convert.ToInt32(Serial));
if (Serial != 0)
{
frp_read_flow(frpHandle, sensor_index, ref TimeCheck, ref flow_rate);
RTBox.Text += ("\n Flow-rate:" + Convert.ToSingle(flow_rate) + "\t ul/min");
}
frp_close(frpHandle);
RTBox.Text += ("\n FRP session closed");
}
private void button1_Click(object sender, EventArgs e)
{
Test();
}
|
|
|
|
|