|
I have a collection of Items
I Want To select random given numbers from the collection
Without Select an Item Twice
in Other Word If The Collection have 25 items i want to
Select 7 items form it without select an item twice
|
|
|
|
|
I don't think this is best approach,other CPains please correct me:Use Random class to create random numbers and store each number in seprate place and check second time create another number and check if that number is not used before.
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
Thanks Mazdak for your Reply
I Thought about Solution Like Yours
That I Create arraylist and store the value and check about
its existig before add item to the arraylist and loop until
I have the selected number but i didn't use this solution
becaus i think that may be lead to infinit loop
In other words what happend if some items in collection never selected and i still need more item i'm not sure if this true all the algorithms
in Random Class Designed to avoid This situation
|
|
|
|
|
What I've done in the past is:
1. Add the objects (numbers in your example) to a collection, so that the collection count is 25 (items 0 through 24).
2. Create a loop to select n items from the collection.
3. On each iteration of the loop, select a random number from zero to collection.Count - 1
4. Retrieve the collection element at the selected index from the collection, then remove it from the collection. The next time the loop iterates, the previously selected item will no longer be selectable, it has been removed from the collection.
There are various optimizations over using a collection, depending on the type of item you're trying to select (reference type, character, integer etc), but the principle is valid, assuming that your original range contained no duplicates.
Hope this helps.
|
|
|
|
|
I'm attemping to create a custom control for the first time and I cannot figure out what I'm doing wrong. I'm attempting to inherit from TreeView using the folowing:
public class HL7TreeView : System.Windows.Forms.TreeView
I've added some properties and Methods and built the new DLL. I then am able to add it to my ToolBox with (Add/Remove ToolBox Item). But when I attempt to drag it into my Designer I get a StackOverflow exception. I've rebooted countless times. I even attempted a UserControl instead of a CustomControl with the same results. Any thoughts ?
I think I might have found the problem....
I commented out the Get and Set command for my properties and the error no longer occurs. Can i not use Gets andf Set with CustomControls ?
|
|
|
|
|
Your Get property probably calls itself, that would be the place to start looking.
|
|
|
|
|
Thanks for the help.
Since this is my first Custom Control i guess I'm not sure how to debug it. I added my HL7TreeView Control Library Project to a test project. Then I added a generic TreeView control to the Windows form. I changed the declaration to use HL7TreeView instead of plain TreeView, but the debgger won't enter my Control Library code. Any hints you can give on debugging a custom control ?
|
|
|
|
|
Hi,
I'm having trouble setting the main form visible from within the child form. I have set the main form to hide (SignatureMain.Hide()).
I then load the child form from within a button on form1:
Treatment.ShowDialog();
On the child form Treatment i try to do the following in an Exit button:
this.ParentForm.Visible = true;
this.Close();
What i'm gettign is the following:
An unhandled exception of type 'System.NullReferenceException' occurred in Signature.exe
Additional information: Object reference not set to an instance of an object.
NOTE:
I have even set the parent form as follows:
Treatment.ShowDialog(this);
This is suppose to set the this (SignatureMain) as the parent to Treatment.
When i run the debbugger and look at ParentForm, its null???
Thanks
|
|
|
|
|
Ok i have figured out the solution. Here it is for anyone looking for the information later.
When loading a form using: myForm.ShowDialog(this) were "this" is the OWNER of the form you are loading, you need to use in the child form the following to get to its properties:
this.Owner.Visible = true; <-- "this" is the child form and i'm accessing the visible property of the parent form (owner).
I'm still not clear as to why Parent and or ParentForm did nto work so if anyone has the answer to that one please post.
Enjoy!
|
|
|
|
|
The Owner or Parent is not immediately available during the creation of the child window/control.
I'm not sure when the reference is exaclty created, but I use the Layout event of the child window with a flag that's set to true when the layout event has been called so the code (or some part of it) in the event handler won't be called again.
<br />
private void Child_Layout....{<br />
if(!flag)<br />
{<br />
flag = true;<br />
}<br />
}<br />
|
|
|
|
|
If I want to draw on a control, is it best to override the OnPaint function or to capture the Paint event?
|
|
|
|
|
When you're inheriting (which you obviously are, since otherwise you wouldn't have the choice) you should override OnPaint . The Paint event exists to notify anyone outside your class that needs to know when you're repainting.
This is true for all the OnXxx protected methods of the class.
Charlie
if(!curlies){ return; }
|
|
|
|
|
To explain why Charlie said what he did (which is correct), overriding is polymorphic, allowing base classes to call your code even though they don't even know about your class. In intermediate language (IL, the code all .NET languages compile to and embedded in assemblies), this instruction uses callvirt instead of simply call . This is much faster to execute and is a good polymorphic design. When you handle events, several - sometimes many - intructions are required to enumerate the delegates for the event (commonly stored in a list after compiling) and to invoke the delegate (late-binding).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hii
i m trying to delete a row using DeleteCommand of DataAdapter, but i can't.
i m using this code..
using System;
using System.Data;
using System.Data.SqlClient;
class ModifyDataTable
{
static void Main()
{
string constr = @"server=(local)\NetSDK;Integrated Security=true;"+
"database=address";
SqlConnection Conn = new SqlConnection(constr);
try
{
string SQL = "SELECT * from Hello";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(SQL,Conn);
DataSet ds = new DataSet();
da.Fill(ds,"Hello");
DataTable dt = ds.Tables ["Hello"];
("Select * From Hello",Conn);
foreach( DataRow r in dt.Rows)
{
Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15),
r["Phone"].ToString ().PadLeft(25));
}
Console.WriteLine("\n" + dt.Rows .Count.ToString ());
da.DeleteCommand = new SqlCommand ("DELETE FROM Hello WHERE (Name='Amit')" ,Conn);
da.Update (ds,"Hello");
Console.WriteLine ("\n" + dt.Rows.Count.ToString ());
foreach( DataRow r in dt.Rows)
{
Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15),
r["Phone"].ToString ().PadLeft(25));
}
}
catch (Exception e)
{
Console.WriteLine ("Error Occured : " +e.Message );
Conn.Close ();
}
finally
{
Conn.Close();
Console.ReadLine ();
}
}
}
This isn't deleting anything neither in the dataset nor in the database
|
|
|
|
|
You should use a DataAdapter when you want your updates and deletes to be managed for you depending on the state of the DataSet .
In your case, you are not altering the <cpde>DataSet, so your delete command is never used.
If you just want to execute a specific delete command:
string sql = "DELETE FROM Hello WHERE (Name='Amit');
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
Charlie
if(!curlies){ return; }
|
|
|
|
|
Im trying to work out how i would go about altering the C# DirectX example "SampleGrabberNET" so i could perform captures from a video file (avi,mpeg,etc) rather than a capture device.
Extremely confused, i can see how the video player example works but not how i intergrate the 2 examples.
Cheers.
|
|
|
|
|
I tried to modify a couple of the icons supplied with .Net using the icon editor I've been using for years. The strange thing is that after I've opened the icon and saved it (even without changes), I can not use the icon as intended.
If an existing icon to a button, it fits nicely into the button as it should. I open the icon and find it is a standard 32x32 icon and save it as a new icon (with or without changes). If I then use the new icon on the same button, the icon is way too big to fit.
Can anybody explain what is different about this new icon that it no longer fits? Is there a recommended tool that I should now be using to create my icons?
Thanks.
db
|
|
|
|
|
hi, anyone out there help me with a c# project, trying to write code involving methods and if else statments - beginners stuff
i'm confused can pay for your time
|
|
|
|
|
1) Rather than paying someone for their time, why not get a book?
What a piece of work is man, how noble in reason, how infinite in faculties, in form and moving how express and admirable . . . and yet to me, what is this quintessence of dust? -- Hamlet, Act II, Scene ii.
|
|
|
|
|
|
You'd get a better response if you post a question here with the problem you encountered on your code.
|
|
|
|
|
Exactly, if it';s beginenr stuff, just post the code and probably somebody'll pick it up...
|
|
|
|
|
hi all,
this days ,i make Assemby ide , but i hae a probem with the resered words to be highlighted ,i want an effectie way to highlight the selected words
thanks
|
|
|
|
|
|
hi all,
this days ,i make Assemby ide , but i hae a probem with the resered words to be highlighted ,i want an effectie way to highlight the selected words
thanks
|
|
|
|