|
Seeing that you want to reduce the number of lines, I reformatted your code:
foreach(Control ctrl in mainPanel.Controls)
{
Panel p = ctrl as Panel;
if(p == null)
continue;
foreach(Control ctl in p.Controls)
{
CheckBox chk = ctl as CheckBox;
if(chk == null)
continue;
if(chk.Text == "Check All" || chk.Text == "Uncheck All")
continue;
if(chk.Checked)
{
sql += " " + chk.Text + "=1,";
}
else
{
sql += " " + chk.Text + "=0,";
}
}
} Basically, continue continues the cycle, so you don't need to do the else (and the extra braces and indenting.)
You're code looks fine. Have you stepped through it in the debugger? See what mainPanel.Control contains. Maybe your two other panels are siblings to mainPanel , or grandchildren of mainPanel .
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Ah thats it, the two that weren't processing were inside another panel. To many panels within panels. I have never been able to get the debugger to work with ASP pages =P.
Thank you again,
Jake
FYI: 264 lines of code compressed into 24.. amazing what you can do when you know what you're doing...
|
|
|
|
|
You're welcome, I'm glad I could help!
JakeHolt wrote:
FYI: 264 lines of code compressed into 24.. amazing what you can do when you know what you're doing...
It has happened to me also. You spend a full day fighting something, and you make it work. Then, a few days later you find out that you have done all of it in 5 minutes with 10 lines of code.
That's the beauty of it!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Hello,
When I am switching tabs, how can I prevent the user from switching the tab unless all the textboxes on the current tab are filled in?
Is there a way of telling the index of the currently opened tab?
Thank you
|
|
|
|
|
Handle this in the SelectedIndexChanged event of the TabControl.
Live Life King Size
Alomgir Miah
|
|
|
|
|
How can I tell in what index Tab I was, and the current index Tab I am in now.
Thank you
|
|
|
|
|
Actually there is no such thing, you will have to maintain a variable yourself called previousIndex. Everytime the index changes set it. So in the next index change you can use it.
Hope this helps.
Live Life King Size
Alomgir Miah
|
|
|
|
|
But can I get a current index, inside SelectedIndexChanged event?
Thanks
|
|
|
|
|
Thats right.
this.tabControl1.SelectedIndex will give you the current index.
Live Life King Size
Alomgir Miah
|
|
|
|
|
There's no built-in way, something like a SelectedIndexChanging event. But it's easy to implement:
private int selectedTab = 0;
private int newTab = -1;
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(tabControl1.SelectedIndex == selectedTab ||
tabControl1.SelectedIndex == newTab)
return;
newTab = tabControl1.SelectedIndex;
tabControl1.SelectedIndex = selectedTab;
if(DialogResult.Yes == MessageBox.Show(this, "Sure?", "??",
MessageBoxButtons.YesNo))
{
tabControl1.SelectedIndex = newTab;
selectedTab = tabControl1.SelectedIndex;
}
newTab = -1;
} I hope this helps!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Both of your replays are very informative, and helpful
Thank you very much.
|
|
|
|
|
You're welcome, I'm glad I could help!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I already needed something similar, so I set up to implement it. Here's the result.
public delegate void TabControlCancelEventHandler(object sender,
TabControlCancelEventArgs e);
public class TabControlEx : TabControl
{
public event TabControlCancelEventHandler SelectedIndexChanging;
public TabControlEx()
{
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
if(newTab != -1)
return;
try
{
newTab = SelectedIndex;
SelectedIndex = selectedTab;
TabControlCancelEventArgs e2 = new TabControlCancelEventArgs(newTab);
OnSelectedIndexChanging(e2);
if(e2.Cancel)
return;
SelectedIndex = newTab;
selectedTab = SelectedIndex;
base.OnSelectedIndexChanged(e);
}
finally
{
newTab = -1;
}
}
protected virtual void OnSelectedIndexChanging(TabControlCancelEventArgs e)
{
if(SelectedIndexChanging != null)
SelectedIndexChanging(this, e);
}
private int selectedTab = 0;
private int newTab = -1;
}
public class TabControlCancelEventArgs : CancelEventArgs
{
public TabControlCancelEventArgs(int tab)
: base()
{
this.tab = tab;
}
public TabControlCancelEventArgs(int tab, bool cancel)
: base(cancel)
{
this.tab = tab;
}
public int Tab
{
get { return tab; }
}
private int tab;
} To use it, just subscribe to the SelectedIndexChanging event, and set e.Cancel to false if necessary.
Good luck!
-- LuisR
P.S. Sorry for the XML comments.
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I am trying to use it, however even after declaring everything:
TabControl myTab = new TabControlEx ();
when I am trying to assign the event to myTab, I can't see Selected IndexChanging only the SelectedIndexChanged.
What am I doing wrong?
Thank you
|
|
|
|
|
Probably it's the Intellisense. Try writing:
myTab.SelectedIndexChanging += new TabControlCancelEventHandler(myTab_SelectedIndexChanging); [EDIT: is new TabControlCancelEventHandler and not TabControlCancelEventArgs ]
and then add the handler:
private void myTabSelectedIndexChanging(object sender, TabControlCancelEventArgs e)
{
if(validation fails)
e.Cancel = true;
} and compile the code. If you still can't post more details about the code, and I'll check it.
Good luck!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Man, worked like a charm.
Once again thank you very much.
|
|
|
|
|
You're welcome.
I was thinking of writing a little article for this class, but I think it's a bit too simple.
Maybe I'll save it for a future "10 miscellaneous tips" article, or something like that. What do you think?
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Help articles are always welcome.
Especially if they are explained and outlined in the same manner
like you did to my TabControl poblem.
Thanks
|
|
|
|
|
I'll think about it and if I have some free time during the weekend, I'll write a small article.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
In the topic
Cursor Constructor (Type, String) it tells how to add a cursor as a resource to your project so you can open it at runtime without copying around a second file.
There are two parts of it I do not understand. One is that it only talks about the command line changes. What command line? I build from the IDE. The other is the GetType call. What am I supposed to get the type of?
The sample from the help system:
Using the C# compiler:<br />
csc /resource:"MyWaitCursor.cur","MyCursors.MyWaitCursor.cur" MyCursor.cs<br />
Using the Visual Basic compiler:<br />
vbc /resource:"MyWaitCursor.cur","MyCursors.MyWaitCursor.cur" MyCursor.vb
(This sample is in VB rather than C#. Blame MS.
' Looks namespace.MyCursor.cur in the assemblies manifest.<br />
<br />
' The following generates a cursor from an embedded resource.<br />
' To add a custom cursor, create or use an existing 16x16 bitmap<br />
' 1. Add a new cursor file to your project: <br />
' File->Add New Item->Local Project Items->Cursor File<br />
' 2. Select 16x16 image type:<br />
' Image->Current Icon Image Types->16x16<br />
' --- To make the custom cursor an embedded resource ---<br />
' In Visual Studio:<br />
' 1. Select the cursor file in the Solution Explorer<br />
' 2. Choose View->Properties.<br />
' 3. In the properties window switch "Build Action" to "Embedded"<br />
' On the command line:<br />
' Add the following flag:<br />
' /res:CursorFileName.Cur,Namespace.CursorFileName.Cur<br />
'<br />
' The following line uses the namespace from the passed-in type<br />
' and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.<br />
' NOTE: The cursor name is acase sensitive. <br />
<br />
Me.Cursor = New Cursor(Me.GetType(), "MyCursor.Cur")
--------------
Will Pittenger
|
|
|
|
|
1: You only have to read the command line part if you're using the command line to build your projects (and you don't). People that don't have Visual Studio, but only the .Net Framework, can still build projects with the command line, or something like that.
2: You have to give a type, because the cursor looks in the assembly(project) of that type for the resource you specified in the second argument. If you do Me.Cursor = New Cursor(GetType(System.Windows.Forms.Form), "MyCursor.Cur") , you ain't gonna get a cursor, because the System.Windows.Forms.dll doesn't have a 'MyCursor.cur' resource.
Hope this helps!
Will L Pittenger wrote:
This sample is in VB rather than C#. Blame MS.
You gotta love those Redmond guys for that!
|
|
|
|
|
1. GetType is still not a valid function. What namespace is that in?
2. Since I do not want a cursor from the Forms dll but rather my executable, what do I pass GetType?
--------------
Will Pittenger
|
|
|
|
|
1: Dunno, it's a VB.Net operator, i doubt if those are in namespaces. You can use this.GetType() .
2: Well, just pass this.GetType() , and the constructor will look for the cursor in your executable, and in the namespace of this (i think i forgot to mention that) If you want to get a resource in code, you have to get. Example:
namespace BlaBla {
class MyForm : System.Windows.Forms.Form {
cursor GetCursor {
IO.Stream stream = Reflection.Assembly.GetCurrentAssembly.
GetManifestResourceStream("BlaBla.Cursor.ico")
cur = new Cursor(stream);
stream.Close();
Cursor cur = new cursor(this.GetType(), "Cursor.ico");
return cur;
}
}
}
Oh yeah, and if you want a colored cursor, you can do this:
Instead of adding a .cur resource to your project, add a colored .ico Icon resource.
Then you can do Cursor cur = new cursor(this.GetType(), "Cursor.ico");
Hope this helps!
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
No good. Since I only needed one instance of the cursor, I made it static. I wanted to prevent changes, so I added readonly.
I took the readonly part out so I could see if the rest was right.
With the following code to allocate a cursor:
if(cursorMovingCol == null)<br />
cursorMovingCol = new System.Windows.Forms.Cursor(GetType(), "DataListTest.movingCol.cur");
I get the following:
An unhandled exception of type 'System.ArgumentNullException' occurred in system.windows.forms.dll<br />
<br />
Additional information: Value cannot be null.
--------------
Will Pittenger
|
|
|
|
|
Ooh, it's static? Didn't know that!
Then this.GetType doesn't work...
static Cursor Getcursor{<br />
System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("movingCol.cur");<br />
System.Windows.Forms.Cursor cur = new Cursor(stream);<br />
stream.Close();<br />
return cur;<br />
}
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|