|
Yes, if that doesn't work, then this code just plain is not running, or something else is strange on your form.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Both examples work under a button click event.
<br />
foreach (Control txt in this.Controls)<br />
{<br />
if (txt.GetType() == typeof(System.Windows.Forms.TextBox))<br />
{<br />
TextBox txtbx = (TextBox)txt;<br />
txtbx.Text = "";<br />
}<br />
} <br />
<br />
<br />
for (int i = 0; i < this.Controls.Count; i++)<br />
{<br />
if (this.Controls[i] is TextBox)<br />
{<br />
this.Controls[i].Text = "";<br />
}<br />
}
Difference between the two is that the first refers to a Control object. So you need to cast it to the TextBox control to gain access to the TextBox property?. Otherwise your setting the text of the control. Check it out in the debugger! In the second you reference the actual object and this exposes the objects, in this case a TextBox, .Text property.
Hope this helps.
Just because we can; does not mean we should.
|
|
|
|
|
Also, 'it won't work' is not really a question. What does it do ? Does it compile ? What happens when you step through it in the debugger ? I suspect your real problem is not casting, as Control has a Text property, but you've not really done enough work to tell us what is going on, or to try and work out what's really happening.
My best guess is that this code is not running at all.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I am using the same thing on the listbox selection committed and it works..
I set some break points to see if it is doing anything.. I am not getting any errors at all.
I have about twenty textboxes and I see that it is clearing one text box. But it is not clearing any of the other ones.
|
|
|
|
|
actually it is clearing that one textbox and every item in a ListBox???
|
|
|
|
|
Hi
Yes u way is rite,but chaneg ur code like this
foreach (Control txt in this.Controls)
{
if (txt as TextBox)
{
txt.Text = string.Empty;
this.Refresh();
}
}
I think this should be work for u.
thanks
|
|
|
|
|
That is doing the same thing... it clears a listbox, and a richtextbox but none of the regular text boxes
That one Textbox that it was clearing with the other codes I changed to a Label, because that is the only one that I wanted not to clear lol
So that is clearing all the items in a listbox, and the richtextbox, but not any of the other ones. I don't know whats going on.. I don't know what to see you.. I posted the exact code for the button click event
|
|
|
|
|
Hi
Please give a clear picture ,wat are u trying?
once time say listbox, after say textbox?
So, So tell me, wat u r trying?
thanks
|
|
|
|
|
Ok.. I have a button that is suppose to clear EVERY textbox on my form. That is what I am trying to accomplish! The buttons code is this:
This is EXACTLY what I have:
private void button10_Click(object sender, EventArgs e)
{
foreach (Control txt in this.Controls)
{
if (txt is TextBox)
{
txt.Text = string.Empty;
this.Refresh();
}
}
Phone2Listbox.Items.Clear();
phoneListbox.Items.Clear();
AuthPhone1Listbox.Items.Clear();
AuthPhone2Listbox.Items.Clear();
AuthPhone3Listbox.Items.Clear();
MedAllergiesListbox.Items.Clear();
MedPhoneListbox.Items.Clear();
EmergPhoneListbox.Items.Clear();
PaidToDateListbox.Items.Clear();
richTextBox1.Clear();
othercomments.Clear();
}
I am sorry that I said it was clearing a listbox, that is the code I have above that is clearing the textbox, and richtextbox. I'm tired, so sorry I said that.
The point is, is that it is not clearing the textboxes.. When I load my form I just type random crap in the text boxes and click my button (Clear All) and it doesn't clear the textboxes..
By the way I can't do:
if (txt as TextBox)
It has to be:
if (txt is TextBox)
The reason is, is because when you use the txt AS textbox it throws an exeception:
" Cannot implicity convert type 'System.Windows.Forms.Textbox' to 'bool' "
|
|
|
|
|
OK cool
can can
i'm directly typed that code in that post,and this is web applications?
|
|
|
|
|
I'm not sure whats going on.. I don't understand why it is clearing the listbox and the richtextbox and not any of the other textboxes. Which it is suppost to see that it is a TextBox and for some reason it is clearing all the items in a listbox.
I've got to go to bed, sorry guys! Thanks for trying to help me out.. I will mess with it more tomorrow night after banging my head on the wall a couple of times lol.
|
|
|
|
|
Next time, step through it in the debugger and let us know what you see. That's the missing ingredient here, that's why we can't work out what's going wrong.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Ok I set break points. This is what going on for this code:
foreach (Control txt in this.Controls)
{
if (txt is TextBox) <-- BREAK HERE * txt is Textbox | false *
{
txt.Text = string.Empty;
this.Refresh();
}
}
It is saying false for every control that it loops through? That is all I can tell you because that is all I know..
|
|
|
|
|
You know, all the textboxes are in Groupboxes.. does that make a difference?
Because it seems to be noticing all the Groupboxes but none of the textboxes
yes that is it! dang sorry for wasting your guys time! I can't just do:
this.Controls
I have to do this.[groupbox name].Controls
But why is this?
|
|
|
|
|
OK
i try now in my VS2005, and this is that
private void button1_Click(object sender, EventArgs e)
{
RecursiveClearTextBoxes(this.Controls);
}
private void RecursiveClearTextBoxes(Control.ControlCollection cc)
{
foreach (Control ctrl in cc)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
tb.Clear();
else
RecursiveClearTextBoxes(ctrl.Controls);
}
}
hope finish of ur bug.
|
|
|
|
|
Hi,
I am starting the development of a chat server/client application and i would like to know how to call some kind of "world timer/clock".
I want my application both server and clients to share the same timer/clock.
Is there some cloth in the internet i can call when i start the server?
Thanks in advance
|
|
|
|
|
Hi
Nice Idea, Just use a common web service server for the client and server timer.
thanks
|
|
|
|
|
Hi All,
I've been trying various things for an hour now and can't crack this particular nut. I have this code that is throwing an enumeration exception:
string pathcategory = FileScore.OrderBy(input => input.Value).Last().Key;
foreach (KeyValuePair<string, int> kvp in FileScore)
{
FileScore[kvp.Key] = 0;
}
I think I see the problem. The first line is setting the order to the Value and then the foreach is attempting to iterate over the dictionary but each execution of foreach is changing the values that the enumerator is using ... how do I reset the ordering back to the key??
Thanks,
Jammer
Going where everyone here has gone before!
|
|
|
|
|
Just iterate over FileScore.Keys, then you won't be changing what you're iterating over, and it will work.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian,
Not sure that will work in this instance as its coded now. The dictionary is storing a list of possible matches to words in a string. Depending on the match i'm incrementing the value held against a key with potentially many matches to different words in the string, so I end up with a dictionary that might look like:
string1, 10
string2, 0
string3, 30
string4, 50
string5, 10
The line that sorts the dictionary is looking for and finding string4 and i'm passing that key back. Then I want to set all the values back to 0.
Would it be wiser to remove the sorting line and then just iterate over the dictionary holding the highest value in the key/value pair? Sorry if this is what you mean, just wanted to clarify.
Hope your well.
Jammer
Going where everyone here has gone before!
|
|
|
|
|
If you have a foreach and your code modifies what you are iterating over, it won't work. If you just iterate over the keys, I believe it will work.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Gotcha ... will have a look at that tonight.
Jammer
Going where everyone here has gone before!
|
|
|
|
|
I've just been testing this again and I've come to the conclusion that using a Dictionary as I am is a no go option ... no matter what I try there is an exception thrown.
foreach (KeyValuePair<string, int=""> kvp in FileScore)
{
if (FileScore[kvp.Key] > highestscore)
{
pathcategory = kvp.Key;
highestscore = kvp.Value;
}
FileScore[kvp.Value] = 0;
}
return pathcategory;</string,>
Even though the line that 'resets' the Value to zero isn't changing the Key the foreach throws an exception saying that the collection has changed. I think i'm going to use a normal generic list in its place.
Cheers,
Jammer
Going where everyone here has gone before!
|
|
|
|
|
Hi,
I encounter a very strange (at least to my understanding) behavior.
I have a Windows Forms program. In the program I use openfiledialog to choose some file.
Later on I save some data to an XML file using XmlDocument.Save().
When I write in the Save a specified path, e.g. "c:\\folder\\bin\\debug\\config.xml" it works fine.
But, when I write only a relative path: 'doc.Save("config.xml");' I get the strange behavior:
The config.xml file is save in the last folder the I visited when I used the openfiledialog control.
Wherever I visit with that control - the xml file is save into that folder.
I can't believe that this is the inteded behavior.
Did anyone encounter that?
Can it be something wrong in my code? If so - what king of possible mistake?
If it a bug of XmlDocument.Save() - how can I work around it?
Thanks in advance,
Itzik
|
|
|
|
|
It's not strange at all. When you don't tell the class where to save, it will save to the current directory. When you browse the file system, you're changing the current directory. Always tell a file where you want it to go, or you won't know where it is.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|