|
i did some coding for message boxes but the result was some errors which i had no clue of what it was trying to say...can anyone teach me whats the correct way to code a message box and do i need to declare it like this
"private System.Windows.Forms.Message;" <-- i add this and i get one error
error = Invalid token ';'in class, struct or interface member declaration.
i dont add it SIX more errors come up...help me guys...
Arvinder Gill
|
|
|
|
|
I don't know if I totally understood what you want to do...
if you just want to display a message box do it this way:
At the top of your source file:
using System.Windows.Forms;
To display a message box:
MessageBox.Show("Some text for the message box", "Caption text",
MessageBoxButtons.OK, MessageBoxIcon.Error); Check the MSDN for MessageBox.Show() to see all overloads and possible parameters...
I hope I could help you...
Regards, mYkel
|
|
|
|
|
Here is an exampel:
System.Windows.Forms.MessageBox.Show("Here you write the message inside the box","Here you write the header of the messagebox ", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
This will create a messagebox, with one OK button(MessageBoxButtons.OK), and a picture of an exclamation (MessageBoxIcon.Exclamation)
You should not declare the messagebox as a private item(I'm not even shore if it's posible)
|
|
|
|
|
using System.Windows.Forms;
................
MessageBox.Show("Your error!");
|
|
|
|
|
All the answers above are correct - use the MessageBox class. The Message class encapsulates a Windows Message that is recieved through a window procedure or application/system hook and is completely different. Also, you didn't even specify a member name in your statement - just a Type with an access modifier, i.e. you didn't specify WHAT you were declaring.
At any rate, you shouldn't (can't without reflection anyway) hold a reference to a MessageBox (only contains statics) and that's probably what you want anyway judging by your subject line.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanx for the solutions but.....i tried all three solutions....i dunno why, but i still get the same errors...tried searchin around but to no avail...couldnt find one solution at all...well this is my last resort...i hope someone can point out whats wrong whit this code ..:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace CLINICINFORMATIONSYSTEM
{
/// Summary description for Password.
public class frmPassword : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.Label lblUserName;
private System.Windows.Forms.Label lblPassword;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.ComponentModel.Container components = null;
public frmPassword()
{
InitializeComponent();
SqlConnection myConnection = new SqlConnection("user
id=username;" +
"password=password;server=serverurl;" +
"Trusted_Connection=yes;" +
"database=database; " +
"connection timeout=30");
try
{
myConnection.Open();
}
catch(Exception e) <problem area, error 1>
{
MessageBox.Show("Unable to connect to server. Please try again" , "Reminder" , MessageBoxButtons.RetryCancel , MessageBoxIcon.Warning); <problem area, error 4 and 5>
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
if(txtPassword.Text > 10) <problem area, error 3>
{
MessageBox.Show("The password should not be more than TEN characters. Please check your password again", "Reminder" ,MessageBoxButtons.OK,MessageBoxIcon.Warning); <---the problem area again! <problem area, error 2>
}
else.............
..............................................................................
with this i get these errors :
1 . the value 'e' is declared but never used
2 . The best overloaded method match or'System.Windows.Forms.MessageBox.Show string, string, System.Windows.Forms.MessageBoxButtons)' has some invalid arguments
3. Operator '>' cannot be applied to operands of type 'string' and 'int'
4. Argument '3': cannot convert from 'System.Windows.Forms.MessageBoxIcon' to 'System.Windows.Forms.MessageBoxButtons'
5. Argument '2': cannot convert from 'System.Windows.Forms.MessageBoxButtons' to 'string'
well im really hoping someone could point out to me whats realy wrong with this code...im nearing my deadline but this problem has delayed me by a DAY!!
Thanx a million
Arvinder Gill
|
|
|
|
|
This isn't a difficult problem if you'd only read the .NET Framework SDK documentation. Type MessageBox.Show in the help index and you'll see all the overloads. Most of your method calls are all wrong. The majority of your errors are because you're using the wrong parameters in the wrong order for MessageBox.Show - this is all documented in the .NET Framework SDK. "Error" 1 isn't even an error - it's a warning. If you're not going to use the exception information, then you don't need to declare an Exception variable. You can do either of the following:
try
{
}
catch (Exception)
{
}
try
{
}
catch
{
} You must read the documentation. Fumbling around blind won't help. Even looking at the documentation displayed in IntelliSense (the drop-down menu that appears when you're coding statements) would tell you what parameters should go where.
Also, in one line you're trying to use the > operator with a string! This doesn't work. If you want to check the length, use txtPassword.Text.Length > 10 .
In many of your incorrectly called MessageBox.Show calls, you call it using specific buttons but you never compare the return with a DialogResult ! What's the point? If all you're doing is displaying an error to a user, you shouldn't even define which buttons to display because "OK" is the default button. If you actually get the return value and compare it against a DialogResult enumeration, that's a different subject:
DialogResult result = MessageBox.Show("Would you like to retry?", "Prompt",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everybody.
Does anybody know how to set the zoom factor of a PrintPreviewDialog before
loading the form???????
I found this example:
// Set the zoom to 50 percent.
this.printPreviewDialog1.Zoom = 0.50;
But C# doesn't know this property...
Thanks Jeff.
|
|
|
|
|
It's not that C# doesn't "know" this property, it's that it does not exist. C# is just one of many languages that target the CLR and uses the same assemblies in the same way as any other language can (though some language features - not class members - are not supported by all languages).
In this example you saw, either the author is wrong (like I said, the Zoom property doesn't exist) or he has created his own PrintPreviewDialog class, or he is using the PrintPreviewControl and either named it differently or hosted it in a dialog and is encapsulating the PrintPreviewControl.Zoom property. You could do the same thing.
The PrintPreviewControl does have a Zoom property but you must host this control in your own Form . One final way is to enumerate the PrintPreviewDialog.Controls property and find the PrintPreviewControl that it uses internally and then set the Zoom property on that, like so:
foreach (Control c in this.printPreviewDialog1.Controls)
{
if (c is PrintPreviewControl)
{
((PrintPreviewControl)c).Zoom = 2.0f;
}
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you for the solution.
I tried it already. It works. The papersize is shwon in 75 percent, when I use this code:
<br />
foreach (Control c in this.printPreviewDialog1.Controls)<br />
{<br />
if (c is PrintPreviewControl)<br />
{ <br />
((PrintPreviewControl)c).Zoom = 0.75;<br />
}<br />
}<br />
But there is one more problem:
In the menu is the menuitem "automatic" still ckecked and not correctly the menuitem "75%".
Did you know any trick so set the correct menuitem (75%)?
Regards, Jeff
|
|
|
|
|
This incorrect behavior doesn't surprise me. You are, after all, accessing this control in a way that wasn't intended (otherwise it won't be exposed as a property of PrintPreviewDialog , or at least some of its properties like Zoom would). Unfortunately, there doesn't appear to be an easy way to correct this problem. When the Zoom property is changed, and event isn't even fired so you'd have to keep the values in sync yourself!
The only way I can think of is to get the ComboBox using the same enumerative code I gave you for the control, and update it when you programmatically change the Zoom . You can use ildasm.exe (the IL disassembler) that comes with the .NET Framework SDK to figure out in what Controls collection the ComboBox is added. If you can't read IL (and it wouldn't hurt to learn - it definitely can help you understand how to write more efficient code at the very least), you can use a good decompiler like .NET Reflector[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I thought already at this.
But a lot of work for that purpose.
After all, thanks for the trouble...
|
|
|
|
|
Hi guys!
I want to (de)serialize a SortedList that contains a couple of "complex" objects.
Each of those objects in the list contains two attributes that are references
to another objects (they are also stored and (de)serialized in a SortedList).
Here's the problem: All references work fine until I serialize and deserialize
the data. I compared the deserialized reference with the object it should link
to and they are not the same. Before deserialization they are the same.
It seems that not the references are serialized but copies of the objects where
the references point to. That perhaps makes sense to me cause references are
similar to adresses. And adresses surely can change while (de)serialization.
So I guess after deserialization I have two separate (but equal) objects
and not one object and a reference to it.
Is it anyhow possible to (de)serialize object references correctly?
Many thanks in advance!
Regards, mYkel
|
|
|
|
|
You are correct - references are address, but in the case of the .NET Framework it manages these objects in memory. Since memory address shouldn't be serialized (because the .NET Framework can move these around at any time, which is why the fixed statement is needed to work with unsafe memory addresses in C#), you need a serialization manager that can fix-up these objects. With .NET Remoting, the remoting infrastructure does this automatically when marshaling data types across AppDomain s. Many programs use serialization to save state and exist, restoring that state when the program restarts so such hooks-up aren't needed.
You should read through the Serializing Objects[^] section in the .NET Framework if you haven't already. It should cover a few of these things.
It's also possible that some things in your list aren't serializable (to be serializable, objects must be attributed with the SerializableAttribute and can optionally implement ISerializable to control serialization). Some properties of the list (or any other object) might also be non-serializable. What exactly differs between the two lists?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You wrote:
Some properties of the list (or any other object) might also be non-serializable. What exactly differs between the two lists?
The structure of the lists looks like that:
ClassA (objects are stored in SortedListA)
| |
| |----> reference to object of ClassB (objects are stored in SortedListB)
|
|--------> reference to object of ClassC (objects are stored in SortedListC)
All classes contain only simple data types like strings, uints, floats, etc. Furthermore ClassA contains, as you can see in my amazing ASCII-diagram , a reference to an object of ClassB and a reference to an object of ClassC. Nothing more. So I think everything should be serializable.
As I said in my first post, everything works fine until deserialization. Then the references seem to be independent objects. I can't manipulate the referenced objects through the reference. So perhaps the framework notices that I want to serialize a reference and since this is not possible cause addresses are shifting, the framework serializes just a copy of the referenced object.
But because of these limitations I would prefer at least a warning while compiling or an exception at runtime...
Regards, mYkel
|
|
|
|
|
Which serialization are you using? The System.Runtime.Serialization namespace elements or the System.Xml.Serialization namespace elements? The former does serialize references (as I mentioned before) but requires something to associate them with the original object. Since you don't have such a serialization manager, a copy of the class is deserialized, yes. There is not way to get an existing reference back since memory address shouldn't be stored (isn't durable), only to associate the data with existing objects. If you don't have something that will do that, all that can be returned is a copy of the class.
Again, read the link I sent you for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How can i specify how many decimal places is displayed when i parse a double to a string??
Sorry, i'm still a beginner with c#!
|
|
|
|
|
Fairly simple:
double d = 2.12165464578784545;
string s = d.ToString("#.##");
Tomas Rampas
------------------------------
Systems analyst, MCSD
rampas(at)gedas(dot)cz
http://www.gedas.com/
------------------------------
|
|
|
|
|
If you want to maintain a precision of 2, use "#.00" instead of "#.##" so that 0 will be used when no number exits in that position.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Is there a way to change a row's color in a datagrid? I've got a datagrid where i would like to change every 3rd row's color. Is this possible???
|
|
|
|
|
I have a article about How to change a back color of one cell based on its value.
http://www.codeproject.com/csharp/custom_datagridcolumnstyl.asp[^]
There are sone other articles in this site which I think about changing the color of entire row, use the serach engine at top of this page.
Mazy
"Art is life, life is life, but to lead life artistically is the art of life." - Peter Altenberg
|
|
|
|
|
Thnxs for your answer, I got it to change every 3rd row. But now there is a different problem, the instant i scroll down on the datagrid the colors get messed up or changes to one color. Do you know what this problem could be?
|
|
|
|
|
Don't worry, i got the problem, it was pretty stupid. Thnxs for the help.
|
|
|
|
|
I have a datatable i will add some records to it..
I want this datatable to update directly to database. Using DataSets and DataAdapter
Please let me know abt this new
Praveen C
|
|
|
|
|
SqlDataAdapter.Update() method.
Mazy
"Art is life, life is life, but to lead life artistically is the art of life." - Peter Altenberg
|
|
|
|