|
Like Guffa said. You can also evaluate the ID of sender to determine the identifier associated with the button.
|
|
|
|
|
Here is some code to summarise what others have said.
<code>
private void generalbutton_click(object sender, EventArgs e)
{
Button button = (Button)sender;
MessageBox.Show(button.Name.ToString());
}
</code>
|
|
|
|
|
private void GeneralButton_Click(object sender, Eventargs e)
{
Button btn = sender as Button;
// Do whatever here.
}
Mark.
|
|
|
|
|
Hi,
the fundamental piece of information is: object sender is whatever object
has caused the handler to be invoked, in your case the sender will be a
button, so do:
Button button=sender as Button;
and now you are holding a reference to the button that got clicked;
you can use its position, text, color, ...
three properties are intended specially for identication:
- the Name, that's a string
- the Tag; you can assign whatever you like to a Tag; it takes any object,
and does not use them, its like a post-it to yourself
- the DialogResult; you can assign one of the DialogResult enum values to it.
|
|
|
|
|
|
I have a tabpage and a panel inside this tabpage. When I insert anything to panel I want to center tabpage's scrollbar because I insert anything center of this tabpage but TabPage Scrollbar place on left.Can you explain that how I code that???
another how can I reach height of scrollbar(tabpage's autoscroll)????
-- modified at 5:28 Saturday 28th July, 2007
|
|
|
|
|
If any one has practical ideas for IT engineering graduation project please send me it and I will be very thankful for him
my e-mail is michaelqog_71@hotmail.com
|
|
|
|
|
Practical, depends on your skills... You might try searching the forums for other people asking the same question, and then re-consider this post.
Hogan
|
|
|
|
|
Quit asking the same old question all the time. Use your brains
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I have a database and I figured out how to store data but i can't edit anything.
This is a part of my code:
<br />
private void button1_Click(object sender, EventArgs e)<br />
{<br />
MyDataSet.MyTable.AddMyTableRow(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,textBox5.Text);<br />
MyTableAdapter.Update(MyDataSet.MyTable);<br />
toolStripProgressBar1.PerformStep();<br />
MessageBox.Show("Update Complete", "Update Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);<br />
} <br />
<br />
private void button5_Click(object sender, EventArgs e)<br />
{<br />
DataTable dtedit = MyDataSet.MyTable;<br />
DataRow dr11 = dtedit.NewRow();<br />
dr11["Field1"] = textBox6.Text;<br />
dr11["Field2"] = textBox7.Text;<br />
dr11["Field3"] = textBox8.Text;<br />
dr11["Field4"] = textBox10.Text;<br />
dr11["Field5"] = textBox11.Text; <br />
dr11.AcceptChanges();<br />
MyTableAdapter.Update(MyDataSet.MyTable);<br />
}<br />
|
|
|
|
|
Hi,
I'm trying to receive windows messages to notify my program of when it has been deactivated or activated. I've used the following class, and a basic windows form to try it out:
public class AppMessageFilter : IMessageFilter
{
private const int WM_ACTIVATEAPP = 0x001C;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_ACTIVATEAPP)
{
//Lost focus:
if ((int)m.WParam == 0)
{
MessageBox.Show("Deactivated");
}
//Gained focus:
else
{
MessageBox.Show("Activated");
}
}
return false;
}
}
And....
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(new AppMessageFilter());
}
}
However, this does not work. I've tried different windows messages (e.g. WM_ACTIVATEAPP and WM_ACTIVATE), but neither get filtered in the PreFilterMessage method when I switch from my form to another application (e.g. internet explorer or another window). I'm only using the MessageBox parts to test it works - i've also tried omitting these and testing to see if breakpoints are reached.
I've tried this on both Windows 2000 and Vista 64. Any ideas where i'm going wrong?
Thanks
James
|
|
|
|
|
I don't know what's wrong here, but I copied your code and I don't get any of these messages, too.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi!
I am using to read and write to a specific file.
In The beginig i am doing this:
if (File.Exists("Delete_Items.txt"))
{
bfs = new FileStream("Delete_Items.txt", FileMode.Open);
StreamReader m_streamReader = new StreamReader(bfs);
textBox2.Text = m_streamReader.ReadLine();
m_streamReader.Close();
bfs.Close();
}
So i am reading from the file and then free every thing, but then i am tring to do ub another function this:
FileStream bfs = null;
if (File.Exists("Delete_Items.txt"))
{
File.Delete("Delete_Items.txt");
File.Create("Delete_Items.txt");
bfs = new FileStream("Delete_Items.txt", FileMode.Append);
StreamWriter m_streamWriter = new StreamWriter(bfs);
m_streamWriter.WriteLine(textBox2.Text);
m_streamWriter.Close();
bfs.Close();
}
I want to overrite the data and so i am deleting the file before, and i get an exception explainig me that the file is being used by another process, and this is although i freed it.
What is wrong??
Thanks
|
|
|
|
|
Not sure if it's the whole problem, but try disposing the FileStream and the StreamReader instead of just closing them. And you don't need to delete the file before writing to it, use the FileMode that overwrites. (Don't remember off hand which it is, but intellisense tells you.)
But look at the context that the code runs in, you're not doing multithreaded calls to these methods are you?
|
|
|
|
|
Hi,
good question, I will surprise you.
First of all, you should watch the line numbers: when building a debug version,
the Exception normally shows line numbers (at least for your code, not for
system libraries).
My guess is the line number points to File.Delete
Yes you can not delete your own file, because someone else is accessing it.
That someone is trying to help you somehow; I dont know who it is, here are the
usual suspects:
- some anti-virus tool
- some indexing utility, say Google Desktop Search
such tools are always interested in the latest files !
Remedy: make a method to delete a file; the method should:
- use a for loop (say 5 times), inside it:
- use try-catch,
- in the try part File.Delete and return
- in the catch part: Thread.Sleep(1000) followed by
if (last iteration) File.Delete
This code tries to delete 6 times, with 1 second interval; the last one
throws to the caller, the first 5 just make you wait.
The surprise is: this is exactly what Windows Explorer is doing, when
it cannot delete at first, it retries 5 times before telling you it failed !
Good luck !
|
|
|
|
|
try
using ( StreamWriter m_streamWriter = new StreamWriter(bfs))
{
m_streamWriter.WriteLine(textBox2.Text);
}
Because StreamWriter inherits IDisposable, you can have the using statement. After the last }, the object is closed and disposed properly.
Hogan
|
|
|
|
|
I believe your problem lies with these two lines:
File.Create("Delete_Items.txt");
bfs = new FileStream("Delete_Items.txt", FileMode.Append);
File.Create() returns a FileStream to the file you just created -- this FileStream needs to be closed before you can try to open it again for appending, or just use the FileStream that you just made.
So you'd change it to:
File.Create("Delete_Items.txt");
bfs.Close(); //Added
bfs = new FileStream("Delete_Items.txt", FileMode.Append);
OR
bfs = File.Create("Delete_Items.txt");
-Phil
|
|
|
|
|
Hello experts,
There's something that has been bothering me for quite some time now...
Is there a performance difference between the following two implementations?
for (...)
{
int x = ...;
}
int x;
for (...)
{
} In the first case, is x getting allocated for every step of the loop?
Or does .NET knows to allocate it once, and use it again and again?
Thanks in advance,
Shy.
|
|
|
|
|
I haven't looked at the IL, but I'm pretty sure you're creating and dumping a new integer every interation in your first example. And therefore, yes, it performs worse.
|
|
|
|
|
I see...
Is it the same for reference types?
|
|
|
|
|
Hi,
please read my other reply first.
if it is:
string x="aha";
for (...) {
string y=x;
}
then again it wont make a difference, since y=x simply copies a reference,
it does not create an object; it has the same cost as int y=x;
And again, if the compiler notices x can not change during the loop, it
should and would move the statement outside the loop.
|
|
|
|
|
Exactly the answer I was looking for!
Thanks!
|
|
|
|
|
Hi,
you should not believe everything they tell you.
int is a value type, it probably gets "allocated" on stack (meaning the stack pointer
gets lowered a bit more when you enter this method to reserve a spot for it),
this has zero cost.
(the probably means, maybe the int variable does not need storage, the JIT might
decide to keep it in one of the CPU registers, but I am not relying on this)
Anyway, there is no "new", no object, and no garbage collection involved.
both code snippets could and should result in exactly the same MSIL code;
and I would be very surprised if you can measure the slightest difference in
execution time (which would hint a compiler inefficiency, not an inherent difference
between both code snippets).
|
|
|
|
|
I stand corrected, both of these...
public void Inside() {
for (int i = 0; i < 1000; i++) {
int x = i;
}
}
public void Outside() {
int x;
for (int i = 0; i < 1000; i++) {
x = i;
}
}
decompile to something similar to this...
public void Outside() {
int num1;
int num2;
for (num2 = 0; (num2 < 1000); num2 = (num2 + 1)) {
num1 = num2;
}
}
The compiler does you the favor of moving your variable outside the loop where it belongs.
|
|
|
|
|