|
Hi,
I'm just knocking up a quick program which works out how much savings I can save by entering a deposit, a number of weeks i want to save and then outputting me a total. I have got the program working fine, but if my total saving were £150.50 my total field only displays £150.5
Is there a currency format i'm missing? Here's my code:
namespace Savings_Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double deposit;
int weeks;
double total;
private void btnCalculate_Click(object sender, EventArgs e)
{
deposit = Convert.ToDouble(txtDeposit.Text);
weeks = Convert.ToInt32(txtWeeks.Text);
total = deposit * weeks;
txtTotal.Text = "£" + Convert.ToString(total);
}
Many thanks.
|
|
|
|
|
I guess it would be best to use the 'decimal' type, and you can print it nicely with .ToString("N2") (2 decimals displayed)
Visual Studio can't evaluate this, can you?
public object moo<br />
{<br />
__get { return moo; }<br />
__set { moo = value; }<br />
}
|
|
|
|
|
Hi all, I'm hoping someone ca help me out here as I've been stuck on this one for a while now.
I have a DLL (written in C++) that exposes the following function:
int sendAPDU (byte* TxAPDU, int TxLen, byte* RxAPDU, int* RxLen)
Essentially, this function takes an array of bytes (TxAPDU) with TxLen elements and returns an array of byte (RxAPDU) with RxLen elements. The key issue here which is throwing me is the fact that the last two parameters are reference parameters (or strictly speaking output parameters).
My problem is that, in C#, I can't seem to figure out how to import that function from the DLL and call it. I thought it would be something like:
[DllImport("MyLibrary.dll", EntryPoint = "sendAPDU")]<br />
public static extern int sendAPDU(Byte[] TxAPDU, int TxLen, ref Byte[] RxAPDU, ref int RxLen);
And then when calling the function it'd be something like:
Byte [] TxArray = new Byte[26];<br />
Byte [] RxArray = new Byte[MAX_APDU_LEN];<br />
int RxLength;<br />
<br />
int retval = sendAPDU (TxArray, 26, ref RxArray, ref RxLen);
So what is it that I'm doing wrong? Forgive me if this is a straight forward issue but I'm ew to C#.
|
|
|
|
|
Hi..
I don't know if i can help you on this because i'm not very experienced about PInvoke but just a few thoughts:
1. You have to possibility to Marshal Function Parameters in .Net so maybe the MarshalAs - Attribute could help you.
something like:
<br />
[DllImport("MyLibrary.dll", EntryPoint = "sendAPDU")]<br />
public static extern int sendAPDU([MarshalAs(UnmanagedType.LPArray)] Byte[] TxAPDU, int TxLen, [MarshalAs(UnmanagedType.LPArray)]ref Byte[] RxAPDU, ref int RxLen);<br />
the MarshalAs Attribute can be found under System.Runtime.InteropServices
2. i don't know if this makes a difference in PInvoke but did you try to use out instead of ref for your output parameters?
greets
m@u
|
|
|
|
|
Thanks m@u. I was afraid it might have something to do with Marshaling because I bumped into that once before and never really figured it out. I suppose I still don't understand this whole managed memory versus unmanaged memory thing.
At any rate, I tried your suggestion and read up on the MarshalAs attribute but it still didn't solve my problem and I still don't think I really understand how to use this MarshalAs attribute but to my great relief, the solution to my problem popped out of virtually nowhere.
The C++ programmer in me still pondered on this principle of passing pointers as funtion parameters and it occured to me that, if in C++ passing an array and passing a pointer is one and the same thing, doesn't the same apply for C# and reference parameters?
So I changed my function declaration which follows the DLLImport attribute from:
public static extern int sendAPDU(Byte[] TxAPDU, int TxLen, ref Byte[] RxAPDU, ref int RxLen);
to:
public static extern int sendAPDU(Byte[] TxAPDU, int TxLen, Byte[] RxAPDU, ref int RxLen);
and would you believe it, it works!
I'm still unsure about the memory management issue that this introduces. In C++ this would have left room for serious memory overflow issues, so what happens in C# if you call a function from a DLL written in C++? Let's say, I have a function like this:
public static extern int myFunc(byte[] ByteArray);
which, in the DLL, does something like this:
ByteArray[10] = 10;
Now, if you call this from C# like this:
byte[] sendArray = { 1, 2, 3 };<br />
int retval = myFunc(sendArray);
Clearly in C++ this would have written the value 10 into memory, 7 bytes past the end of the memory allocated to sendArray, causing havoc. So what happens when you call the DLL function from C#?
Sorry guys, I realise that this is one of the central issues to C# and you can't claim to be a C# programmer if you don't understand managed vs. unmanaged memory. But I am a noob.
|
|
|
|
|
Hi,
I would like to know, what are advantages and disadvantages of two template generators:
Guidance Automation Toolkit and Code Smith?
Is it better to develop templates using Code Smith or to develop packages with GAT?
What you think about this both tools?
Wojtek
|
|
|
|
|
Hello,
I am having diffiulty in calling a function from the parent form from the child form without having to create a new instance of the parent form.
Parent form spawns child which upon clicking of a button deletes a member from a database. What I need the button to do is to refresh a table on the parent form.
I have tried overriding refresh on the parent form but I still get the same issue: a nullpointerexception and the parent form is closed as well.
The code in the child form is this:
MessageBox.Show("Occurence Deleted!");
this.Parent.Refresh();
this.Close();
This is the override I have in the parent form:
public override void Refresh()
{
DataSet ds = eo.GetSearch();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Employees";
}
The exception I am receiveing is:
A first chance exception of type 'System.NullReferenceException' occurred in Occurence Manager.exe
System.NullReferenceException: Object reference not set to an instance of an object.
(which is at the following line : this.Parent.Refresh();
What am I doing wrong?
|
|
|
|
|
First of all, I would call your refresh routine by a different name. Secondly, cast your Parent variable. So, suppose that your method was called RefreshData and the parent form was called MyParent, you would call it like this
((MyParent)this.Parent).RefreshData();
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I'm still having the same problem with the exception however. When I run the code in the parent form (via a refresh button) it runs just fine and does what it's supposed to do. I am trying to run this code in the child form after it makes edits to the database but unfortunately it still gives me an exception and closes the parent form.
could it be that the parent form (of the child in question) itself is passed a variable from a (grand?)parent form (the main form) and passes that variable into a protected variable upon initialization?
Here is the form in question expanded:
public partial class SearchWindow : Form
{
EmployeeObject eo;
DataSet ds = new DataSet();
public SearchWindow(EmployeeObject o)
{
InitializeComponent();
eo = o;
//RefreshData();
ds = eo.GetSearch();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Employees";
}
public void RefreshData()
{
// ds = eo.GetSearch();
//dataGridView1.DataSource = ds;
//dataGridView1.DataMember = "Employees";
}
... and so on.
|
|
|
|
|
Sounds like you haven't set the parent of the child form then. The null reference obviously indicates this.Parent.RefreshData();
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I'm running into all sorts of trouble just to try to refresh a window. =)
Anyways I set the parent using this:
eef.Parent = this;
(eef being the instance of the child form)
then displayed it with eef.ShowDialog();
but unfortunately the window does not show and additionally it closes the parent form!
I get this exception: System.ArgumentException: Top-level control cannot be added to a control.
I can't do eef.ParentForm = this; because .ParentForm is read only.
I had assumed earlier however that when you instantiated a child form that it would automatically inherit the parent form.
|
|
|
|
|
I asked this question on the MSDN forums and this was the response:
Try setting the Owner property of the Dialog window:
using(frmMyDialog eef = new frmMyDialog())
{
eef.ShowDialog(this);
}
And in your dialog window:
(this.Owner as frmSearchWindow).RefreshData();
And it worked beautifully. =)
Apparently I needed to set the owner property instead of the parent property.
|
|
|
|
|
|
What is a "help privilege"?
|
|
|
|
|
Please don't send me emails; the forum is best for answering questions because others can then see the answer and benefit from it as well.
|
|
|
|
|
sorry i was mistraken,
anyway thnx for ur kind help
prashanth
|
|
|
|
|
How I can install programs from server to client using c#?
|
|
|
|
|
That's too broad of a question to give you any meaningful answer. If you need to do interprocess communication, look up .NET remoting. For installing stuff, potentially you could send the setup files across the pipe to the client, then the client could use System.Diagnostics.Process to launch the setup file. Most installers have a silent mode you can specify in the Process's arguments if you want the installer to run without intervention.
|
|
|
|
|
First, don't email me with questions. The forum is for questions; others can benefit from the answer you receive.
From the description you gave in the email, it sounds like you want to use ClickOnce technology, which provides auto and manual updates built-in: ClickOnce[^]. Deploy your software with ClickOnce, then use the System.Deployment APIs to update your application.
|
|
|
|
|
|
thanks for your pass,
I'll explain my question: suppose i have a program such as winzip program and i want to install it from my pc to another pc which connected to the same network, how i can do this without sending the program to the other pc.
more thanks again,,,
|
|
|
|
|
Hi,
How can i automaticly select and item in a listbox after i insert it?
<br />
lbLayouts.Items.Add("Layout " + lbLayouts.Items.Count+1); <br />
|
|
|
|
|
int index = lbLayouts.Items.Add("Layout " + lbLayouts.Items.Count+1);
lbLayouts.SelectedIndex = index;
|
|
|
|
|
Thank you very much!
Nuno
|
|
|
|
|
Problem Statement
How do I map multiple events to a single event handler?
Details
I have an MDI application and I would like to map a tree view item click to the same event handler for one of the items below the file menu, say filemenuitem. The tree view is a control within the MDI application.
Additionally, I would like one of the toolbars item clicks to map to the same.
Code Snippet/Others
Thanks in advance.
I am a SysAdmin, I battle my own daemons.
|
|
|
|