|
I want to delete member of list with ID that user enter in textbox in form (WPF C#)
my code is:
class Course
{
public int CodeCourse { get; set; }
public string NameCourse { get; set; }
public int UnitCourse { get; set; }
}
class Education
{
public Education()
{
courses = new List<course>();
studentes = new List<student>();
}
public List<course> courses { get; set; }
public List<student> studentes { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
education = new Education();
}
private Education education;
private void AddCourse_Click(object sender, RoutedEventArgs e)
{
Course crs = new Course();
crs.CodeCourse = int.Parse(CodeCourse.Text);
crs.NameCourse = NameCourse.Text ;
crs.UnitCourse = int.Parse(UnitCourse.Text);
education.courses.Add(crs);
}
private void DeleteCourse_Click(object sender, RoutedEventArgs e)
{
//delete a member of list<course>
}
private void EditCourse_Click(object sender, RoutedEventArgs e)
{
//edit a member of list<course>
}
modified 12-Oct-14 10:58am.
|
|
|
|
|
Start by using the editor here to insert <> so your generic Lists appear correctly.
Then, study the basic operations on generic List<T> [^]:
Remove Removes the first occurrence of a specific object from the List<T>.
RemoveAll Removes all the elements that match the conditions defined by the specified predicate.
RemoveAt Removes the element at the specified index of the List<T>.
RemoveRange Removes a range of elements from the List<T>. Unless you establish a habit of using the documentation to answer very simple questions like this one, you are going to have great difficulty learning any language.
Obviously, you are going to need a Button, and to have some way to pass the information about which List Item to delete to the EventHandler assigned to that Button that actually deletes the Item from the specific instance of your List<Course> stored in the instance of the Education Class.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
Hi,
I am trying to use this article to generate a unique identifier:
Generating Unique Key (Finger Print) for a Computer for Licensing Purposes[^]
I created a project, the added a class called FingerPrint.cs
Now I want to know how can I get the result?
I tried:
FingerPrint finger_print = new FingerPrint();
this.Text = finger_print.ToString();
but not getting the license key!
Kindly help...
Technology News @ www.JassimRahma.com
|
|
|
|
|
first of all, these sort of questions are better posted to the article themselves - that way they get the attention (hopefully) of the author
second, where do you see ToString() defined for the fingerprint class, hmmmm ? if your answer is 'I dont'', then that's why you're not seeing a result - it looks like you need you use :-
this.Text = finger_print.Value();
|
|
|
|
|
I'm new to c# and i'm using vs 2013 and access database..my i need to do multiple update records after I filtered the records in datagridview
in my database table I have these fields ID(PK/autonumber), EID,Date,Day,Daystatus..
In my form I have a button and it's fucntion is filter by date using bidingsource.fiter and this was the result
EID------Date-----------Day---Daystatus
10175--10/11/2014---sat--------regular
10176--10/11/2014---sat--------regular
10177--10/11/2014---sat--------regular
10178--10/11/2014---sat--------regular
what I want to happen is when I click another button called "Apply to all" I want to Update all the daystatus to "Legal Holiday" of the filtered records..I know how to update one by one using simple update..but I need to do the multiple update..
I tried to make a code but it was updating all the records not the filtered only here's my code hope someone can help me
try
{
OleDbCommand command = new OleDbCommand();
connection.Open();
command.Connection = connection;
string query = "SELECT EID From EmployeeTable";
command.CommandText = query;
String a = "Legal Holiday";
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection;
//String counter2;
//String status = "Absent";
for (var i = 0; i < 1; i++)
{
command2.Connection = connection;
string query2 = "update EmployeeData set DayStatus='" + a + "'where EID='" + reader["EID"] + "'";
command2.CommandText = query2;
command2.ExecuteNonQuery();
}
}
MessageBox.Show("Successfully");
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
modified 11-Oct-14 11:09am.
|
|
|
|
|
Your code is potentially susceptible to SQL Injection[^].
NEVER use string concatenation to build a SQL query; always use a parameterized query instead.
using (var command2 = new OleDbCommand())
{
command2.Connection = connection;
command2.CommandText = "update EmployeeData set DayStatus = ? where EID = ?";
command2.Parameters.AddWithValue("@DayStatus", a);
OleDbParameter pEID = command2.Parameters.Add("@EID", OleDbType.VarChar);
while (reader.Read())
{
pEID.Value = reader["EID"];
command2.ExecuteNonQuery();
}
}
In this case, since you're filtering by date, you'll need to pass that date filter to your query:
using (var command = new OleDbCommand())
{
command.Connection = connection;
command.CommandText = "update EmployeeData set DayStatus = ? where Date = ?";
command.Parameters.AddWithValue("@DayStatus", a);
command.Parameters.AddWithValue("@Date", theFilteredDate);
command.ExecuteNonQuery();
}
If you can't pass the filter to the query, then you'll have no choice but to update each record individually.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Please provide code to create Web browser in C# without using web browser control and perform Sending HTTP request messages
and Receiving HTTP response messages,History and fav
|
|
|
|
|
Use the HttpWebRequest and HttpWebResponse classes to send/receive raw HTML. (Both are available on MSDN)
If you consider that hard, then also consider that interpreting the HTML "correctly" will be harder.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Errm. That is not the way this site works. Building a we browser from scratch is a complex task. It's highly unlikely that we are just going to give you that.
|
|
|
|
|
I want to invoke MapWindow GIS software from external .net or c# programme script give any simple examples like "hello World" and small processes like "union","intersection" etc
|
|
|
|
|
Examples are found using Google, and never built on request.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
I am using NAudio library to play songs using an ASIO Soundcard. Currently, AsioOut class exposes 'ChannelOffset' property to select the channel. If we are playing a Stereo file with ChannelOffset as 4, the media will be played at Channel 4 and 5.
The Stereo file will have Left and Right Channel which will play the media at Channel 4 and 5. So, whatever user specifies, right channel will be assigned to a channel incremented by 1.
I need to provide the flexibility to the user to specify the channel number for both the Left and Right Channel. So, if user specifies 4 and 7, the left channel will play media at 4 and right channel at 7.
Is there a way to achieve this using NAudio and specifically for ASIO soundcard ?
Any suggestion or workaround is welcome!
Praveen Raghuvanshi
Software Developer
|
|
|
|
|
You would probably get more and better suited responses at the CodePlex forum of NAudio, as you are asking a question that's rather specialized in their area.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Okay, I've spent 2 days trying this and I give up/surrender. Please help...
I have a form (form1) and it has 3 labels (label1, label2, laebel3). On form1_load, label1, 2, & 3 load with values from a stored procedure. From a menu item, I select form2. form2 has a textbox (tb1). I populate tb1 with the #2. I press change and pass 2 and the value of form1.label1 into a stored procedure. Also, pressing change closes form2. The stored procedure from form2 retrieves three values. I want to pass the three values back to form1 and repopulate label1, 2, & 3. That is where the problem is. I pass values into form2's procedure just fine. I return the values just fine. I just cannot get the form1 labels to refresh with the new data.
I have some code below that I tried, but nothing seems to work. I was thinking a refresh or application do events or initialize components... nothing.
private void ChgLoc()
{
GetPrimeLocBal.UidDal = Login.PassUserIdText;
GetPrimeLocBal.NewLoc = tbLocId.Text;
}
private void RunChgLoc()
{
lblHoldSecLoc.Text = GetPrimeLocBal.SecondaryLoc;
lblHoldTeam.Text = GetPrimeLocBal.TeamName;
lblHoldUserName.Text = GetPrimeLocBal.UserNameId;
}
private void TransferMain()
{
new MainDesktop
{
PrimeLoc = lblHoldSecLoc.Text,
UserNameId = lblHoldUserName.Text,
TeamName = lblHoldTeam.Text
};
}
private void MainReload()
{
var md = new MainDesktop();
md.panel1_Enter();
}
private void btnChange_Click( object sender, EventArgs e )
{
ChgLoc();
RunChgLoc();
TransferMain();
MainReload();
Close();
}
|
|
|
|
|
fyi: OriginalGriff has written a series of articles here on CP illustrating different methods of passing data; I think you'll learn a lot reading them: [^].
Your code: you are creating new instances of Forms every time you execute some of your methods, and since there's no reference kept to the Form instances created, as soon as you exit the method code, that Form instance goes into the void.
Do create your Form instances once, keep references to them, and re-use them.
Assuming the Form with the three labels is your main form in a WinForm application, and it creates and shows the second Form, the issue of how to update the Labels on the main form can be addressed in several different ways. I'll illustrate one of them:
private Form2 theForm2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
theForm2.ChangeLabels = delegate(string s1, string s2, string s3)
{
label1.Text = s1;
label2.Text = s2;
label3.Text = s3;
};
theForm2.Show();
}
public Action<string, string, string> ChangeLabels;
private void TestChangeForm1Labels_Click(object sender, EventArgs e)
{
ChangeLabels("1", "2", "3");
} This is really a variation on the "classic way" of handling this kind of asynchronous update of one Form from another by defining a Public Event in the secondary Form which the "consumer" Form can then subscribe to if it has a reference to the secondary form.
I say "variation" because an Action (or a Func) in C# is a delegate, and can be used directly in the same way "classic" EventHandlers ... which have the "object sender, EventArgs e" business are used. In this example we "inject" a pointer to a method in the instance of the main form into the instance of a secondary form: essentially we give the secondary form a call-back to the main form.
I chose to illustrate this method here because: I am bored with writing standard event-handling code in answers here on CP, and felt like doing something different, and, to me, this technique is kind of "leaner and meaner"
But, there are many ways one can implement form-to-form (object-to-object) communication; this is only one of them.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
modified 9-Oct-14 17:42pm.
|
|
|
|
|
Perfect, absolutely perfect. This is exactly what I needed.
Here is what I did...
private string _holdSecLoc;
private string _holdTeam;
private string _holdUname;
public Action<string, string, string> ChangeLabels;
private void ChgLoc()
{
GetPrimeLocBal.UidDal = Login.PassUserIdText;
GetPrimeLocBal.NewLoc = tbLocId.Text;
}
private void btnChange_Click( object sender, EventArgs e )
{
ChgLoc();
GetPrimeLocBal.GetPrime();
_holdUname = GetPrimeLocBal.UidDal;
_holdTeam = GetPrimeLocBal.TeamName;
_holdSecLoc = GetPrimeLocBal.SecondaryLoc;
ChangeLabels( _holdUname, _holdTeam, _holdSecLoc );
Close();
}
Now in MainDesktop (aka Form1)
_locChgChange.ChangeLabels = delegate( string s1, string s2, string s3 )
{
tssUserId.Text = s1;
tssTeam.Text = s2;
tssPrimeLoc.Text = s3;
};
private void changeSitesToolStripMenuItem_Click( object sender, EventArgs e )
{
_locChgChange.Show();
}
|
|
|
|
|
I am developing a windows App and i have a requirement to make cell of datagridview "Read Only or Disable" after just clicking the cell of datagridview this must not be happened on button click it must be done on cell click only.
I am newbie to C# that's why not getting the appropriate solution, Please help me by giving any example.
THANKS in Advance
|
|
|
|
|
|
Hi Simon,
I have just tried all the method by making changes according to me giving in this link provided by you but it is not working. I think it is for two column cell but i have to make current cell disable or read only after just clicking it
|
|
|
|
|
I have been intrigued by the fact that you can use Func and Action instances as EventHandlers directly (after all, they are delegates). Using them "raw," without the usual business/ceremony of 'object sender, 'EventArgs e, does, of course, go "against the grain" of the conventional usage pattern for Events in C#.
Let me wallow in some mud code before I get to my question:
Using this very simple class:
public class UseAGenericFunc<T>
{
public Func<T, T, T> FuncToUse;
}
private void TestUsingGenericFunc()
{
UseAGenericFunc<string> testWithString = new UseAGenericFunc<string>();
testWithString.FuncToUse = delegate(string str1, string str2)
{
MessageBox.Show("direct delegate assignment");
return str1 + str2;
};
testWithString.FuncToUse = (str1, str2) =>
{
MessageBox.Show("lambda assignment");
return str2 + str1;
};
Func<string, string, string> aStringFunc = delegate(string str1, string str2)
{
MessageBox.Show("use of named Func added by +=");
return "named Func " + str1 + str2;
};
testWithString.FuncToUse += aStringFunc;
testWithString.FuncToUse += delegate(string str1, string str2)
{
MessageBox.Show("delegate added by +=");
return str1 + str2 + str1;
};
testWithString.FuncToUse += NamedMethodFuncToUse;
string result = testWithString.FuncToUse("hello ", "goodbye");
}
private string NamedMethodFuncToUse(string str1, string str2)
{
MessageBox.Show("using named method");
return "named method " + str1 + str2;
} You are astute (well, at the least, competent) if you looked at the code above and anticipated that the test would result in four appearances of 'MessageBox. And, I am sure you understood that the second assignment (to a lambda expression) essentially discarded/replaced the first assignment (to a "formal" delegate).
Of course, if you assign an anonymous method as an EventHandler, you are never going to be able to remove it from the delegate's method dispatch queue using the -= operator. So, that's one "big" argument in favor of assigning named methods, or, in this case, named instances of Func.
Finally (sound of large gong), the question(s):
Do you make use of "raw" Action or Func delegates in your coding practice; if so, why, and in what contexts do you find them indicated, or useful ?
If you think such usage of Action and Func are very bad things, why ?
thanks, Bill
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
BillWoodruff wrote: Using them "raw," without the usual business/ceremony A multicast delegate* as a field (not focussing on it not residing in the class where the event is being invoked)
Let me first defend the event-pattern, that's easier; there's always a sender - it's an object, so there's no excuse for passing "null" there. Makes tracing stuff a lot easier. There's also probably a need for a list of parameters, but one can pass those as an object;
class MyArgs: EventArgs
{
public string Str1 {get;set;}
public string Str2 {get;set;}
} It is a clean and predictable pattern, which inherits nicely
"Clean" means that there's no 360-column list of arguments, but two - an object, and args (one with mayhaps 360 members).
BillWoodruff wrote: If you think such usage of Action and Func are very bad things, why ? Depends on where you do it, and why. If it has an advantage over other constructs, then it is automatically good.
Change the code to bind nothing to the field of the generic object, and run it. The field is called while being null. That could be bad, but without context that is hard to judge. The section on Events[^] also adds that it is thread-safe if there's more than one delegate bound to the invocation list.
MSDN magazine[^] has a good explanation on why there's more code in the pattern than in a raw delegate, and why. If for your class the "why" does not apply, then you have a good argumentation.
BillWoodruff wrote: Of course, if you assign an anonymous method as an EventHandler, you are
never going to be able to remove it from the delegate's method dispatch queue If you put your method in a delegate (say, a local variable or a field), and then bind it, you can also unbind it again using that reference. You can verify that by invoking the GetInvocationList[^] method on the delegate-field (of the generic object).
*) "Delegates with more than one method in their invocation list derive from MulticastDelegate, which is a subclass of System.Delegate", from MSDN[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks for your very thoughtful reply, Eddy; just the type of substantial response I was hoping for.
While I do admit to just generally "liking" the structure/behavior/use of Action, and Func Types, I am not on a "crusade" for using Action/Func as delegates in the way shown here; it's my nature to continually explore the language, and, by testing its/my limits, hopefully increase my own understanding.
I consider it possible that one response to this thread might be something related to thread-safety, or other aspects, of using Action/Func that I would have never been able to formulate which may revise my thoughts about their use.
"Delegates with more than one method in their invocation list derive from MulticastDelegate, which is a subclass of System.Delegate." I find this very interesting: I note, and show in the code example, that one can bind any number of methods to an Action or Func delegate: doesn't that mean they are also MultiCast ? Aren't all delegates inherently multi-cast ?
"If you put your method in a delegate (say, a local variable or a field), and then bind it, you can also unbind it again using that reference" I should have qualified my statement about the limitation possible by assigning an anonymous method to a Func/Action delegate by saying (the obvious): you cannot remove it if you have not kept a reference to it.
It's interesting to me that some third-party controls I use that expose Events really do not provide a practically useful object in the Events 'object sender parameter.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
BillWoodruff wrote: Thanks for your very thoughtful reply My pleasure, it was a fun question to answer. And come with code. And actual and expected results. Most people have items on their buglist that come with less information
BillWoodruff wrote: Aren't all delegates inherently multi-cast You can treat 'em like they are, even though the multicast-delegate appears to inherit from delegate. There's no inheritance-chain displayed on MSDN for other delegates. As far as I can see, every delegate has an invocation list. That would imply that each delegate could potentially be multicast.
BillWoodruff wrote: It's interesting to me that some third-party controls I use that expose Events
really do not provide a practically useful object in the Events 'object sender
parameter. We never touched the subject in school, so I expect that most programmers will not be very familiar with it.
..and it's not just third-party controls; WPF shows perfectly how Microsoft ignores its own recommendations.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: there's always a sender - it's an object, so there's no excuse for passing "null" there.
Static events?
Do not pass null as the sender parameter when raising a non-static event.
On static events, the sender parameter should be null .
Static events should pass null as the value of sender
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It's a recommendation, and half a good one; try putting a static object into an object variable. There's no sense in boxing/unboxing if there's no object to box/unbox. Even the compiler thinks so.
Depending on the size of the project, I'd recommend passing the typeof(sumClassName).FullName as the sender there, which is allowed. It adds cost, of course, since one is fetching a property-value and boxing that.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|