|
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?
|
|
|
|
|
I recently did a combination plugin for Excel/Outlook using the Office 2007 PIAs and VSTO2005... I ran into the same behavior with BOTH applications... What I finally had to end up doing was after each event was fired, I rewired the event, just to be on the safe side because I could not predict when and where it would happen.
It's a nasty thing to have to do, but it's no worse than having to switch a 1-based index just to code to Office apps.
HTH,
~dnc
|
|
|
|
|
That I already tried - even so the the problem still remains
|
|
|
|
|
|
hi
i have 3 tables : t_books,t_sessions,t_titles that has master/details relation together :
t_books(bookID(PK),bookName) -> master
t_sessions(sessionID(PK),bookID(FK),sessionName) -> details
t_sessions(sessionID(PK),bookID(FK),sessionName) -> master
t_titles(titleID(pk),sessionID(fk),titleName,text,footerPage) -> details
and in normal load data(when retrieve all data without any condition) it's work good, but when i retrieve data with condition, i recieved the
following error message :
This constraint cannot be enabled as not all values have corresponding parent values.
my code is here :
private void FillTreeView(string searchText)<br />
{<br />
SqlConnection con = new SqlConnection(Properties.Settings.Default.Database1ConnectionString);<br />
SqlCommand cmd = new SqlCommand();<br />
SqlDataAdapter adapter = new SqlDataAdapter();<br />
cmd.CommandText = "select * from t_books where bookName like '%" + searchText + "%'";<br />
cmd.Connection = con;<br />
adapter.SelectCommand = cmd;<br />
DataSet ds = new DataSet();<br />
<br />
SqlCommand cmd2 = new SqlCommand();<br />
SqlDataAdapter adapter2 = new SqlDataAdapter();<br />
cmd2.CommandText = "select * from t_sessions where sessionName like '%" + searchText + "%'";<br />
cmd2.Connection = con;<br />
adapter2.SelectCommand = cmd2;<br />
<br />
SqlCommand cmd3 = new SqlCommand();<br />
SqlDataAdapter adapter3 = new SqlDataAdapter();<br />
cmd3.CommandText = "select * from t_titles where titleName like '%" + searchText + "%'";<br />
cmd3.Connection = con;<br />
adapter3.SelectCommand = cmd3;<br />
<br />
adapter.Fill(ds, "t_books");<br />
adapter2.Fill(ds, "t_sessions");<br />
adapter3.Fill(ds, "t_titles");<br />
<br />
DataRelation dataRelation;<br />
DataRelation dataRelation2;<br />
<br />
DataColumn dc1 = ds.Tables["t_books"].Columns["bookID"];<br />
DataColumn dc2 = ds.Tables["t_sessions"].Columns["bookID"];<br />
<br />
DataColumn dc3 = ds.Tables["t_sessions"].Columns["sessionID"];<br />
DataColumn dc4 = ds.Tables["t_titles"].Columns["sessionID"];<br />
<br />
dataRelation = new DataRelation("Relation1", dc1, dc2);<br />
dataRelation2 = new DataRelation("Relation2", dc3, dc4);<br />
ds.Relations.Add(dataRelation);<br />
ds.Relations.Add(dataRelation2); <br />
TreeNode book;<br />
TreeNode session;<br />
TreeNode title;<br />
<br />
foreach (DataRow row in ds.Tables["t_books"].Rows)<br />
{<br />
book = new TreeNode(string.Format("{0}", (string)row["bookName"]));<br />
book.Tag = row;<br />
ds.Tables["t_sessions"].DefaultView.RowFilter = "bookID=" + row["bookID"];<br />
for (int i = 0; i < ds.Tables["t_sessions"].DefaultView.Count; i++)<br />
{<br />
DataRowView sessionRow = ds.Tables["t_sessions"].DefaultView[i];<br />
session = new TreeNode(string.Format("{0}", (string)sessionRow["sessionName"]));<br />
session.Tag = sessionRow;<br />
<br />
ds.Tables["t_titles"].DefaultView.RowFilter = "sessionID=" + sessionRow["sessionID"];<br />
for (int j = 0; j < ds.Tables["t_titles"].DefaultView.Count; j++)<br />
{<br />
DataRowView titleRow = ds.Tables["t_titles"].DefaultView[j];<br />
title = new TreeNode(string.Format("{0}", (string)titleRow["titleName"]));<br />
title.Tag = titleRow;<br />
<br />
session.Nodes.Add(title);<br />
}<br />
<br />
book.Nodes.Add(session);<br />
}<br />
this.treeView2.Nodes.Add(book);<br />
}<br />
}
how to solve my problem ? thanks
-- modified at 16:31 Monday 14th May, 2007
|
|
|
|
|
hdv212 wrote: DataColumn dc3 = ds.Tables["t_sessions"].Columns["sessionID"];
DataColumn dc4 = ds.Tables["t_titles"].Columns["sessionID"];
Are you sure that each value in t_titles has a session Id that is found in t_sessions ?
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 )
|
|
|
|
|
hi
i have 3 tables : t_books,t_sessions,t_titles that has master/details relation together :
t_books(bookID(PK),bookName) -> master
t_sessions(sessionID(PK),bookID(FK),sessionName) -> details
t_sessions(sessionID(PK),bookID(FK),sessionName) -> master
t_titles(titleID(pk),sessionID(fk),titleName,text,footerPage) -> details
and in normal load data(when retrieve all data without any condition) it's work good, but when i retrieve data with condition, i recieved the following error message :
This constraint cannot be enabled as not all values have corresponding parent values.
my code is here :
private void FillTreeView(string searchText)<br />
{<br />
SqlConnection con = new SqlConnection(Properties.Settings.Default.Database1ConnectionString);<br />
SqlCommand cmd = new SqlCommand();<br />
SqlDataAdapter adapter = new SqlDataAdapter();<br />
cmd.CommandText = "select * from t_books where bookName like '%" + searchText + "%'";<br />
cmd.Connection = con;<br />
adapter.SelectCommand = cmd;<br />
DataSet ds = new DataSet();<br />
<br />
SqlCommand cmd2 = new SqlCommand();<br />
SqlDataAdapter adapter2 = new SqlDataAdapter();<br />
cmd2.CommandText = "select * from t_sessions where sessionName like '%" + searchText + "%'";<br />
cmd2.Connection = con;<br />
adapter2.SelectCommand = cmd2;<br />
<br />
SqlCommand cmd3 = new SqlCommand();<br />
SqlDataAdapter adapter3 = new SqlDataAdapter();<br />
cmd3.CommandText = "select * from t_titles where titleName like '%" + searchText + "%'";<br />
cmd3.Connection = con;<br />
adapter3.SelectCommand = cmd3;<br />
<br />
adapter.Fill(ds, "t_books");<br />
adapter2.Fill(ds, "t_sessions");<br />
adapter3.Fill(ds, "t_titles");<br />
<br />
DataRelation dataRelation;<br />
DataRelation dataRelation2;<br />
<br />
DataColumn dc1 = ds.Tables["t_books"].Columns["bookID"];<br />
DataColumn dc2 = ds.Tables["t_sessions"].Columns["bookID"];<br />
<br />
DataColumn dc3 = ds.Tables["t_sessions"].Columns["sessionID"];<br />
DataColumn dc4 = ds.Tables["t_titles"].Columns["sessionID"];<br />
<br />
dataRelation = new DataRelation("Relation1", dc1, dc2);<br />
dataRelation2 = new DataRelation("Relation2", dc3, dc4);<br />
ds.Relations.Add(dataRelation);<br />
ds.Relations.Add(dataRelation2);
TreeNode book;<br />
TreeNode session;<br />
TreeNode title;<br />
<br />
foreach (DataRow row in ds.Tables["t_books"].Rows)<br />
{<br />
book = new TreeNode(string.Format("{0}", (string)row["bookName"]));<br />
book.Tag = row;<br />
ds.Tables["t_sessions"].DefaultView.RowFilter = "bookID=" + row["bookID"];<br />
for (int i = 0; i < ds.Tables["t_sessions"].DefaultView.Count; i++)<br />
{<br />
DataRowView sessionRow = ds.Tables["t_sessions"].DefaultView[i];<br />
session = new TreeNode(string.Format("{0}", (string)sessionRow["sessionName"]));<br />
session.Tag = sessionRow;<br />
<br />
ds.Tables["t_titles"].DefaultView.RowFilter = "sessionID=" + sessionRow["sessionID"];<br />
for (int j = 0; j < ds.Tables["t_titles"].DefaultView.Count; j++)<br />
{<br />
DataRowView titleRow = ds.Tables["t_titles"].DefaultView[j];<br />
title = new TreeNode(string.Format("{0}", (string)titleRow["titleName"]));<br />
title.Tag = titleRow;<br />
<br />
session.Nodes.Add(title);<br />
}<br />
<br />
book.Nodes.Add(session);<br />
}<br />
this.treeView2.Nodes.Add(book);<br />
}<br />
}
how to solve my problem ? thanks
|
|
|
|
|