|
Ah, Excellent. That's a great help.
BTW: This isn't my CS202 Final Project. I already Graduated from this University. The project was suppoed to be done in c++, but I needed practice in C# so I decided to look at my old university professors website and get some exercises. I don't really know a lot of places with good C# exercises.
Thanks again Christian.
|
|
|
|
|
Yeah, the Stroustrup C++ book came with excellent exercises, I don't know of a C# book that does the same.
No worries, glad to help.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hey, now i've hit another snag. Since i'm not all that familiar with C# (this is really my first big project in it) I was wondering how I handle events like clicks on these buttons.
I can't really create a fuction called
private void this.button[1]_Click(object sender, EventArgs e)
can i?
I don't think so.
How would I make it so that if i clicked on any of the 81 buttons, it would change the button.BackColor to System.Drawing.Color.Red?
User created event handlers is something i haven't learned or read about yet.
|
|
|
|
|
You can create a single click event, and the sender object identifies the button that sent it. So, if you set the Tag property of each button to identify the button, then you can do something like this:
Button btn = sender as Button;
if ( btn!=null)
{
switch(btn.Tag)
{
}
}
Not sure if switch will work or if you need a bunch of if then else statements. Tag may be an object.
Just read the rest of the question, sorry. As btn is now the button that you clicked, you can set any property, such as teh back color, and it will be set on the button that was clicked.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
You can create a single Button_click handler and apply it to many buttons
this.button[i].Click+=new EventHandler(...);
inside the click handler, use the first arg (object sender) to find out which button
got clicked: cast the object to a button (preferrably using the "as" keyword),
and use the button name or tag to identify it.
|
|
|
|
|
Ultimately what i'm trying to avoid here is the need to create a handler for each individual button since they all bascially act the same no matter which action is performed on them. But the action should only effect the button that was clicked on.
|
|
|
|
|
Ah, you hit it right on the head Luc. I didn't know I could cast the sender variable to a button to figure out which button it was. Most Excellent.
|
|
|
|
|
Ok, now this is a interesting problem.
I put in this code in my Event Handler Function LeftClickButton()
private void LeftClickButton(object sender, EventArgs e)<br />
{<br />
Button temp = sender as Button;<br />
string str;<br />
str = temp.Name;<br />
int x = int.Parse("0" + temp.Name);<br />
int y = Convert.ToInt32(temp.Name);<br />
this.button[x].BackColor = System.Drawing.Color.Red;<br />
}
and whenever I click on a button, the background doesn't change, and I get an exception unhandled error (which i can continue through if possible).
The code fails on the int x = int.Parse("0" + temp.Name); and int y = Convert.ToInt32(temp.Name); both. It's like the convert or the int.Parse arn't converting the temp.name into a integer readable by the dictionary template index. Any advice on this would be appreciated.
|
|
|
|
|
Hello Kataric,
As your Button name is not a number "this.button[i].Name = buttonname.Insert(6, i.ToString())", you can't directly convert to int.
You could name it like: this.button[i].Name = i.ToString();
Apart from that fact you should use the TryParse method instead of Parse or Convert.ToInt32, as TryParse is not throwing an exception.
And last, there is no need to get your Button instance out of the Dictonary as you already have the instance by the sender object.
if(temp!=null) //Should always be done after an as conversion
{
temp.BackColor = Color.Red;
}
Hope it helps,
All the best,
Martin
|
|
|
|
|
I didn't know editing temp would work. Isn't that a new instance of a button?
I come from C++ and i'm used to believing things, unless specified otherwise, be passed by value. Not quite sure how all that works in C# yet.
|
|
|
|
|
Hello,
Kataric wrote: Isn't that a new instance of a button?
No, it's just a reference to the instance.
Kataric wrote: Not quite sure how all that works in C# yet.
I'm sure you will love C# once you get used to it!
All the best,
Martin
|
|
|
|
|
Hi,
I made a new form on my project that allows my user to search using specific search criteria.
Once the record is found, i need to be able to open the Account Management form, so i have a code that looks like this:
private void doEditAccount(IAccount account)<br />
{<br />
AccountManageForm form = new AccountManageForm(account);<br />
form.ShowDialog();<br />
}<br />
<br />
private void searchBtn1_Click(object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
string fileName = "Account_Data.xml";<br />
XPathDocument doc = new XPathDocument(fileName);<br />
XPathNavigator nav = doc.CreateNavigator();<br />
<br />
XPathExpression expr;<br />
expr = nav.Compile("//Account[@ID='" + accountIDInput.Text + "']");<br />
XPathNodeIterator iterator = nav.Select(expr);<br />
<br />
iterator = nav.Select(expr);<br />
<br />
if (iterator.MoveNext())<br />
{<br />
XPathNavigator nav2 = iterator.Current.Clone();<br />
string tmpAccID = (nav2.GetAttribute("ID", ""));<br />
nav2.MoveToFirstChild();<br />
string tmpName = (nav2.Value);<br />
nav2.MoveToNext();<br />
string tmpLastName = (nav2.Value);<br />
nav2.MoveToNext();<br />
string tmpBalance = (nav2.Value);<br />
nav2.MoveToNext();<br />
string tmpOverDraftLimit = (nav2.Value);<br />
nav2.MoveToNext();<br />
string tmpFullAddress = (nav2.Value);<br />
<br />
doEditAccount(tmpName);<br />
this.Close();<br />
}<br />
else<br />
{<br />
MessageBox.Show("Sorry, I could not find any account for\r\nAccount ID : " + accountIDInput.Text + "", "Friendly Bank");<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show("Error : " + ex.Message);<br />
}<br />
}
When i try to build the solution to test this out, im getting this following error(s):
Error 1 - The best overloaded method match for 'FriendlyBank.AdvSearchForm.doEditAccount(AccountManagement.IAccount)' has some invalid arguments
Error 2 - cannot convert from 'string' to 'AccountManagement.IAccount'
Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ
Any help/info would be much appreciated.
|
|
|
|
|
Latheesan wrote: Could someone help me out, where im i going wrong? the code looks right to me, but the compiler begs to differ
Latheesan wrote: string tmpName = (nav2.Value);
tmpName is defined as a string.
Latheesan wrote: private void doEditAccount(IAccount account)
The method doEditAccount requires as a paramters an object of a type that implements the IAccount interface, string does not implement that interface. You need to create the relevant object and pass that to the method.
|
|
|
|
|
Thank you very much for that prompt reply.
I looked at my orignal code inside my MainForm which already had this doEditAccount method which is working fine, i could not see anything different from what i am trying to do on my search form.
Have a quick look at the working code (if you have time) - http://nopaste.php-q.net/294607[^]
The orignal working form does not need any parameter passed into the doEditAccount yet it works fine :s
|
|
|
|
|
I would guess this is what you are missing:
IAccount account = doFindAccount(nameTextBox.Text);
|
|
|
|
|
Thanks for your reply again, i tried to put it in, but it said the doFindAccount method is not found.
Even if i managed to get the method working, the problem is that, the doFindAccount searches for account in the hashtable. When im searching for the first time, this account name would not be in the hashtable.
So, i looked at my main form and found this code that adds new account:
IAccount account = new Account(nameTextBox.Text, "");<br />
account.SetAccNumber(nameTextBox.Text);<br />
doEditAccount(account);<br />
bank.AddAccount(account);
So, from that, i see, the code adds the account to the hashtable and then it does the editing part by opening the acc manage form.
So, i tried to do this in my search form like this:
IAccount account = new Account(tmpName, "");<br />
doEditAccount(account);<br />
bank.AddAccount(account);<br />
this.Close();
Now the previous error is gone and have a new one:
The name 'bank' does not exist in the current context
The AdvSearchForm is VERY similar to the MainForm, yet how can the form cannot find the instance 'bank' ?
|
|
|
|
|
Latheesan wrote: The AdvSearchForm is VERY similar to the MainForm, yet how can the form cannot find the instance 'bank' ?
Because bank is a field on the MainForm, not the form you are working on.
The MainForm takes the bank as a parameter in the constructor and stored it in a field (sometimes known as a member variable). Perhaps you should do the same with this form.
|
|
|
|
|
Great, brilliant suggestion.
I added the following line towards the begining of the partial class:
IBank bank;
editted the search method like this:
IAccount account = new Account(tmpName, "");<br />
doEditAccount(account);<br />
bank.AddAccount(account)
And i was able to successfully build the solution.
However, when i use the search form and enter an account ID and click search, it opens the account management form correctly, but when i close the account management form, i get this exception error message:
http://i9.tinypic.com/4poqqab.png[^]
|
|
|
|
|
Nevermind, i fixed it by removing this line:
bank.AddAccount(account);
from the search method and it seems to be working.
I know this is terrible, trial-and-error method is not a good way to learn new programming language, but im short on time. Im just glad its working and i can move onto the next piece of work.
|
|
|
|
|
But you didn't pass a bank object to the form. Either do this in the constructor, or by setting up a property to receive it. The constructor option is usually preferable if it will always need it, or the object is mandatory.
|
|
|
|
|
But you didn't pass a bank object to the form.
I didn't need to, so i ended up removing the line IBank bank;
|
|
|
|
|
In addition to what colin said, I assume your IAccount instance probably has a property that you can access to pass the information you're wanting to pass where a string is expected.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Thanks allot sir for your reply, but im a bit confused by what you said, since i've never programmed before till few months ago, im still getting used to using Visual Studio C# Express software and learning C#
This is for my programming course (1st year) and it's alrady doing my head.
|
|
|
|
|
well, I'd recommend buying a book and working through it. We're glad to help ( you're asking us to help and trying to do it yourself, and that's never a problem ), but a trial and error approach isn't going to help you understand the core principles that you're struggling to grasp now. The main issues here as far as I can see:
1 - a variable has a type. If you try to pass an object of the wrong type, you're putting a square peg in a round hole. It's a 'thing', but it's not the right 'thing'.
2 - A class is an object designed to wrap specific functionalty. Once you define a class, you can have many instances of a class, just like knowing what a car is, I can own 3. If I have three cars, they are all independant objects, changing the tires on one car won't change the other ones. And, objects that exist within a class, are not visible outside that class, unless they are public, and then they need to be addressed via the class instance. If I want something that's in my car, I have to look inside the car, not on the ground.
I hope that makes *some* sort of sense, but seriously, read through the texts you've been given, or consider buying a book. And, do some really basic to the point of useless projects that exist entirely to help you understand concepts like types and class scope.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I'm writing an Outlook Add-in using VSTO 2005. However there's a slight problem; when I attach event handlers to the CustomAction event of MailItems sometimes the function doesn't get called. There are cases when attaching them more times (at run-time) overcomes this problem, however sometimes it looks like they don't "stick", meaning I click a custom action button once and it works, then I push it a second time and now the handling function does not get called. I even fail to recognize the pattern in this random behavior.
Has anyone else faced similar issues, and if so how do I overcome them?
|
|
|
|
|