|
I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too.
please help!!!!
|
|
|
|
|
Can you temporarily hide the main form?
What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.
|
|
|
|
|
Every form you create acts just like a class would so you could just do.
MySecondForm form = new MySecondForm();
form.Show();
this.Dispose();
The 'this' keyword refers to the object you're form is derived from including all of the custom objects
and functions you have put in. You could also just say 'Dispose();' instead of 'this.Dispose();' but
for keeping clarity it's better to use the this keyword.
You could also say 'this.Hide();' but then it keeps that form in memory which when you're dealing
with multiple forms this could eat up your memory pretty quickly. I just take my forms out of
memory when I don't need them.
------------------
I'm naked under my clothes...
|
|
|
|
|
this.Dispose(); doesn't work for me. it still closes the whole application. the this.Hide works and altought I'm making a small program a slow computer is gonna run it so if you can recommend me of somthimg else that will allow me to save in memory usage please find me somthing that doesn't just hides the window but thanks anyway.
Modyfied: I just realized I don't need to close the main form and that the this.Dispose works for my other forms. anyway thanks alot you've been a great help for me. see ya later
|
|
|
|
|
You can't dispose the main form because that's what the message pump (which Application.Run starts) waits for. If you dispose the main form, you effectively let the message pump know that it can quit and your entry point returns. When the entry point returns, the CLR unloads your application (if there are no running background threads).
See Charlie's response for a good solution (no need to repeat it).
Also, you might consider a wizard-like interface, even if it has no "Back" or "Previous" button. You load one Form which adds and removes instances of container classes (like the Panel ) onto its surface. This is how property sheets and wizards (which actually use the same interfaces and functions) work in Windows Explorer and other applications that use the shell-provided property sheet implementation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If you're using VS.NET, your application entry point probably looks like this:
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
This means that
1) this call will not return until Form1 has closed
2) the application will terminate immediately after Form1 is closed.
If you need to close Form1 have have the app keep plugging away, you can't rely on the VS.NET-generated code. Instead, you can write a class to handle showing the form(s) you need to show.
public class AppStarter{
public AppStarter(){
Form1 frm = new Form1();
frm.Show();
}
[STAThread]
public void Main(){
Application.Run(new AppStarter());
}
}
Now the application will run as long as the instance of AppStarter created in Main() is alive. You need to remember to call Application.Exit() when your last form closes or the app will never die, even if there's nothing on screen because the lifetime of the app is no longer directly related to the lifetime of the form.
Charlie
if(!curlies){ return; }
|
|
|
|
|
Hey all,
I'm pretty new to .NET, and want to know how to use someone else's managed C++ code from my C# app. The C++ code is already compiled to DLLs, but doesn't appear to have been put in a namespace.
All I want is to create an object of the class, and call a few void methods
Any help is much appreciated, G
|
|
|
|
|
Just reference it like you would if you were calling your c# code in a dll. Every .NET language compiles down to the same basic construct which is IL.
------------------
I'm naked under my clothes...
|
|
|
|
|
Before I go and write my own converter - is there one available in .NET Framework? Is there a function to which I can give 0.003 inch and it converts it to milimeters (I don't need the code for this function - I need to know if in the .NET Framework there exists one already)?
Tomaz
|
|
|
|
|
No. The System.Windows.Forms.SystemInformation encapsulates some of the GetDeviceCaps functionality, but not all - including things you'd need like LOGPIXELSX and LOGPIXELSY . It has some other useful things, though, if you want to check it out anyway.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, I was wondering if anyone had tried printing from a webservice without the use of forms. Im trying to create a webservice that when called from a remote browser, will print to the computers default printer. I have found many resources for printing but they all use forms. Any help/guidance is much apprieciated.
|
|
|
|
|
Is what you're printing documents that are send to the web service, or are you printing data passed to the web service? In the former case, you must invoke the application associated with that object. In many cases, the major programs support a "/print" switch or "print" verb. You could do something like the following:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "path to document";
info.UseShellExecute = true;
info.Verb = "print";
Process.Start(info); If you are printing data, you need a graphics handle in which to print. The .NET base class libraries allow you to instantiate a new PrintDocument and to attach handlers to its events like PrintPage , which pass a Graphics object in the PrintPageEventArgs . You can see the PrintDocument class documentation for an example. It is invoked from a Button on a form's Click event handler, but common sense would tell you that you don't need to invoke this from a control. Just instantiate the PrintDocument , attach your handlers, and do the painting on the PrintPageEventArgs.Graphics .
Note, it is likely that the username under which ASP.NET runs will require a Windows station, i.e. a Windows desktop session associated with it. Make sure that account under which ASP.NET runs ("ASPNET" by default) has the "Log in locally" privilege ("ASPNET" should by default).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks for your reply. I am trying to print data that is passed into the webservice. I belive I have set up the handler correctly but its still not working. The code works in a regular C# project just fine but not in my webservice. When I browse to the webservice I just get an error "This page cannot be displayed HTTP 500 - Internal server error". It seems as if PrintPageEventArgs object that is passed into the print handler needs some setting up first, but i'm not sure how to do this. Here is my code if anyone cares to point and laugh. Again thanks for any help.
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
PrintDocument pd = new PrintDocument();
[WebMethod]
public string printIt()
{
pd.PrinterSettings.PrinterName = @"\\printers\printer / HP";
pd.PrintPage += new PrintPageEventHandler(this.printItHandler);
PrintPageEventArgs ea;
if (pd.PrinterSettings.IsValid)
{
pd.Print();
return "printing worked";
}
return "Printing failed";
}
private void printItHandler(Object sender ,
PrintPageEventArgs e)
{
String textToPrint = "Printing is fun!!";
Font printFont = new Font("Courier New", 12);
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint, printFont,
Brushes.Black, leftMargin, topMargin);
}
}
|
|
|
|
|
HTTP status code 500 could be just about anything. That's like just saying, "There's an error". If this is an ASP.NET application, check the trace.axd "file" from your web application's root if logging is enabled from your web application's Web.config file. This should give more information.
I'm not even totally sure if this will work, although I'm fairly positive this will not work if the user doesn't have local login privileges since a Windows station (i.e., support for window and graphic handles) would have to be available (so far as docs read).
If you need more information about how to set up printing, then read the .NET Framework SDK documentation for the related classes, such as PrintDocument , PrinterSettings , etc. That's why it's there.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks for the reply. I'll definatly give the trace.axd a look. As far as login privaledges go, Im testing on the same machine as I am developing on so that should'nt be an issue. Also, everything I have so far IS from the SDK documentation and has not helped. If I wanted to use forms with button event listeners I am sure that info would be of great use. Like I said, this code works fine in a standard C# project. Thanks again.
|
|
|
|
|
You don't seem to get it. The ASP.NET worker process (aspnet_wp.exe) runs as a particular user, just as IIS runs as a particular user - and it isn't you unless you know how to properly edit the machine.config file (and judging by your comments, I doubt that). In IIS6 (on Windows Server 2003), the application pool runs as a particular user. Again, this isn't you. By default, ASP.NET 1.0 and 1.1 on IIS5 and 5.1 run as the ASPNET local account. Application pools in IIS6 run as NetworkService (NT AUTHORITY\NETWORK SERVICE) by default. These accounts must be granted privileges. It has nothing to do with you, even if you enable impersonation (which executes the thread - not process - under your credentials).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Your right. I dont get it. If I got it I probably would'nt be posting here! But thanks for you're help. I will look at that and see if I can figure it out. Thanks
|
|
|
|
|
No, what you don't seem to get is what I've explained three times in this thread: ASP.NET runs under a particular user's credentials - not yours. Credentials are a username and password. By default, the ASP.NET worker process runs under the local "ASPNET" account. This may not have all the necessary security privileges. You do, but ASP.NET isn't running as you (nor should it - it should be a dedicated, mostly unpriviledged account). The reason this works for you in Windows Forms and not for the ASP.NET application is because your account has a Windows station associated with it - hence all the windows and graphic UI you see. ASP.NET doesn't, unless you set your machine up correctly to do so. This has absolutely nothing to do with C# or .NET.
There are also examples out there. I simplied googled for "ASP.NET PrintDocument" and found several.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I have a problem in GDI+, I put a code snippet (draws a rectangle with a text inside) in the onpaint event handler of my simple form but when I resize the form, the handler redraws my rectangle? Is there a way to determine if there is a rectangle or some other thing on the form's drawing area to not to redraw the rectangle? How can I solve this problem? (my code is below...)
Thanks in advance...
Cem Louis
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GDI_Test01
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.form1_Paint);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void form1_Paint(object sender, PaintEventArgs e)
{
// Obtain the Graphics object
Graphics g = this.CreateGraphics();
int rect_width = 110; int rect_height = 20;
Rectangle rect = new Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;
// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect, strFormat1);
verdanaFont.Dispose();
g.Dispose();
}
}
}
|
|
|
|
|
A couple of things.
First, the problem is not that the rectangle is being drawn again when the form is resized, it's that the old one is not being erased.
To solve this use the Graphics object passed into the paint event handler (e.Graphics) instead of creating a Graphics object.
Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the line ResizeRedraw = true; to your form's constructor. You can see for yourself the difference this makes.
One last thing, since you're doing this painting in the form itself, you can override the OnPaint method rather than handling the paint event.
Charlie
if(!curlies){ return; }
|
|
|
|
|
Hi Charlie,
Thank you for your reply, I didn't know the ResizeRedraw property before.
Regards,
Cem Louis
|
|
|
|
|
Hi.
Can someone show me how to fill 50% of the below drawing with a solid color.
From buttom to top.
Thanks
Regards r9
The drawing:
private System.Drawing.Graphics graphicsObj = null;
private System.Drawing.Pen penBlackWidth1 = new System.Drawing.Pen(System.Drawing.Color.Black,1);
private Point[] curvePoints = null;
private SolidBrush lightGrayBrush = new SolidBrush(Color.LightGray);
private Point point1;
private Point point2;
private Point point3;
private Point point4;
private SolidBrush blackBrush = new SolidBrush(Color.Black);
...................................................
try
{
graphicsObj = e.Graphics;
graphicsObj.DrawLine(penBlackWidth1, 45, 20, 175, 20);
graphicsObj.DrawLine(penBlackWidth1, 45, 20, 45, 120);
graphicsObj.DrawLine(penBlackWidth1, 175, 20, 175, 120);
graphicsObj.DrawLine(penBlackWidth1, 45,120,90,160);
graphicsObj.DrawLine(penBlackWidth1, 175, 120, 135, 160);
graphicsObj.DrawLine(penBlackWidth1, 90, 160, 135, 160);
graphicsObj.DrawLine(penBlackWidth1, 465, 335, 490, 335);
graphicsObj.DrawLine(penBlackWidth1, 580, 335, 605, 335);
graphicsObj.DrawLine(penBlackWidth1, 465, 335, 490, 335);
graphicsObj.DrawLine(penBlackWidth1, 580, 335, 605, 335);
graphicsObj.DrawLine(penBlackWidth1, 473, 345, 483, 345);
graphicsObj.DrawLine(penBlackWidth1, 473, 345, 478, 335);
graphicsObj.DrawLine(penBlackWidth1, 483, 345, 478, 335);
graphicsObj.DrawLine(penBlackWidth1, 587, 345, 597, 345);
graphicsObj.DrawLine(penBlackWidth1, 587, 345, 592, 335);
graphicsObj.DrawLine(penBlackWidth1, 597, 345, 592, 335);
point1 = new Point(592,335);
point2 = new Point(597,345);
point3 = new Point(587,345);
point4 = new Point(592,335);
curvePoints = new Point[]
{
point1,point2, point3, point4
};
graphicsObj.FillPolygon(blackBrush, curvePoints, FillMode.Alternate);
point1 = new Point(478,335);
point2 = new Point(483,345);
point3 = new Point(473,345);
point4 = new Point(478,335);
curvePoints = new Point[]
{
point1,point2, point3, point4
};
graphicsObj.FillPolygon(blackBrush, curvePoints, FillMode.Alternate);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Here's a though - look in your mathematics book. If you want exactly 50%, then find the algorithm given specified polygram. This is a forum for C#, not math.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath Stewart
The algorithm is not the problem.
But how do I fill the figure?
(the C# GDI+ code is my problem)
Regards
C#
|
|
|
|
|
Looking at the Graphics class, the logical choices are FillPolygon , FillPath , or FillRegion . Full class documentation - it's a great thing to read.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|