|
I would like to know how to draw a square/rectangle, not filled, and have it move with the arrow keys. I have looked around and everything is just odd, and unworking. If anyone doesn't know how to draw it, that's fine, but I would really apprectiate the movement with arrows. Thanks in advance, M.S.
MasterSharp (not REALLY a master...)
|
|
|
|
|
If a rectangle is a target object, then create a Label control with no text and BorderStyle set to Single. Then handle KeyDown event of the parent Form.
If you are going to expand graphics, then use GDI (Graphics.DrawRectangle(...); ), however in such case you will have to invalidate a form in this rectangle (this.Invalidate(rect); ) (Another way a rectangle would leave it's trace after a movement).
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Hi,
there are four steps to draw something so it becomes visible on the screen:
1.
decide on what object you want to draw; it normally is a Control (e.g. a Panel) or a
Form itself. I prefer to add a Panel to a Form, then draw on the Panel.
2.
create some variables (Rectangle, struct, class, whatever) that hold the parameters of
your drawing. In your case that could be top and left coordinate, and width+height,
or just a Rectangle.
3.
create a Paint handler for that Panel, and do all your drawing in there, using the
Graphics class and your variables.
4.
when you want to animate things, modify the variables and call Panel.Invalidate() or
one of its overloads (for selective invalidation).
BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them
alive in class members (hence create them only once); or create them inside the Paint
handler and don't forget to call Dispose() on them.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I get this error using this code :
string strSQL = "";
if (klantID > 0)
{
strSQL = "INSERT...";
}
else
{
strSQL = "UPDATE...";
}
The error is on the line where the update-query is assigned to strSQL.
What's the reason I get this error ?
|
|
|
|
|
I don't see how that code can produce that error. Is this the actual code you are using?
|
|
|
|
|
This is the actual code (except for not mentionning the whole query because otherwise it would be too long).
|
|
|
|
|
Landie110 wrote: This is the actual code (except for not mentionning the whole query because otherwise it would be too long).
How do you build the query?
|
|
|
|
|
this is the full code:
if (klantID > 0)
{
strSQL = "INSERT INTO [IBAX].[dbo].[klanten] ([klantOndNr],[klantNaam],[klantStraat]" +
",[klantPostcode],[klantGemeente],[klantLand],[klantContact],[klantTelefoon]" +
",[klantGSM],[klantFax],[klantEmail],[klantURL],[klantTaal]) VALUES (";
strSQL = strSQL + "'" + klantOndNr + "', '"
+ klantNaam + "', '"
+ klantStraat + "', '"
+ klantPostcode + "', '"
+ klantGemeente + "', '"
+ klantLand + "', '"
+ klantContact + "', '"
+ klantTelefoon + "', '"
+ klantGSM + "', '"
+ klantFax + "', '"
+ klantEmail + "', '"
+ klantURL + "', "
+ klantTaal + ")";
}
else
{
strSQL = "UPDATE [IBAX].[dbo].[klanten] SET [klantOndNr] = '" + klantOndNr
+ "', [klantNaam] = '" + klantNaam
+ "', [klantStraat] = '" + klantStraat
+ "', [klantPostcode] = '" = klantPostcode
+ "', [klantGemeente] = '" + klantGemeente
+ "', [klantLand] = '" + klantLand
+ "', [klantContact] = '" + klantContact
+ "', [klantTelefoon] = '" + klantTelefoon
+ "', [klantGSM] = '" + klantGSM
+ "', [klantFax] = '" + klantFax
+ "', [klantEmail] = '" + klantEmail
+ "', [klantURL] = '" + klantURL
+ "', [klantTaal] = " + klantTaal
+ " WHERE klantid = " + klantID;
}
|
|
|
|
|
It must be something specificly with the update-instruction.
I've tried it using only the update (so no if...else, always use update) and I've got the same error message.
I've replaced the update with the exact same code for if (the insert-instruction) and then I don't get the error.
I've tried changing the building of the update instruction a little bit but I still get the same error.
|
|
|
|
|
Landie110 wrote: + "', [klantPostcode] = '" = klantPostcode
You have put an assignment operator (=) where the concatenation operator (+) should be.
-- modified at 16:51 Saturday 29th September, 2007
(Incidentally, the swedish word "klant" describes a person that has messed up. )
---
single minded; short sighted; long gone;
|
|
|
|
|
Well, spotted. I just went on a tangent about SQL Injection Attacks. (Well, what else am I going to talk about).
Just as well I asked to see the whole code.
|
|
|
|
|
Colin Angus Mackay wrote: I just went on a tangent about SQL Injection Attacks. (Well, what else am I going to talk about).
ROTFL !!!
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Guffa wrote: (Incidentally, the swedish word "klant" describes a person that has messed up. )
Just as well Landie110 is from Belgium where it means "client".
|
|
|
|
|
So the client is someone who messed up ? I mean, surely there's a link between two european countries using the same word to mean different things ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Are all the variables strings?
Also, it would be better if you didn't inject values into the SQL command as it can lead to SQL Injection Attacks.
sqlSQL = "UPDATE [IBAX].[dbo].[klanten] "+
"SET [klantOndNr] = @klantOndNr, "+
"[klantNaam] = @klantNaam";
mySqlCommand.CommandText = strSQL;
mySqlCommand.Parameters.AddWithValue("@klantOndNr", klantOndNr);
mySqlCommand.Parameters.AddWithValue("@klantNaam", klantNaam);
If you restructure as above, you might find your error easier to find (if it doesn't fix it) AND the security of your application improved.
|
|
|
|
|
I've changed my code to what you suggested and it works.
Thanks !
|
|
|
|
|
HI All,
I am trying to put together a print function but I am having some trouble with it. I have looked at numerous code and have tried to put my own together but I getthe following error message, "Object not reference not set to an instance of an object". I think I know where the problem is but do't know how to fix it.
<br />
public partial class Orders : Form<br />
{<br />
<br />
private PrintDocument printDoc = new PrintDocument();<br />
private PageSettings pgSettings = new PageSettings();<br />
private PrinterSettings prtSettings = new PrinterSettings();<br />
<br />
<br />
<br />
<br />
<br />
<br />
public Orders()<br />
{<br />
MenuItem fileMenuItem = new MenuItem("&File");<br />
MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", new EventHandler(filePageSetupMenuItem_Click));<br />
MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", new EventHandler(filePrintPreviewMenuItem_Click));<br />
MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP);<br />
<br />
fileMenuItem.MenuItems.Add(filePageSetupMenuItem);<br />
fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem);<br />
fileMenuItem.MenuItems.Add(filePrintMenuItem);<br />
this.Menu = new MainMenu();<br />
this.Menu.MenuItems.Add(fileMenuItem);<br />
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);<br />
<br />
InitializeComponent();<br />
}<br />
<br />
#region Printing Code<br />
<br />
private void filePrintMenuItem_Click(Object sender, EventArgs e)<br />
{<br />
printDoc.DefaultPageSettings = pgSettings;<br />
PrintDialog dlg = new PrintDialog();<br />
dlg.Document = printDoc;<br />
if (dlg.ShowDialog() == DialogResult.OK)<br />
{<br />
printDoc.Print();<br />
}<br />
}<br />
private void filePrintPreviewMenuItem_Click(Object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
<br />
PrintPreviewDialog dlg = new PrintPreviewDialog();<br />
dlg.Document = printDoc;<br />
dlg.FormBorderStyle = FormBorderStyle.Fixed3D;<br />
dlg.SetBounds(20, 20, this.Width, this.Height);<br />
dlg.ShowDialog();<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message.ToString());<br />
}<br />
<br />
<br />
<br />
}<br />
private void filePageSetupMenuItem_Click(Object sender, EventArgs e)<br />
{<br />
PageSetupDialog pageSetupDialog = new PageSetupDialog();<br />
pageSetupDialog.PageSettings = pgSettings;<br />
pageSetupDialog.PrinterSettings = prtSettings;<br />
pageSetupDialog.AllowOrientation = true;<br />
pageSetupDialog.AllowMargins = true;<br />
pageSetupDialog.ShowDialog();<br />
}<br />
<br />
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)<br />
{<br />
<br />
DrawAll(e.Graphics);<br />
<br />
}<br />
Problem is in here.
private void DrawAll(Graphics g)<br />
{<br />
RectangleF srcRect = new Rectangle(0, 0, this.BackgroundImage.Width, BackgroundImage.Height);<br />
<br />
Then the rest of the code
<br />
int nWidth = printDoc.PrinterSettings.DefaultPageSettings.PaperSize.Width;<br />
int nHeight = printDoc.PrinterSettings.DefaultPageSettings.PaperSize.Height;<br />
RectangleF desct = new Rectangle(0, 0, nWidth, nHeight / 2);<br />
<br />
g.DrawImage(this.BackgroundImage, desct, srcRect, GraphicsUnit.Pixel);<br />
<br />
float scalex = desct.Width / srcRect.Width;<br />
float scaley = desct.Height / srcRect.Height;<br />
<br />
Pen aPen = new Pen(Brushes.Black, 1);<br />
<br />
for (int i = 0; i < this.Controls.Count; i++)<br />
{<br />
if (Controls[i].GetType() == this.t1TextBox.GetType())<br />
{<br />
TextBox theText = (TextBox)Controls[i];<br />
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());<br />
}<br />
if (Controls[i].GetType() == this.groupBox1.GetType())<br />
{<br />
GroupBox theGroup = (GroupBox)Controls[i];<br />
Rectangle aRect = theGroup.Bounds;<br />
g.DrawRectangle(aPen, aRect.Left * scalex, aRect.Top * scaley, aRect.Width * scalex, aRect.Height * scaley);<br />
}<br />
if (Controls[i].GetType() == this.orderDateDateTimePicker.GetType())<br />
{<br />
DateTimePicker aPicker = (DateTimePicker)Controls[i];<br />
g.DrawString(aPicker.Text, aPicker.Font, Brushes.Black, aPicker.Bounds.Left * scalex, aPicker.Bounds.Top * scaley, new StringFormat());<br />
}<br />
if (Controls[i].GetType() == this.comboBox1.GetType())<br />
{<br />
ComboBox theCombo = (ComboBox)Controls[i];<br />
Rectangle cRect = theCombo.Bounds;<br />
g.DrawRectangle(aPen, cRect.Left * scalex, cRect.Top * scaley, cRect.Width * scalex, cRect.Height * scaley);<br />
}<br />
<br />
}<br />
}<br />
|
|
|
|
|
It's quite simple. You have forgotten about initializing BackgroundImage and you get null reference error as long as "null" is a default value of BackgroundImage. Try doing this:
BackgroundImage = new Bitmap(Width, Height);
or sth like that, whatever.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Thanx mate. That seems to have worked. Now, while that seems to have worke fine I have fond yet another problem. My textbox details have gone missng or better yet itisnot moved into the print preview. Is my code missing something? The textboxes are all within a Group Box. My labels are also not printing which probably mean I have not declared them. Correct? f you could ave a look at why my text boxes are not comming through I would greatly appreciate it. Cheers
|
|
|
|
|
Well, I have a deep night here in Poland so I am in a "not responding" state. Maybe tomorow I will be able to help you.
PS. I have noticed following expression in your code:
Controls[i].GetType() == this.t1TextBox.GetType()
Why not use the is operator?
Controls[i] is TextBox
Good Night - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
gajatko wrote: deep night here in Poland so I am in a "not responding" state.
I'll have to remember that and put California in place for Poland if I use the same line
"Find it your bloody self - immediately!" - Dave Kreskowiak
|
|
|
|
|
Thanx guys but I am still having problems with this one. My Text boxes don't seem to print over. I think it may because they are sitting in the GROUPBOX. Any further Ideas?
|
|
|
|
|
Yes. You are enumarating Form.Controls collection, which doesn't contain controls which are in a groupbox. Controls in a groupbox are in the GroupBox.Controls collection.
About printing a form. Did you try to use the Form.DrawToBitmap method?
Bitmap b = new Bitmap(Width, Height);
this.DrawToBitmap(b, new Rectangle(0, 0, Width, Height));
b.Save("test.png");
b.Dispose();
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Thanx mate. I managed to get one of the groupboxs onto the form now. However groupBox1 doesn't seem to be comming over. I have managed to get some code for printing off the codeprojact site which caters for two groupboxes but it doesn't work in my project. Any other ideas?
|
|
|
|
|
i try to write code for create a barcode. i searched in this site for this and found some documents and samples, they show a table for numbers but i couldn't understand clearly. please help me about barcodesss.
signature...
|
|
|
|
|