|
Eliee wrote: so pay attention to efficiency
Gosh! That's a bit of a schoolmarm comment.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
My: Website | Blog
|
|
|
|
|
Is there any reason you want to combine the sets? If you mean that the combined set should not have duplicates, that seems like a lot of unnecessary work if it only is going to be used to find the largest value.
Otherwise just find the largest value in each set, then pick the largest of those two.
Are you making something that is going to be used for something, or is it just a school assignment?
---
b { font-weight: normal; }
|
|
|
|
|
Okay, if you want to find the n largest numbers in a large array of numbers, here's one way to do it:
(I'm a VB.Net regular myself, and I don't really have the time to figure out how to produce guaranteed-to-work C# code, so I'll do pseudo-code here)
Define a variable smallestSelected to hold the smallest eligable number, and initialize it to zero.
Define an array, say largestNumbers, large enough to hold n values.
Define a variable smallestSelectedIndex to hold the index of the smallest selected number in the largestNumbers array
Define a rolling index variable, say index, to hold an index into the largestNumbers array.
Loop through all your array's elements, and if the current element value > smallestSelected, then:
if the largestNumbers array is full (index == n-1):
replace the element in largestNumbers at smallestSelectedIndex with the current value
if the largestNumbers array is not yet full,
set the largestNumbers array element at index to the current value; increment index;
At this point you should determine the new smallest value from the largestNumbers array will be and to do this you will either have to sort it, or loop over it to find that number.
If you're opting for the sorting approach you could make largestNumbers a sorted list of kinds, and there'd be no need for a rolling index pointer since you could always pick the first element (which is the smallest number), remove it, and add the new value.
The result of this should be O(n) on the original array, but a slight overhead on each iteration that yields a value that is larger than the current smallest eligable number.
This is a bit off the top of my head, so if there's any really bad flaws to the logic, feel free to comment on it ;o)
Hope this helps,
Danny
|
|
|
|
|
Hello people !!. I'm developing in vs 2005 and I've a problem that may be you can help me to solve it.
I have a winform, a boolean attribute for a state and a timer. The timer see the state and if is closing then it start fading out. So the only things I have is a timer to animate fade out on mouse leave event.
The problem is this example run excelent in Visual Studio 2003, but a flick appear in VS 2005 when I leave the form. May be a problem with winfows.form or system.drawing
Any ideas??
I have this very simple code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplicationTest
{
public class Form1 : Form
{
private bool m_closing;
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Timer timer1;
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 20;
this.timer1.Tick += new System.EventHandler (this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font ;
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(292, 266);
this.DoubleBuffered = true;
this.ForeColor = System.Drawing.Color.White ;
this.Name = "Form1";
this.Text = "Form1";
this.MouseEnter += new System.EventHandler(this.Form1_MouseEnter);
this.MouseLeave += new System.EventHandler (this.Form1_MouseLeave);
this.ResumeLayout(false);
}
#endregion
public Form1()
{
this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true );
InitializeComponent();
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
m_closing = true;
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
this.m_closing = false;
this.Opacity = 1;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (m_closing)
{
if (this.Opacity > 0)
{
this.Opacity = this.Opacity - 0.01;
}
else
{
Close();
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Thanks a lot !!!!
|
|
|
|
|
Have you tried enabling double buffering?
|
|
|
|
|
yeap, I try that when I start havinf the problem but the problem still there. You'll see that the problem is with VS 2005 but not in vs 2003.
Thanks.
|
|
|
|
|
I found the problem ...
See this link:
http://www.dotnet247.com/247reference/msgs/42/214827.aspx
Yes, that is what I found. I hadn't seen the flickering before because my
form was too small.
However, this is true. The flickering occures when the underlaying window is
switched to "layered mode". Actually it flickers even if you set alpha to
255 (fully opaque).
Switching to layered mode involves three steps.
1. Checking the os version - it has to be NT 5 or latter
2. Set WS_EX_LAYERED window style
3. SetWindowLayeredAttributes this method is not defined in user32.dll for
versions before Win2k that's why first step is necessary.
When a Windows Forms control is created it is not a layered window. And it
switches to this mode when set the opacity to some value less then 1.
So my solution is to switch to layered mode in some early stage of the form
life. As soon as the form is created before to become visible. Otherwise
we'll see the flickering.
Javier is completely correct that the layered window has to be fully
redrawn. But it goes only when the window (form) resizes. You can still
invalidate some portions of the form and it will works how ot supposed to
work.
Layered window could even show better performance in some situations.
How internaly layered windows work is all painting is redirected to
off-screen buffer and when the painting's done alpha blending is applied to
the resulting picture and the result is drawn on the screen. This of course
is done internaly in windows and it uses the video card hardware support for
alpha blending if it has any.
Becuse all winodw is double buffered Windows doesn't send WM_PAINT when the
form is moved for example or when some part of the form is uncovered. In
such cases Windows uses double buffered image.
When children control is moved on the form it updates as much as it is
necessary. So the performance hit is not so big.
Anyway, my first solution that I was planning to post involved P\Invoke and
calling all API functions necessary to switch to "layered mode", but then I
found that if one set Opacity to value less then 1 and then return it back
to 1 the form class doesn't switch to the "normal mode" and leave the window
layered.
So the easiest way to get rid if this flickering is by overriding
OnControlCreated method and adding the following two lines:
this.Opacity = 0.9;
this.Opacity = 1;
No more flickering .
Thanks a lot people !!!
|
|
|
|
|
hello
i would like insert a image in sql Data base with oledb Connection.
please help me.
vahid
|
|
|
|
|
|
i think following code will help u:
1st Step: Take pic. box
2nd Step: Take a Button and Text box to take the image, which u want to save into Database
private void btnBrowse_Click(object sender, System.EventArgs e)<br />
{<br />
OpenFileDialog ofd = new OpenFileDialog( ) ;<br />
ofd.Filter = "Image files |*.jpg" ;<br />
if ( ofd.ShowDialog( ) == DialogResult.OK ) <br />
{ <br />
txtPath.Text = ofd.FileName ; <br />
try<br />
{<br />
FileStream f = new FileStream ( txtPath.Text, FileMode.Open ) ;<br />
byte[ ] buff = new byte [ f.Length ] ;<br />
f.Read ( buff, 0, ( int ) f.Length ) ;<br />
MemoryStream ms= new MemoryStream(buff,0,buff.Length);<br />
Image i = Image.FromStream ( ms ) ;<br />
img.Image = i ; <br />
f.Close();<br />
}<br />
catch(Exception ee) <br />
{ <br />
MessageBox.Show("An error has occured.\n" + ee.Message); <br />
} <br />
}<br />
}<br />
3rd Step: Create table where take one field which will image type
CREATE TABLE [dbo].[tableName] ([img_name] [image] NULL )
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
<br />
string dbcon = "data source=ServerName;database=dbName;user id=sa;password=";<br />
private void linklblInsertPicture_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)<br />
{<br />
FileStream st = new FileStream ( txtPath.Text, FileMode.Open ) ;<br />
byte[ ] buff = new byte [ st.Length ] ;<br />
st.Read ( buff, 0, ( int ) st.Length ) ;<br />
<br />
try<br />
{ <br />
using(SqlConnection conn = new SqlConnection())<br />
{ <br />
conn.ConnectionString = dbcon;<br />
conn.Open();<br />
SqlCommand cmd = new SqlCommand();<br />
cmd.Connection = conn;<br />
cmd.CommandText = "insert into tableName(imageName) values(@pict)";<br />
cmd.Connection = conn;<br />
cmd.CommandType = CommandType.Text;<br />
cmd.Parameters.Add("@pict",buff);<br />
cmd.ExecuteNonQuery();<br />
}<br />
}<br />
catch(SqlException sqle)<br />
{<br />
Console.WriteLine(sqle.Message);<br />
}<br />
st.Close();<br />
}<br />
<br />
private void linklblShowPicture_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)<br />
{<br />
try<br />
{<br />
using(SqlConnection conn = new SqlConnection())<br />
{<br />
conn.ConnectionString = dbcon;<br />
conn.Open();<br />
SqlCommand cmd = new SqlCommand();<br />
cmd.Connection = conn;<br />
cmd.CommandText = "select imageName from tableName where .... ";<br />
\\here u can use track id in datatabel to select, update or delete image<br />
cmd.Connection = conn;<br />
cmd.CommandType = CommandType.Text;<br />
<br />
SqlDataReader r = cmd.ExecuteReader();<br />
if(r.Read())<br />
{<br />
byte[ ] b = ( byte[ ] ) r [ 0 ] ;<br />
MemoryStream st = new MemoryStream( ) ;<br />
st.Write ( b, 0, b.Length ) ;<br />
Image i = Image.FromStream ( st ) ;<br />
img.Image = i ; <br />
}<br />
}<br />
}<br />
catch(SqlException sqle)<br />
{<br />
Console.WriteLine(sqle.Message);<br />
}<br />
}
Thankyou
BILLAH
|
|
|
|
|
|
Hi.
What work do the throw statement, alone.
What exception it force to take place?
Best wishes
|
|
|
|
|
The throw keyword *edit*(when used by itself, alone, i.e. throw; )*/edit* can only be used within a catch block. Throw will simply throw the exception that was caught within the catch block. Last modified: Saturday, May 27, 2006 8:10:31 PM --
|
|
|
|
|
Judah Himango wrote: The throw keyword can only be used within a catch block. Throw will simply throw the exception that was caught within the catch block.
Sorry to correct you, but throw can also be used outside of a catch block. The general purpose of this is to initiate an user generated exception. E.g.
if (!File.Exists("test.txt")) {
throw new FileIOException("File was not found");
}
else {
....
}
Graham.
|
|
|
|
|
Graham N wrote: Sorry to correct you, but throw can also be used outside of a catch block.
Sorry to correct you, but the OP was talking about the throw keyword alone, that is without specifying an exception. That can only be used inside a catch block.
---
b { font-weight: normal; }
|
|
|
|
|
Perhaps I've misread what specifically the OP was asking about. But as you have said, the throw keyword without an exception can only be used inside the catch block. That of course is true.
|
|
|
|
|
I believe the original poster was talking of the throw keyword by itself, rather than throwing an exception instance:
try
{
}
catch
{
throw;
}
|
|
|
|
|
Hi.
I have used "AxInterop.ShockwaveFlashObjects.dll" in my project.
How can I handle write click on it?
There is't any mouse event for it.
Best wishes
|
|
|
|
|
Hello,
I got a weird problem with an sql string and a datetime field.
First the code:
<br />
SqlCommand myCommand = new SqlCommand("SELECT * FROM orders WHERE date = @date", connect);<br />
<br />
myCommand.Parameters.AddWithValue("@date", DateTime.Parse(listOrders.Text.ToString().Substring(0, 19)));<br />
<br />
SqlDataReader myReader2 = myCommand.ExecuteReader();<br />
<br />
while (myReader2.Read())<br />
{<br />
Messagebox.Show(myReader2["amount"].ToString() + " | " +products = myReader2["productid"].ToString());<br />
}<br />
The table orders exists of a few rows which are added for testing purposes.
Some are added by hand (so in the "show table data" window), others are added by a form with a datetime.now value.
Now the weird thing is that the query gets a result when the date of the row was added by hand, but when the date of the row was added by the other form, it doesn't get a result (so the while loop isn't executed).
I don't understand why this goes wrong?
Can somebody plz help?
Tnx!
|
|
|
|
|
So, the form doesn't insert data? Or it inserts date but not with the values you expect? Either way, it would appear to be a problem with the way that the data is inserted, not with the way it is retrieved.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
My: Website | Blog
|
|
|
|
|
No, it's really weird, I will give you exact copies of the values I find in the date field.
By Hand:
23/10/1986 20:24:23
Inserted data:
26/05/2006 12:39:48
So the data is EXACTLY the same (as far as I know).
That's why I cannot understand it?
|
|
|
|
|
JelleM wrote: So the data is EXACTLY the same (as far as I know).
The problem is that you don't know enough.
The dates you are looking at cuts the time after the seconds and doesn't show the milliseconds. When you compare if two dates are equal, they have to be equal down to the last millisecond. When you take the truncated date that you see and compare it to the date in the database, they will differ by a fraction of a second, but that is enough for them not to be equal.
To get the exact time you need to make the comparision work, you have to display the date with full precision so that you get the millisecons also.
As you see, identifying records by a datetime value is not a very good method. You should use an id instead.
---
b { font-weight: normal; }
|
|
|
|
|
Tnx for the reply!
But the thing is I don't see the milliseconds (if I show the results in the database), so they must be hidden in some way or another.
But can't I use some sort of * to add to the truncated date, so the query doesn't look at the milliseconds or something like that?
TNX!
|
|
|
|
|
JelleM wrote: But the thing is I don't see the milliseconds (if I show the results in the database), so they must be hidden in some way or another.
They are not so much hidden, as they are not shown. If you read the values from the database and display them using a date format that shows the milliseconds, you'll see them.
JelleM wrote: But can't I use some sort of * to add to the truncated date, so the query doesn't look at the milliseconds or something like that?
You could subtract and add a second from the time, and get the records between the times. But then the time is not unique any more...
As I said, using a datetime value to identify a record is not a very good method.
---
b { font-weight: normal; }
|
|
|
|
|
TNX!
|
|
|
|
|