|
You can do this inside the form itself, or using another object which has subscribed to the forms event.
1st within the form:
using System.Windows.Forms;
using System.ComponentModel;
class MyForm : Form
{
private bool stopUserFromClosingForm = true;
public MyForm()
{
}
protected override void OnClosing(CancelEventArgs e)
{
if(stopUserFromClosingForm)
{
e.Cancel = true;
}
}
}
or if another object has a reference to the form
static void Run()
{
Form myForm = new Form()
myForm.Closing += new CancelEventHandler(this.HandleFormClose);
myForm.Show()
}
private void HandleFormClose(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
There is also a Closed event, which is juts a normal EventHandler - it has no Cancel property.
|
|
|
|
|
Thanks for replying, but I am using ASP.NET, which means Web Forms instead of just a Window Form, do you have an example for that?
|
|
|
|
|
Hi,
I'm not very familiar yet with going between forms and classes, and would love any help on this.
I have a main form which contains a class called Tabs . When the Search button is clicked, a new form opens up (this class is called called Searching ) which asks for some info. I'd like to take that info back with me when I close the form containing Searching without having to save it in an xml file.
So, I know I need to declare a public string which I'll call searchThis . Do I do this on the Tabs form, the Searching form, or both? (I tried a few things that didn't work, including declaring public string searchThis on the Tabs form, then public Tabs searchThis on the Searching form.) Next, I need to return a value for searchThis before closing my Searching form, then access it between showdialog and dispose on my Tabs form. What I have so far is:
(under Tabs)
public string searchThis;<br />
private void searchClick(object sender, System.EventArgs e)<br />
{<br />
Searching s = new Searching();<br />
s.ShowDialog();<br />
MessageBox.Show(searchThis);<br />
s.Dispose();<br />
}
(under Searching)
public Tabs searchThis;<br />
private void goClick(object sender, System.EventArgs e)<br />
{<br />
string searchThis = "";<br />
searchThis = "cust";<br />
this.Close();<br />
}
When I run it this way, the MessageBox comes up empty, so I must be having trouble saving searchThis's value.
Anyway, thanks so much for your help!
Mel
|
|
|
|
|
You were on the right track. This should work.
<br />
(under Tabs)<br />
private void searchClick(object sender, System.EventArgs e)<br />
{<br />
Searching s = new Searching();<br />
s.ShowDialog();<br />
MessageBox.Show(s.searchThis);<br />
s.Dispose();<br />
}<br />
|
|
|
|
|
The problem appears to be in your goClick, where you redefine searchThis as a local variable.
Here's more or less how I would do code it:
(Under Tabs)
private void searchClick(object sender, System.EventArgs e)
{
using (Searching s = new Searching())
{
if (s.ShowDialog(this) == DialogResult.OK)
MessageBox.Show(s.SearchThis);
}
}
(Under Searching)
public string SearchThis;
private void goClick(object sender, System.EventArgs e)
{
SearchThis = "cust";
DialogResult = DialogResult.OK;
Close();
}
Regards,
Alvaro
I cannot take anything the Bush administration does seriously. The corruption, the cynical disregard for humanity, the cronyism and incompetence, all wrapped in a slimey flag of ultra-marketed nationalism repulses me. -- consdubya from fark.com.
|
|
|
|
|
When I drag the width of the leftmost column of a listview to 0 pixels and begin expanding it back out again, if hte lv is drawn with gridlines except for the rows that are selected the line seperating the leftmost column from the one next to it will not be drawn unless the collumn is subsequently narrowed or something else forces a repaint. Is there any way to work around this and force a repaint?
|
|
|
|
|
Hi
I am trying to draw a rectangle over an existing image by using the following code (pb is the PictureBox and has an image)
Graphics oGraphics = Graphics.FromImage(pb.Image);<br />
oGraphics.DrawRectangle(new Pen(Color.Red, 2), new Rectangle(5, 5, 300, 300));<br />
pb.Refresh();
The problem comes when I want to draw another rectangle (at a different location) and I can't remove/clear the existing rectangle before drawing the new one.
Is there any way I can remove the existing drawn rectangle?
I don't want to use the pb.CreateGraphics() b/c the drawing is not consistent when this method is used.
I am using the FromImage() method b/c later I want to resize (zoom) or rotate the image with drawn rectangle and It won't require any further coding for rotating and resizing the rectangle.
I tried reload the Image again, but large images take to much time to load, so I want to avoid the Image reloading.
Please help!
|
|
|
|
|
Hi!
I'm afraid you can't do this. You'll have to store the base image somewhere, duplicate it and then perform all the painting you want to be seen one after the other. That way you don't modify the original image and you can draw the original version as often as you like.
In order to remove the rectangle you've drawn previously you'd have to remember all the pixel values for each pixel your rectangle is drawn over and then restore them one by one.
I strongly doubt that this is working efficiently, especially when you have more forms than just a rectangle.
Regards,
mav
|
|
|
|
|
If you modifiy an image like you do it in your example there is no way going back to a previous state. One solution would be to clone the whole image before altering it and going back to the original one when needed.
Another possibility would be to catch the Paint event of the PictureBox and paint the 'current' rectangle in the handler. It would probably even be a good idea to dump the PictureBox and draw image and rectangle yourself.
|
|
|
|
|
You have at least 2 solutions:
1. Create a class that extends PictureBox (or Control if you get adventurous) and override OnPaint().
2. Keep a copy of the image in memory.
I think the 1st option is the better choice because it opens the possibility for code-reuse and uses less memory.
Example:
public class PictureSelectionBox : PictureBox
{
private Rectangle selectionRect;
private Pen selectionPen;
public Rectangle CurrentSelection
{
get { return this.selectionRect; }
set
{
this.selectionRect = value;
this.Refresh();
}
}
public Pen SelectionPen
{
get { return this.selectionPen; }
set
{
this.selectionPen = value;
this.Refresh();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.selectionPen != null &&
this.selectionRect.Width != 0 &&
this.selectionRect.Height != 0)
{
e.Graphics.DrawRectangle(this.selectionPen, this.selectionRect);
}
}
public void CommitSelection()
{
Graphics g = Graphics.FromImage(base.Image);
g.DrawRectangle(this.selectionPen, this.selectionRect);
g.Dispose();
this.CurrentSelection = new Rectangle(0, 0, 0, 0);
this.Refresh();
}
}
You could even override the OnMouseDown, OnMouseMove, and OnMouseUp events in the derived class to allow the user to make his/her selection. You better watch out with the use of Refresh() doing this though because you'll get some ugly flickering.
I'd either [DllImport] InvalidateRect() or something like:
Graphics g = this.CreateGraphics();
OnPaint(new PaintEventArgs(g, this.ClientRectangle));
g.Dispose();
Enjoy,
David
|
|
|
|
|
Thank You David,
Let me check it out!
|
|
|
|
|
I have 2 DateTime Vars.
DateTime dtFrom = '1/1/2005';
DateTime dtTo = '1/24/2005';
How can I select a period of: Week, 2 Weeks, Month...
And Divide the date range into it to something like this:
Example: 1 week:
1 2 3 4 5
======== ======== ========= ========= =========
1/1/2005 1/2/2005 1/9/2005 1/16/2005 1/23/2005
1/1/2005 1/8/2005 1/15/2005 1/22/2005 1/24/2005
======== ======== ========= ========= =========
|
|
|
|
|
First Row Differences:
1-1-05, 1-2-05 = 1 Day
1-2-05, 1-9-05 = 7 Days
1-9-05, 1-16-05 = 7 Days
1-16-05, 1-23-05 = 7 Days
Second Row Differences:
1-1-05, 1-8-05 = 7 Days
1-8-05, 1-15-05 = 7 Days
1-15-05, 1-22-05 = 7 Days
1-22-05, 1-24-05 = 2 Days
I don't see any patterns in the differences here, so what exactly do you want again?
What do you mean by "divide the date range into it" ?
What do you want to do?
Are looking for every Sunday/Saturday in the month?
The work-week days?
Display a calender to the screen?
David
|
|
|
|
|
you were reading this wrong.
1/1/2005 - 1/1/2005 - 1 (only one day cos' it is last day of a week)
1/2/2005 - 1/8/2005 - 2 (full second week)
...and so on...
got it?
|
|
|
|
|
Ok, well you could try something like:
DateTime dtStart = new DateTime(2005, 1, 1);
DateTime dtEnd = new DateTime(2005, 1, 30);
DateTime dtCurrent = dtStart;
while (dtCurrent < dtEnd)
{
double dDays;
if (dtCurrent.DayOfWeek != DayOfWeek.Sunday)
dDays = 6.0 - (double)dtCurrent.DayOfWeek;
else if ((dtCurrent.Day + 6) <= dtEnd.Day)
dDays = 6.0;
else
dDays = dtEnd.Day - dtCurrent.Day;
Console.Write(dtCurrent + " - ");
DateTime dtEndOfWeek = dtCurrent.AddDays(dDays);
Console.WriteLine(dtEndOfWeek);
dtCurrent = dtEndOfWeek.AddDays(1.0);
}
David
|
|
|
|
|
i need to understand what kind of object is just one..
i have myOwn it could be of Car or Bike.
How can i understand it?
i thought about:
if( typedef(myOwn) == Car)
but typedef doesn't exist in c# 1.1
I don't want to use try catch,
tnx
|
|
|
|
|
your best bet is to use typeof()
if(typeof(myOwn) == Car)
DoSomethingWithCar(myOwn as Car)
else
DoSomethingWithBike(myOwn as Bike)
The reason why a poor man will always be poor and the rich man will be rich, is because the poor always maximizes his expenditures and the rich man always maximizes his potential. --T.Parker
|
|
|
|
|
if(myOwn is Car)<br />
{<br />
}
or
if(myOwn.GetType() == typeof(Car))<br />
{<br />
}
They are the same in essence.
|
|
|
|
|
Just to add to J4amieC 's suggestion, if you need to actually use the methods of your specific class, you could use the as keyword to reduce the number of casts that you'll use. For example:
Car c = someObject as Car;
if( c != null )
{
}
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
There's a small difference between using is and as .
If you use as and then check for null you don't know if someObject actually is a Bike or if it's a Car with value null .
With is you can really test for the correct type.
Usually this won't matter, but it's a different meaning nevertheless...
Regards,
mav
|
|
|
|
|
Well the idea is:
Car car = someObj as Car;
Boat boat = someObj as Boat;
Plane plane = someObj as Plane;
if (car != null) {
}
else if (boat != null) {
}
else if (plane != null) {
}
The assumption is that your testing classes on the same level in the class hierarchy.
If your not testing classes on the same level, then the difference between is and as matters. But you could also write your if-else statement so that it tests the more specific class types to the more broad class types.... lol
Anyways,
David
|
|
|
|
|
I agree with you about most of that. If I use typeof on a null value, then I still not get what I need in terms of runtime type information. Here, a use of as prevents an extra cast later on. For example, in the following code, we pass null values and with both is and as , we cannot retrieve runtime type information:
public class Foo {}
public class Goo {}
public class Test
{
[STAThread]
static void Main( string[] args )
{
Foo f = null;
Goo g = null;
Foo ff = new Foo();
Goo gg = new Goo();
Console.WriteLine( Form1.TestMeAsFoo( f ) );
Console.WriteLine( Form1.TestMeAsFoo( g ) );
Console.WriteLine( Form1.TestMeAsFoo( ff ) );
Console.WriteLine( Form1.TestMeAsFoo( gg ) );
Console.WriteLine( Form1.TestMeIsFoo( f ) );
Console.WriteLine( Form1.TestMeIsFoo( g ) );
Console.WriteLine( Form1.TestMeIsFoo( ff ) );
Console.WriteLine( Form1.TestMeIsFoo( gg ) );
}
public static bool TestMeIsFoo( object o )
{
return ( o is Foo );
}
public static bool TestMeAsFoo( object o )
{
return ( o as Foo ) != null;
}
}
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
-- modified at 15:24 Monday 5th December, 2005
|
|
|
|
|
Hello all, I am currently taking a C# programming class and have no prior knowledge of any programming. I am to write a program that takes a user's input and encrypt it, only using the ASCII code table (32-126). Now I know that there is a way of doing without re-inventing the wheel to say. I just can not find it. I think I need to use a character array, but just can not figure it out. Also I know that there is something in the System.Security that does this, but can not find out any information. Please help. Thanks
Rich Poulin
|
|
|
|
|
|
Hi,
I am making an application in VSC# 2005 which has a "WebBrowser" control in it, I want to be able to manipulate a webpage in the webrowser control from within the application.
I have worked out how to get (and set) the text in a form on the webpage with something like;
webBrowser1.Document.GetElementById("Textbox1").InnerText.ToString();
which is fine, but I just can't work out how to invoke a button on the webpage. Basically I want to get the text from the form, change it, paste it back into the form (all of which I can do) and then call the "Save" button to save my changes (which I can't do). Any help would be greatly appreciated!
Thanks in advance, Martin.
|
|
|
|