|
The way I think about it is like this:
We create normal classes (with constructor) because we might want to have multiple instances of that class at the same time. There is no need to create multiple instances of the MessageBox class because the only thing you use a MessageBox is to show it to the user and wait for a input. The MessageBox is a modal dialog and hence the application will have to sit and wait till user clicks a button. Once the user clicks the button the only information you will need is which button the user clicked (if there is more than one button). So the entire purpose for which MessageBox class exists is finished when we call the Show method and check its return value. Hence the natural way is to make it a static class so that its very easy for use (means write less code). User never needs to create an instance.
---
"Drawing on my superior command of language I said nothing."
|
|
|
|
|
Blekk wrote: And yeah, I realised you cannot use MessageBox as a normal class, using new etc.
That's because MessageBox
a) doesn't have any constructors
b) the Show method is declared static , which means that you don't need an instance of the class and call it directly like Class.Method
hope this helps,
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks Pradeep C and Greeeg, I now understand why you don't have to create an instance and you have both also made the whole concept of the C# language a little more clear.
There are still many things I don't really know at this stage, but probably should know, meh, I would rather find it out this way than reading through endless chapters of a book and then forgetting what it was at the end of the book.
Thanks.
|
|
|
|
|
|
DialogResult result = MessageBox.Show("do something?", "title here", MessageBoxButtons.YesNo);
if(result == DialogResult.Yes)
{
}
else
{
}
|
|
|
|
|
Hi Blekk.
Exists two way for using.
1)
if( MessageBox.Show(Handle, msg, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question) == DialogResult.Retry)<br />
{<br />
}
2)
DialogResult p = MessageBox.Show(Handle, msg, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);<br />
if( p == DialogResult.Retry )<br />
{<br />
}
SINCERELY.
ANTHONY ACUÑA
PREFERED PHRASE:
SOMEBODY TELL ME WHY IS MORE REAL WHEN I DREAM THAT I AM WAKE?
|
|
|
|
|
Thankyou also, I will use both methods and see which I find the easiest to understand etc.
Thanks for all the help.
|
|
|
|
|
Thanks all for your help, I've never known anybody respond so quickly on any forum ever, with so much useful information too.
Thanks all for your help.
|
|
|
|
|
These two programs are giving errors while compiling.
WAP that counts the no. of occurence of particular character in a line of text.
class demo
{
private int Occurance(string Input, string Find)
{
System.Text.RegularExpressions.Regex rx =
new System.Text.RegularExpressions.Regex(Find);
// to display them do this
System.Text.RegularExpressions.Match mt = rx.Match(Input);
while (mt.Success)
{
System.Console.WriteLine("Found >>" + Find + "<< at position " + mt.Index.ToString());
mt = mt.NextMatch();
}
// end display
return rx.Matches(Input).Count;
}
}
This program is giving error that entry point is not defined.
-------------------------------
WAP to take 2 dates using objects(day,month,year)& display both dates,their comparison also that which one is greater(take the dates with the help of three parameter constructors)
class demo
{
private DateTime getDate(int day, int month, int year)
{
return new DateTime(year, month, day);
}
private void compareDates(DateTime a, DateTime b)
{
if (a > b)
Console.WriteLine(a.ToShortDateString() + " > " + b.ToShortDateString());
else if (a == b)
Console.WriteLine(a.ToShortDateString() + " = " + b.ToShortDateString());
else
Console.WriteLine(a.ToShortDateString() + " < " + b.ToShortDateString());
}
}
this program is giving error "type or namespace couldnot be found(are u missing a using directive or an assembly reference).
|
|
|
|
|
1. You haven't defined an entry point. Where's your Main()?
2. Are you creating a dll?
BTW: Make your title more descriptive in the future.
|
|
|
|
|
Remember that the sintax is:
using System;<br />
<br />
namespace demo<br />
{<br />
public class demo<br />
{<br />
public demo()<br />
{<br />
}<br />
<br />
public Main()<br />
{<br />
this.demo();<br />
}<br />
<br />
private int Occurance(string Input, string Find) <br />
{ <br />
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(Find);
System.Text.RegularExpressions.Match mt = rx.Match(Input); <br />
while (mt.Success) <br />
{ <br />
System.Console.WriteLine("Found >>" + Find + "<< at position " + mt.Index.ToString()); <br />
mt = mt.NextMatch(); <br />
}<br />
<br />
return rx.Matches(Input).Count; <br />
} <br />
<br />
private DateTime getDate(int day, int month, int year) <br />
{ <br />
return new DateTime(year, month, day); <br />
} <br />
<br />
private void compareDates(DateTime a, DateTime b) <br />
{ <br />
if (a > b) <br />
{<br />
Console.WriteLine(a.ToShortDateString() + " > " + b.ToShortDateString()); <br />
}<br />
else if (a == b) <br />
{<br />
Console.WriteLine(a.ToShortDateString() + " = " + b.ToShortDateString()); <br />
}<br />
else <br />
{<br />
Console.WriteLine(a.ToShortDateString() + " < " + b.ToShortDateString()); <br />
}<br />
} <br />
<br />
~demo()<br />
{<br />
}<br />
<br />
}<br />
}
SINCERELY.
ANTHONY ACUÑA
PREFERED PHRASE:
SOMEBODY TELL ME WHY IS MORE REAL WHEN I DREAM THAT I AM WAKE?
|
|
|
|
|
I hope I'm just missing something basic.
Is it possible to have a custom background (say, gradient) for a System.Windows.Forms.Button without having to do *ALL* the drawing yourself.
OnPaintBackground does not appear to be called; the paint code for Button appears to be integrated with it's background logic.
|
|
|
|
|
I think you should be able to do this without much problem by creating a csutom control which derives from Button class.
You can override the OnPaint method and call base.OnPaint() so that it would do all the normal painting required for creating a Button and the you could draw whatever you want, like gradient or ...
---
"Drawing on my superior command of language I said nothing."
|
|
|
|
|
That solution will not let you paint the background and ignore the foreground. The need is to have a custom background, but let .NET draw the foreground image and text. The calculations of *where* the image and text appear is fairly convoluted due to the ImageAlign, TextAlign, and TextImageRelation properties.
I have pretty much confirmed this is not possible as .NET draws the button background and image/text in one operation, giving derived classes no opportunity to inject in the process.
Thank you for the answer.
|
|
|
|
|
Can't you set a picture as the background of a button? and have another picture for the foreground, and text and all that other stuff?
Or is it I that is confused?
|
|
|
|
|
Hello,
I'm using an Access file as a data source but am having problems when it comes to saving to the "database." If I do not "copy the file into the project" it works fine but if I copy it into the project it won't save. I need to get it into the project.
Also, for this project I'm having a little bit of a problem with the SELECT query:
I'm taking a C# entry level course and the book we're using gives this example:
SELECT Bla, BLa, Bla.... FROM dbo.Addresses WHERE LastName = @lastName
I'm trying to do a query exactly like this but it gives me an error:
Error in WHERE clause near '@'. Unable to parse query text.
Using this query:
SELECT PartNumber, PartName, Description, Price, Quantity FROM Invoices WHERE PartNumber = @partNumber
If I ignore the error, the @partNumber parameter doesn't show up in the Adapter window.
Thanks
|
|
|
|
|
I don't understand what you are try to do with the Access file. However, on the web server, you must give ASPNET write accesss in order for ASPNET to INSERT or UPDATE the Accesss file.
The data provider for Access does not support named parameters such as "@lastName". You have to use unnamed parameters using "?":
SELECT * FROM Addresses WHERE LastName = ?
|
|
|
|
|
Is it possible to edit the properties for a file such as the ones that can me found on the summary tab. i.e. author, title, comments, keywords, etc...
thanks.
|
|
|
|
|
Ok, I was just debugging my little web browser app I was creating and before Form1 even loads, I get the litte yellow highlight over this text:
Application.Run(new Form1());
With this message:
"Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead."
This is in Program.cs, I am new to C# and so do not really understand what the error is, I haven't changed anything in Program.cs atall, just the regular Form.cs
Can somebody please tell me what the hell it is trying to tell me?
Thanks,
Any reply is appreciated.
|
|
|
|
|
Is your Form1 attempting to show another form somewhere using Application.Run() ??
|
|
|
|
|
Not atall, Form1 just contains a textbox and a menustrip so far with a File>New that creates a new instance of a class called File in Text.cs
|
|
|
|
|
Hi,
In my C# Win form (.NET 2.0 - VS 2005), I have 4 datagridviews which are filled by 5 different data tables.
these data tables are directly filled by 5 different SQL Server Everywhere Tables.
Now, Placed all Table Saving Codes within a single method event handler for the save button.
Like this,
private void saveAll()<br />
{<br />
this.Validate();<br />
this.myBindingSource1.EndEdit();<br />
this.myDataSet.MytableAdapter1.Update(.....);<br />
<br />
this.validate();<br />
this.myBindingSource2.EndEdit();<br />
this.myDataSet.MytableAdapter2.Update(.....);<br />
<br />
this.validate();<br />
this.myBindingSource3.EndEdit();<br />
this.myDataSet.MytableAdapter3.Update(.....);<br />
}
Now, When I call the save method, most of the time, I get Error with the message "Concurrency Violation."
Is there any way I can get rid of this problem ?
Thanks in advance,
Emran
|
|
|
|
|
Are any constraints on database children?
|
|
|
|
|
Hi,
Thanks for your reply.
No, My tables are independent, they are not related to any other table as Parent Child and thus there is no question of constraint. Is there any other possibilities for getting rid of this error ?
Regards
Emran
|
|
|
|
|
Your problem is more than likely related to your update commands or identity column
|
|
|
|