|
Not the best, but neither was the original code
I agree that a lot of duplicate typing could have been avoided.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hi,
1.
sebogawa wrote: images[0] = new Bitmap(Bitmap.FromFile("0.bmp"));
images[1] = new Bitmap(Bitmap.FromFile("1.bmp"));
images[2] = new Bitmap(Bitmap.FromFile("2.bmp"));
images[3] = new Bitmap(Bitmap.FromFile("3.bmp"));
images[4] = new Bitmap(Bitmap.FromFile("4.bmp"));
images[5] = new Bitmap(Bitmap.FromFile("5.bmp"));
images[6] = new Bitmap(Bitmap.FromFile("6.bmp"));
images[7] = new Bitmap(Bitmap.FromFile("7.bmp"));
images[8] = new Bitmap(Bitmap.FromFile("8.bmp"));
images[9] = new Bitmap(Bitmap.FromFile("9.bmp"));
images[10] = new Bitmap(Bitmap.FromFile("10.bmp"));
images[11] = new Bitmap(Bitmap.FromFile("11.bmp"));
images[12] = new Bitmap(Bitmap.FromFile("12.bmp"));
images[13] = new Bitmap(Bitmap.FromFile("13.bmp"));
images[14] = new Bitmap(Bitmap.FromFile("14.bmp"));
images[15] = new Bitmap(Bitmap.FromFile("15.bmp"));
images[16] = new Bitmap(Bitmap.FromFile("16.bmp"));
images[17] = new Bitmap(Bitmap.FromFile("17.bmp"));
images[18] = new Bitmap(Bitmap.FromFile("18.bmp"));
images[19] = new Bitmap(Bitmap.FromFile("19.bmp"));
images[20] = new Bitmap(Bitmap.FromFile("20.bmp"));
images[21] = new Bitmap(Bitmap.FromFile("21.bmp"));
images[22] = new Bitmap(Bitmap.FromFile("22.bmp"));
images[23] = new Bitmap(Bitmap.FromFile("23.bmp"));
images[24] = new Bitmap(Bitmap.FromFile("24.bmp"));
images[25] = new Bitmap(Bitmap.FromFile("25.bmp"));
images[26] = new Bitmap(Bitmap.FromFile("26.bmp"));
images[27] = new Bitmap(Bitmap.FromFile("27.bmp"));
images[28] = new Bitmap(Bitmap.FromFile("28.bmp"));
images[29] = new Bitmap(Bitmap.FromFile("29.bmp"));
Did you notice some similarity between those lines? Take advantage of it, write a simple 2-line loop that does exactly the same, saving over 1000 characters (and keystrokes?).
2.
How would you do it by hand?
3.
Hint: forget about arrays, this is another great place for a collection. Look at List<Bitmap>
|
|
|
|
|
How about loading your images into a List<Bitmap> . Apply a random Sort algorithm, then create a Queue<Bitmap> from the list.
Now you can just dequeue each image as you display it.
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Use a List, pick a random index (0 to Count-1 ), display and remove that image, repeat until empty.
|
|
|
|
|
Yes, and while we're at it: Why load all these bitmaps in advance at all? I would only load the one currently selected for display. For selection an array of filenames will suffice. This will go a little easier on the memory.
With 30 bitmaps this may not appear important, but still it is a waste of memory. Ans when one day the program is expanded to support 300 bitmaps (or 30000, 3000000 ...) you will get to see something I have not seen since the days of the old C64: An out of memory exception.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
Hi
I would try the following approach. Create the class in which you store the bitmap and a boolean field, which tells you if the image was already shown:
public class ImageItem
{
private Bitmap _image;
public bool Shown{get;set;}
public ImageItem(Bitmap image)
{
_image = image;
Shown = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if(images.Any(image => image.Shown == false))
{
nextImage()
}
else
{
}
}
private void nextImage(Bitmap image)
{
int randNumber;
do
{
randNumber = RandomNumber(0, 29);
}while(images[randNumber].Shown != false)
images[randNumber].Shown = true;
imageArea.Image = images[randNumber];
}
I typed the code directly to the page(without visual studio) so I hope I didn't make any typos.
Uros
|
|
|
|
|
So 12 hours later you other responses and pass it off as your own
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hey sorry I didn't even know that is a bad thing. I just saw a question and offered to help.
|
|
|
|
|
How do i declare an array that is one-dimensional and contains 30 bitmaps? The bitmaps are loaded from picture files on my hard disk.
|
|
|
|
|
Bitmap[] bitmaps = new Bitmap[30];
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
An array is an array, it isn't special just because it holds Bitmaps.
I suggest you get your C# book and study it (again).
|
|
|
|
|
Luc Pattyn wrote: study it (again)
What makes you think they studied it the first time
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Nothing really. That is exactly why I've used parentheses.
Now if I had be sure, I could have left out again entirely.
|
|
|
|
|
I am using this code on combo value change but it's not saving to the XML when the user navigates quickly..
xml_document.SelectSingleNode("/Resume/Detail/ReferencesTitle").InnerText = (string)cboTitle.EditValue;
xml_document.Save(public_var.file_name);
is there any way to lock the application until the above process is completed?
modified on Tuesday, March 23, 2010 9:57 AM
|
|
|
|
|
You don't want to lock the application but rather synchronize access to the resource. You can use many synchronization objects for this, like a Monitor or Mutex.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
any example or resource for such case (writing to xml)? thx
|
|
|
|
|
We're not here to spoon feed you! You have enough information to do your own research.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
How can I call
private void btnSave_Click(object sender, EventArgs e)
from
private void txtName_KeyDown(object sender, KeyEventArgs e)
something like:
private void txtName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
RUN btnSave_Click
}
}
|
|
|
|
|
Call the PerformClick method on the button:
btnSave.PerformClick();
Kristian Sixhoej
"Invisible women out to kill ya - Oh, great!"
|
|
|
|
|
The easiest thing to do is take the code out of btnSave_Click and put it in a separate method. Give it an informative name.
Then call that method from the btnSave_Click and txtName_KeyDown handlers.
|
|
|
|
|
Not quite. See the previous reply.
|
|
|
|
|
OK, it's not easier, but I hate having code in event handlers.
Put the code in a separate method and call the method.
|
|
|
|
|
As KeyEventArgs derives from EventArgs you can pass an instance of it to the click handler if you like so you can do:
if (e.KeyCode == Keys.Enter)
btnSave_Click(sender, e); This will just run the code in the click handler. If you actually want to simulate a click then you should call PerformClick();
if (e.KeyCode == Keys.Enter)
btnSave.PerformClick(); The effect may be the same but it is possible that other things can happen in the control's internal code before the handler so base your decision on whether this could potentialy cause an issue (this could happen in a later version of the control and possibly break your application).
If the code that is being run due to the the Enter key press and button click is not directly related to the click then you should move it out to a separate method, and call your new method in each handler.
private void txtName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
DoMyStuff();
}
private void btnSave_Click(object sender, EventArgs e)
{
DoMyStuff();
}
private void DoMyStuff()
{
}
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
modified on Monday, March 22, 2010 6:42 PM
|
|
|
|
|
This doesn't really answer your question, but from the example code you gave, you could accomplish the same thing by going to the form's properties and setting "AcceptButton" to "btnSave". Then, when you press ENTER, btnSave_Click will be called automatically.
|
|
|
|
|
Except that the keydown event appears to be on a textbox not the form itself.
I know the language. I've read a book. - _Madmatt
|
|
|
|