|
No-one with a LINQ version yet?
class Form1
{
IEnumerable<Control> summingLabels;
public Form1()
{
InitializeComponent();
summingLabels =
from c in Controls.Cast<Control>().AsQueryable()
where c.Name.StartsWith("sumLabel")
select c;
foreach(Control l in summingLabels)
l.Text = l.Name;
}
}
Keep a pointer around to the controls that you found; no need to iterate them every time
I are Troll
|
|
|
|
|
how about lambda?
foreach (Control l in Controls.Cast<Control>().Where(c => c.Name.StartsWith("sumLabel")))
l.Text = l.Name;
|
|
|
|
|
|
Or Maybe to make it nicer with no casting and type checking:
summingLabels =
from c in Controls.OfType<Label>()
where c.Name.StartsWith("label")
select c;
foreach(Label l in summingLabels)
l.Text = l.Location.ToString();
|
|
|
|
|
Yup, works, and more concise and readable
I are Troll
|
|
|
|
|
The correct answer is: don't do that
If you need multiple storage locations (variables) and access them by index, use an array.
If you really have multiple variables (e.g. because they are generated by the VS forms designer), then you can still put those into an array:
Label[] labels = { labela1, labela2, labela3 };
|
|
|
|
|
|
David Knechtges wrote: Is there a way to do that?
No, at least if I understand what you want... However, why would you want to do that? What's wrong with:
Label[] labela = {
labela0,
labela1,
labela2,
labela3,
};
for (int i=0;i<4;i++)
labela[i].Text = "something";
modified on Wednesday, September 1, 2010 5:55 PM
|
|
|
|
|
|
You can also try reflection if you really really really really really really really really must do it this way and not using array/collection (depending on a situation).
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Try to use this construction:
this.GetType().GetField("variablename")
This should work for c# but better try to avoid such kind of code. Other languages has different RTTI, for example in c/c++ this is impossible at all.
|
|
|
|
|
Not sure about the performance implications of FindControl.. But this one is pretty close to what you originally wrote
for (int i=1;i<4;i++)
(Controls.FindControl("labela" + i.ToString()) as Label).Text = "something";
|
|
|
|
|
This is not exactly what you were asking for but is an alternative using binding. This may be overkill for what you are trying to do but it would be more elegant. Bind your labels as you create them to an object of a class that implements INotifyChanged then when you change the property of the object each label will be updated without having to work loops.
http://stackoverflow.com/questions/639894[^]
Dave
|
|
|
|
|
Array.ForEach(new Control[] { labela1, labela2, labela3 }, delegate(Control c) { c.Text = "something"; });
|
|
|
|
|
How to do this is quite simple, but not obvious since we do not normally think of dimensioning a variable as a control type.
The simplest way (to me) is to dimension an array of labels, Label(1), Label(2) etc. as Label. Then set each member of the array equal to each of the labels you want to control a property for.
Then you can write a loop setting a property of each label in the array as an iterated function, such as:
Dim Label(1) as Label
Dim Label(2) as Label ...
Label(1) = LabelA
Label(2) = LabelB ...
For x = 1 to 5
Label(x).text = "Text " & x
Next
(Sorry, I am an old VB.NET programmer, but the concept is the same in all )
I think this is what you want, works great for me, and works for the other common controls, too.
|
|
|
|
|
|
There has to be tools in Visual Studio 2008 for outlook that we can add to my software to allow us to connect via outlook connection to exchange.... so , which one can do that?
|
|
|
|
|
drag and drop a lable whit c#.net
|
|
|
|
|
At it's most basic:
Capture the mouse down event. Use the mouse offset while moving the label round. Capture the mouse up event.
|
|
|
|
|
Yours is not a question at all.
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
|
|
|
|
|
Smithers-Jones wrote: Yours is not a question at all.
Sure it is. It says question in the title.
|
|
|
|
|
If a question at all, it's a questionable question.
|
|
|
|
|
Start here[^] if you're using WPF, or here[^] if you're building WinForms. There's code-examples for multiple .NET languages, and you can select the .NET version that you need by clicking the link at the top of the page.
--edit;
As an additional remark, not as criticism, but to help you get a better answer sooner; it would have helped if you had put the phrase "Drag and Drop" in the "subject"-line of your post. A few hints on your current environment (VB.NET, C++, .NET 1.1, .NET 4) are usually helpful too.
I are Troll
|
|
|
|
|
You didn't post a question at all.
But, if you want to know how to do it, read my article on the subject, here[^].
|
|
|
|
|
(i posted this incorrectly to quick answers. I am reposting this here)
I am working on my first C# SQL project. A simple checkbook register.
I referred to the CodeProject article:
Using the DataGrid Control Using the DataGrid Control[^]
I added a datagridview control to my form and set the data source as my checkbook register in SQL Express.
I am able to see all the checkbook entries when I run the form.
My first attempt at updating the sql table is generating a compile error.
The error is:
cannot convert from 'System.Data.DataSet' to 'FirstCheckBookInSQL.CheckBookManagementDataSet.CheckbookUSBPersonalDataTable'
I've been researching this for 3 days, but I'm just not making sense of what I am seeing.
Can someone help please!
Thank you.
Mark
My code in Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FirstCheckBookInSQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.checkbookUSBPersonalTableAdapter.Fill(this.checkBookManagementDataSet.CheckbookUSBPersonal);
}
private void TBDbutton_Click(object sender, EventArgs e)
{
DataSet myChangedDataSet = this.checkBookManagementDataSet.GetChanges();
if (myChangedDataSet != null)
{
int modifiedRows = this.checkbookUSBPersonalTableAdapter.Update(myChangedDataSet);
this.checkBookManagementDataSet.AcceptChanges();
myChangedDataSet.AcceptChanges();
}
MessageBox.Show("no data changed");
}
}
}
</pre>
|
|
|
|