|
foreach (Control c in form.Controls)
c.Enabled = false;
Any remotely useful information on my blog will be removed immediately. There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't.
Judah Himango
|
|
|
|
|
thanks
There is no spoon.
|
|
|
|
|
That'd only work if your controls are nested only 1 level deep. To enable all controls in all containers contained with your container, you'll need to use recursion - not enumeration:
public void EnableAll(bool enabled)
{
this.Enabled = enabled;
EnableAllRecursively(this.Controls, enabled);
}
void EnableAllRecursively(Control.ControlCollection controls, bool enabled)
{
if (controls != null)
{
foreach (Control c in controls)
{
c.Enabled = enabled;
EnableAllRecursively(c.Controls, enabled);
}
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
Thanks to all.
Both techniques work.
In my case Judah's simple loop was the solution I've finally found.
But It is true that the simple loop work only at 1 level.
for nested windows, the recursive version is better...
Thanks to all again
There is no spoon.
|
|
|
|
|
Making such generalized assumptions is not a good practice. The recursive method would be best to implement now in case someday you add a composite control - like a UserControl - to your form. I merely adds an extra 1 or 2 calls to the call stack depending on the state, but still enumerates your first level controls as did the first example.
an even better approach and recursive in nature is to extend a control class and override Enable to also have it disable its children (some controls do this inherently, like the ComboBox ). Then when its children are disabled they disable their children. Now your code isn't responsible for its children and you have a better OO design.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Setting the .Enabled property on a control automatically sets the enabled property on all child controls, no?
Say you have 2 panels, and each panel has a button. Disabling the 2 panels will also disable the nested buttons.
Actually, perhaps a better solution would be to simply set the .Enabled property of the form itself, which should also disable any nested controls. Even if you explicitly turn on the Enabled property of a nested control, it will still be disabled if its parent is disabled.
Any remotely useful information on my blog will be removed immediately. There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't.
Judah Himango
|
|
|
|
|
Typically, yes. It depends on the control.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I don't know if you're talking about a windows app or a web app, but here goes:
Assuming all the controls in question can be enabled or disabled, then you'll have to recurse through the collection of controls from the top (form) level (since controls can contain other controls), so for each control found, you'll have to check its Controls collection.
Whenever you find a control, you'll have to make sure it can be enabled or disabled (meaning you'll have to cast it to its original type or a base type) before setting the enabled/disabled flag.
Please let me know if this isn't detailed enough - I can try to make some sample code, but I'm short on time right now.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
I'm having an issue with C# string literals. I'm trying to include double quotes in the string for a sql text. I've followed all the advice as to use either the backslash or "double double" quotes, but when I check the resulting output in the command window during debug, I get the backslahes showing up in the string.
code:
(backslash)
string s ="select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = \"MS\" from ld_det, pt_mstr where ld_part like \"" + spec + "\" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= \"\" order by ld__dec01, ld__dte01";
(@ sign and double doubles)
string s = @"select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = ""MS"" from ld_det, pt_mstr where ld_part like """ +spec+ @""" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= """" order by ld__dec01, ld__dte01";
output in command window(either way):
?sql
"select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = \"MS\" from ld_det, pt_mstr where ld_part like \"12406%\" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= \"\" order by ld__dec01, ld__dte01"
desired result:
"select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = "MS" from ld_det, pt_mstr where ld_part like "12406%" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= "" order by ld__dec01, ld__dte01"
Any ideas?
thanks!
Bill Polewchak
Technical Design Lead
Software Development/Architecture
Avery Dennison Corp.
|
|
|
|
|
Both of your examples will produce the same string. The debug window shows the backslashes because it is intended to display "special" characters in that way. When the string is sent to the database, the characters will be expressed as intended, not parsed for display. I don't know what kind of database you're using, but what you have should work in Access.
You might consider sidestepping the whole issue and using single quotes - I think they'll work in Access, and they're required by most other DB platforms instead of double quotes.
One other note: You might want to also consider using parameterized queries instead of putting everything into a single string. I don't know where the value for "spec" comes from, but if it is in any way visible to users, you're opening yourself to SQL injection attack. Also, parameterized queries can often be cached by the DB platform, improving performance.
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Thanks for promptly confirming my hopes! I was wondering whether or not this is a display issue in the command window.
Since this is being used in an oledb (ODBC.NET) object query to a Progress database in our ERP system, I needed to use the double quotes. The resident application db is SQL Server 2000, so in that case, I could have used single quotes, or as I'm more used to doing, using a SQLOLEDB command object to call a parameterized sp!!!
I actually had to do a bit of research to use this old school text method
Thanks again.
cheers,
Bill Polewchak
Team Leader
Software Development/Architecture
Avery Dennison Corp.
|
|
|
|
|
Hello,
I have a database application in my connection string l have the following.
OleDbConnection cnnTeacher = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Library\LibrarySystem.mdb;Persist Security Info=False");
But if want to install this program on another machine l don't want to have to change every connection string. Is there an easy way to do this.
The database is in the same folder that the solution is in.
Thanks in advance,
Steve
|
|
|
|
|
You could create a config (app.config or web.config depending on your project type) file for your application and expose the connection information there as separate variables (e.g. Provider, Data Source, Persist Security Info etc.) See the System.Configuration namespace for information about accessing the contents of config files (or check out the CMAB [Configuration Management Application Block] provided by MS).
This approach is favorable to hard coding the connection information for a number of reasons:
1. You, the developer, are free to change your database platform easily in your development environment.
2. Users of your application can place their datastore where they choose.
3. Changes may be made at any time, and will take effect immediately, and without requiring a recompile.
4. You can encrypt the authentication info in a config file if necessary.
Note: You may also want to include parameters for accessing secured Access databases. Even if you don't lock down the DB currently, you may want to in the future.
Alternate choices to using a config file include using the registry, or a text file.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Hello,
I managed to find a solution that worked. I placed the database where the exe file is, and used the Application.StartupPath to get the full path to where the database is.
Is that a good way to solve this problem when you create an installer, to be installed on another machines?
OleDbConnection cnnLogin = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + @"\LibrarySystem.mdb;Persist Security Info=False");
Many thanks in advance,
Steve
|
|
|
|
|
Hi,
I have a user control that includes two other controls, one is a Panel and the other a TextBox (or actually, a user control inherited from a TextBox). The Panel can be used for mouse input (the position of a mouseclick and so on) and the TextBox is there for numerical input. What I want to achieve is that the user can type in a number in the TextBox without having to explicitly give focus to the TextBox first. Preferably, I'd like the TextBox to be filled without ever recieving the focus. I've been able to redirect the key input through the OnKeyDown and OnKeyUp of the user control to those of the TextBox, but it doesn't seem to get processed when the TextBox doesn't have the focus. What I'm desperately trying to prevent is having to catch and interpret every key high-up, for instance in ProcessCmdKey, and then set the text in the TextBox myself. What you're doing then is essentially rebuilding the functionality of a TextBox, which doesn't seem like a good idea.
So I though a good compromise would be to let the user control 'listen' for any key input and take that as a sign that the user wants to use the TextBox. Upon the first encountered key I give the focus to the TextBox and redirect all key input to it. Unfortunately, when the very first key reaches the TextBox, it DOES have the focus but the key still isn't shown. Although the Focused-property of the TextBox is true, somehow it doesn't seem to realise this yet deep down. Calling the TextBox' OnKeyDown twice doesn't help. Only the second key I press gets displayed.
Does anyone know why this occurs or how to circumvent it? Or perhaps someone has an entirely different approach to achieve the effect I'm aiming for, basically a TextBox that can take raw keyboard input but that doesn't need focus.
Thanks.
|
|
|
|
|
I'm a newbie. So please be patient. I'm working on a C# app in compact framework for a PDA. It connects to a device and extracts and updates data. I need to double check with the user before I continue with the update. I would normally use a MessageBox with an OkCancelButton. However, the compact framework doesn't support but one overload: MessageBox(string). I need to send confirmation (bool) back to the form and act accordingly with the update. Could someone please head me in the right direction.
Thanks in advance for any and all help.
|
|
|
|
|
Actually, the Compact Framework support 3 overloads of MessageBox.Show. The one your looking for supports which buttons to dispaly and also returns which button was clicked. Look here[^] for the documentation on MSDN.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Right you are. The compiler was telling me that there were no such overloads because I had them in the wrong sequence.
Thanks a bunch for the help.
|
|
|
|
|
Dear forum:
I've stored .rtf file content into my MS-SQL DB using Image DataType.
Now I have problem to read & show it in a RichTextBox!
First I Fill my DataSet using a DataAdapter & use following code to show it
Byte[] Txt = (Byte[]) dataSet11.LetterSel.Rows[0]["Content"];<br />
MemoryStream ms = new MemoryStream();<br />
ms.Write(Txt, 0, (int)Txt.Length);<br />
richTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText);<br />
ms.Close();
but it has been useless yet! & giles "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection." error
Please help me in the last step of my App.
Look to hear from u ASAP.
Always ,
Hovik Melkomian.
|
|
|
|
|
|
Dear leppie:
Tnx for ur reply, but I tried it before! & didnt solve it. of course now it give another error "Invalid file format."
actually when I open a .RTF file & push it to my buffer, displaying it in RichTextBox has no problem, but when I store it in DB & read it from stored Data to show in RichTextBox gives me that error!
my code is lie belove:
string file = openFileDialog1.FileName;<br />
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);<br />
<br />
richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);<br />
<br />
byte[] buffer = new byte[stream.Length];<br />
stream.Read(buffer, 0, (int)stream.Length);<br />
stream.Close();<br />
<br />
DataRow TxtRow;<br />
TxtRow = dataSet11.LetterSel.NewRow();<br />
TxtRow["Number"] = 1;<br />
PicRow["Content"] = buffer;<br />
dataSet11.LetterSel.Rows.Add(TxtRow);<br />
AdapterLetter.Update(dataSet11, "Letter");<br />
buffer = null;<br />
<br />
dataSet11.Letter.Clear();<br />
AdapterLetter.SelectCommand.Parameters["Number"].Value = 1;<br />
AdapterLetter.Fill(dataSet11, "Letter");<br />
<br />
Byte[] Txt = (Byte[]) dataSet11.Letter.Rows[0]["Content"];<br />
MemoryStream ms = new MemoryStream();<br />
ms.Write(Txt, 0, (int)Txt.Length);<br />
ms.Seek(0, SeekOrigin.Begin);<br />
<br />
<big>richTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText);</big><br />
ms.Close();
any idea?!
Always ,
Hovik Melkomian.
|
|
|
|
|
|
if u mean removing ms.Close(); it was useless! by the way the problem is when I wanna set the RichTextBox content by RichTextBox.LoadFile method witch is before closing memory stream! & LoadFile gives error!
but when I use a fileStream it has no error! How can I write my MemoryStream in a fileStream (make a temp file) & then show its content in RichTextBox?!
of course its a bit dummy!
Always ,
Hovik Melkomian.
|
|
|
|
|
Hovik Melkomian wrote:
but when I use a fileStream it has no error!
maybe try .Flush() after write, i cant think of any reason it should not work. As a last resor, so the content both to a filestream directly and another via memory but also save to file, this way you can just run the file thru a diffviewer and see what is different.
top secret Download xacc-ide 0.0.3 now! See some screenshots
|
|
|
|
|
Dear leppie:
After some debuging & ... I got that my reading is OK, so when I was checking my save operation I saw that I didnt set my Stream position to 0! & so the buffer was full of empty chars & ...
now its ok & I really enjoynig my Paperless Application
That was ur idea (position) & tnx for ur idea & ur time!;)
Always ,
Hovik Melkomian.
|
|
|
|
|