|
Create a C# Windows Application. Replace the non-designer class code in the Form with mine below.
The code simply creates 10 Panels on the form, each with one button on it. I assign each button's parent panel to the button's "Tag" property. I assign one common click event handler to all the buttons. In the click event handler, I simply cast the button's "Tag" to a Panel, then do a Form.Controls.Remove() to remove the Panel from the Form's "Controls" collection.
I also create a "debug" window so that I can see when the button click event handler fires.
Run the application and start hitting the spacebar. Exactly as you would expect, as you "click" each button by hitting the spacebar, the respective panel is removed. But keep hitting the spacebar after no more panels are visible. The click event KEEPS ON FIRING. I can understand that the panel can still be alive (and not have been garbage collected) and still have the button in its' "Controls" collection. But how does the spacebar keypress get from the form (which is in focus and has Controls.Count = 0) to the panel (and on to the button) that is no longer in its "Controls" collection???
public partial class Form1 : Form
{
private Form debugForm;
private TextBox debugTextBox;
public Form1()
{
InitializeComponent();
}
void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Panel p = (Panel)(b.Tag);
this.Controls.Remove(p);
debugTextBox.Text += "Click event fired for " + b.Name + " : this.Controls.Count = " +
this.Controls.Count.ToString() + "\r\n";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
debugForm.Close();
}
catch { }
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Panel p = new Panel();
p.Left = 0;
p.Top = i * 35;
p.Width = 300;
p.Height = 30;
Button b = new Button();
b.Left = 0;
b.Top = 0;
b.Width = 300;
b.Height = 30;
b.Name = b.Text = "Button" + i.ToString();
b.Click += new EventHandler(b_Click);
b.Tag = p;
p.Controls.Add(b);
this.Controls.Add(p);
}
Rectangle rScreen = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
this.Left = 0;
this.Width = rScreen.Width / 2;
this.Top = 0;
this.Height = rScreen.Height;
debugForm = new Form();
debugForm.StartPosition = FormStartPosition.Manual;
debugForm.Left = this.Right + 1;
debugForm.Width = this.Width;
debugForm.Top = this.Top;
debugForm.Height = this.Height;
debugTextBox = new TextBox();
debugTextBox.Multiline = true;
debugTextBox.Dock = DockStyle.Fill;
debugTextBox.ScrollBars = ScrollBars.Both;
debugForm.Controls.Add(debugTextBox);
debugForm.Show();
}
}
|
|
|
|
|
I was working on my project and all my tool strip items, label, combo box ect..disappeared. I can still reference the objects in my code, but I cannot see the items even in Visual Studio.
Thanks!
|
|
|
|
|
Hi,
My question is basicly the title. What are the differences in C# between those two? If I implement a Vector3 class (X, Y and Z, all float member). Why would I use struct instead of class? How do I choose? The sintax is pretty much the same... (as I see)
Thanks,
Dirso
|
|
|
|
|
|
Is google broken where you live ? Did someone steal all your reference books ?
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
Calm down! You don't have to be that rude. Sorry if my question has offended you in anyway - it was not my intention. The previous guy has already sent me to google.
I already undertood what you (both) meant and I'll be more carefull next time before posting a question.
Thanks,
Dirso
|
|
|
|
|
Dirso, it's just that this is a question that has been asked so many times! No hard feelings, we just tell people to look at Google, as it's been asked and answered so many times.
Since you've been good about it, I will fill you in on a few things:
Classes:
* stored on the heap
* require garbage collection (thus, more heavyweight than structs)
* when passed to a function, you're passing a reference ("pointer") to the original object
Contrast this with structs:
* stored on the stack
* no need for garbage collection (thus, much more lightweight)
* when passed to a function, a copy of the object is made
|
|
|
|
|
Thank you so much for this information!
I actually was not sad with you, you're right, I should gougle it first, but yesterday I posted a few questions after googling with no success and since I was here I just asked - my mistake and I'm sorry.
Thanks again for your help,
Dirso
|
|
|
|
|
If you want to be able to change the values X, Y or Z after the instance has been created, you should make it a class. A struct should be immutable, to avoid situations where you would modify a copy of the data that you actually wanted to modify.
A struct is a bit tricker to implement correctly, so until you have read up a bit on the subject you should just stick to classes.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks! Performance could be an issue here, so if struct is faster I'll go for it. about change values I'll try to use "out"/"ref" parameters everytime, right?
Thanks,
Dirso
|
|
|
|
|
Dirso wrote: Performance could be an issue here, so if struct is faster I'll go for it.
If you have an array of the type, a structure uses less memory, which can improve performance a bit. A struct is faster for some operations, but you have to keep it smaller than 16 bytes to be efficient.
Dirso wrote: about change values I'll try to use "out"/"ref" parameters everytime, right?
That doesn't help, as you would still change the vales of a copy, which doesn't affect the original.
A structure should be immutable, i.e. you assign the values when you create the instance, and if you want to change a value you create a new instance. That way the structure instance works as a single value, which is the intention of the structure concept.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hello everyone,
I've got a little problem.
I made a project and it runs well. In the project I access a database, I create table and access them. The database I use is Microsoft SQL Server 2005, the free database from Microsoft. After I installed it, I enter a connectionstring like this: DERESEN\SQLEXPRESS and it will work fine. So I tried it with my friends computer and it didn't work, I had to change the name (DERESEN) in the computer's name, so I did it like this: Environment.MachineName + "\SQLEXPRESS". And it did work.
Now the problem: I need to get it work on every (Windows-based) computer, so I let my girlfriend instal it at her computer (a computer no-no should be able to do it), but it didn't work. I let her instal SQL Server 2005 and it didn't work.
So now my question:
Is there any database which is ALWAYS installed on a windows computer which I can access. Or is there another way (which I didn't see) to get a database running?
I hope you can help me, because I've been searching for days for an answer to this problem.
|
|
|
|
|
Deresen wrote: Is there any database which is ALWAYS installed on a windows computer which I can access.
No.
Deresen wrote: Or is there another way (which I didn't see) to get a database running?
You could always use SQL Server Compact Edition[^]. This runs inside the process space of the application (it runs as a DLL) and it's free.
|
|
|
|
|
Use .\SQLEXPRESS or (local)\SQLEXPRESS
More about connection strings[^]
And make sure that the database is attached to the server.
Sql server (not windows) has its system databases that you should not manipulate.
Eslam Afifi
modified on Thursday, September 11, 2008 8:17 PM
|
|
|
|
|
Eslam is correct, but SQL Server has to be installed with the "default instance" to be able to do that. If someone installs it using a "named instance" and calls it something else, it won't work.
|
|
|
|
|
Deresen wrote: Is there any database which is ALWAYS installed on a windows computer which I can access
No.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
You can use SQLite[^] for your purposes. It's just a single file database. There is also ado.net managed provider for it: System.Data.SQLite[^]
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
This is probably a simple question, but how do I open another application from a Windows form?
What I basically want to do is execute a search for a file or files, have those files appear in a listbox, then have the application open the selected file using a specific application installed on the user's desktop. It's basically a simplified Windows search application designed for a very specific purpose.
Thanks.
|
|
|
|
|
Use System.Diagnostics.Process.Start to start the application. If the file is associated with a default application, that application will be started up - so, for instance, executing Process.Start(@"c:\MyFile.doc"); would start Word and load that document.
|
|
|
|
|
System.Diagnostics.Process.Start
Eslam Afifi
|
|
|
|
|
I have 6 tabs on my windows forms. I want it to change tabs with I click the next button can you any help.
Shelby Learning the Code!
|
|
|
|
|
Call SelectedTab and select the tab you want.
|
|
|
|
|
private void Navigate()
{
//this.tabControl1.SelectedTab = this.tabPage1;
//this.tabControl1.SelectedTab = this.tabPage2;
//this.tabControl1.SelectedTab = this.tabPage3;
//this.tabControl1.SelectedTab = this.tabPage4;
//this.tabControl1.SelectedTab = this.tabPage5;
}
This is what I have. I need with I click the Next button it on to tab 2 if I press again tab 3 and so on. I click the previous button it goes backward.
Shelby Learning the Code!
|
|
|
|
|
So keep track of which tab you're on and use that as the basis for navigation.
|
|
|
|
|
Need help in writing this.
Shelby Learning the Code!
|
|
|
|