|
1) You can do it - sort of - with reflection, but it is one heck of a sledgehammer to crap a simple nut. What you end up with will be longer than the code to do it directly, and a lot harder to read and understand.
If you really want to access a number of controls in a loop, can I suggest the simpler, elegant way of having a class level list of labels?
List<Label>myLabels = new List<Label>();
...
myLabels.Add(lblName1);
myLabels.Add(lblName2);
myLabels.Add(lblName3);
myLabels.Add(lblName4);
...
foreach (Label lab in myLabels)
{
lab.Text = txtName.Text;
}
2) Not sure what you mean here: are you trying to do the same as in (1) but for CheckBox? If so, then the same answer applies. If not, then what are you trying to do?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Well actually I mislead you on the first one it was like;
lblName1.Text = txtName.Text;
lblName2.Text = txtName.Text;
lblSurname1.Text = txtSurname.Text;
lblSurname2.Text = txtSurname.Text;
so seems like it is better for me to code it directly.
for the second one; after evaluating your answer, it seems like the solution for the second is the same with Q 1
|
|
|
|
|
Anyway, even though you've apparently changed what you need to do, what you originaly asked for *is* possible, relatively easily done, and only moderately dangerous.
for (int i = 0; i < 4; i++)
{
(FindControl("lblName" + i.ToString()) as Label).Text = txtName.Text;
}
Pretty sure, anyway.
|
|
|
|
|
Actually yeah that is what I tried to mean. That should do it thanks anyway though but why is it dangerous?
I am guessing ;
During looping, when cant find a matching label name, it will throw an exception?
|
|
|
|
|
Yep. There had better be controls to match the loop variable number, or ka-boom!
Also: if, for whatever reason, you ever need the controlks to be set in a certain order...don't count on it. The compiler will do the loop in whichever order it feels is best.
|
|
|
|
|
Not tested, however I expect this should suffice:
for (int i = 0; i < 4; i++) FindControl("lblName" + i).Text = txtName.Text;
as each Control has a Text property, so no need to cast; and ToString() is implicit when appending to a string.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: as each Control has a Text property, so no need to cast
It won't compile.
'System.Web.UI.Control' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)
Or is there a switch somewhere to "relax" this?
|
|
|
|
|
I wrote: as each Control has a Text property
Sorry, that was wrong. While true for WinForms, it ain't true for System.Web.UI so the cast is required!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Regarding 2), I would think you could put something like this in the code behind:
foreach ( Control c in this.Controls )
{
if ( c is CheckBox )
{
( ( CheckBox )c ).Checked = false;
}
}
Jack of all trades ~ Master of none.
|
|
|
|
|
Hve you consodered For Each TextBox ?
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
No no.
1. For Each is VB, not C#
2. Form.Controls holds all top-level Controls of the Form, no matter what type they are; if one of them isn't a TextBox, foreach will fail to cast it and throw an exception, it does not enumerate just the derived type one is interested in!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
hi All, I am having trouble doing some string manipulation. I get a string with coma delimited. I want remove the first portion of the string.
for example,strMessage = " I, Have, a, string, that, describe, something"
what I want to do is that remove the first coma delimited part of the variable strMessage so it look like this
"Have, a, string, that, describe, something" how do I do this. Any help appreciated.
then I insert the string in my datagridview.
datagridview.Rows.Insert(0,strMessage.Split(','))
I want to keep this style of inserting into dataGridView just want to remove the first portion of the string which is "I".
thanks
|
|
|
|
|
string.Split() is the method you want; it has a few overloads, so I refer to its documentation.
BTW: you won't solve problems any faster by putting as much as possible into a single statement. Split things up, and look at the intermediate values while debugging.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
this is quite simple for the case you stated. That is just the first comma.
Use IndexOf. Something like:
str.Substring(str.IndexOf(",") + 1).TrimStart();
All the best,
Dan
|
|
|
|
|
Here[^] is the solution to all your problems.
(Well the ones about splitting, personal probelms are chargeable).
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
string[] parts = strMessage.Split(",");
for (int i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Trim();
}
dataGridView.Rows.Insert(0, parts);
But don't you need to ensure that the number of parts matches the number of columns you have in the row?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Hi,
I generate an Xml File with XmlTextwriter.I saved it in a known path (C:\Documents and Settings\My Documents\Visual Studio 2008myprojects\MyFile.xml). I want now to let the user choose where he wants to save his xml File.
How can I import the data from my xml File to copy them in the specified file choosen by the user of my application????
Thank u very much
modified on Friday, March 11, 2011 5:33 AM
|
|
|
|
|
Good question.
Here is how i did. Suppose user selects the path like this :
Path="C:\MyFile.xml";
Suppose you have an object of xmlDoc containing the xml.
So now we have to create a Filestream for the new path, and then use the StreamWriter class to write the xml to the new path.
FileStream fs=new FileStream(path,FileMode.Create,FileAccess.ReadWrite);
StreamWriter sw=new StreamWriter(fs,Text.Encoding.UTF8);
sw.Write(xmlDoc.OuterXml);
sw.Close();
fs.Close();
Hope it helped!
People with high attitude deserve the standing ovation of our highest finger!
My Technical Blog![ ^]
|
|
|
|
|
Hi,
I don't understand what u mean xmldoc.outerxml??? xmldoc is is my xml document?
ty
|
|
|
|
|
xmlDoc is an object of XmlDocument. It will contain your MyFile.xml .
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(@"C:\Documents and Settings\My Documents\Visual Studio\MyFile.xml");
and xmlDoc.OuterXml will give you the whole contents of the xml which will be used to write to the new file.
People with high attitude deserve the standing ovation of our highest finger!
My Technical Blog![ ^]
|
|
|
|
|
Hi Tarun,
Fine my file is well copied but the problem all tags appear in the same ligne.
How can i get an organized xml file well organized (tags organized)like this:
<GrpHdr>
<MsgId>DOSSIER1</MsgId>
<CreDtTm>2011-03-07T10:39:29</CreDtTm>
<BtchBookg>True</BtchBookg>
<NbOfTxs>2</NbOfTxs>
<CtrlSum>5534,4</CtrlSum>
<Grpg>MIXD</Grpg>
<InitgPty>
<Nm>Dossier1</Nm>
</InitgPty>
</GrpHdr>
As I conclude, it reads them string.
How can I solve that?
ty
|
|
|
|
|
I will get back to you on this as soon as i find the solution.
People with high attitude deserve the standing ovation of our highest finger!
My Technical Blog![ ^]
|
|
|
|
|
Hi,
even if I use Text.Encoding.UTF8 the file still not organized like an xml File format.
Thank u for u help on this (still waiting for u help )
|
|
|
|
|
hello Turan,
I found a solution. i just add the text.encoding UTF-8 to my code:
this.Response.ContentEncoding = Encoding.GetEncoding ("UTF-8");
I open my file with an Xml editor all works fine and my File is well structred.
Thank u a lot for u help and I am now sssssssssssssssso happy.
|
|
|
|
|
Awesome!
People with high attitude deserve the standing ovation of our highest finger!
My Blog![ ^]
|
|
|
|