|
The WinForms ControlCollection does allow you to use a string Key to access members:
pn.Controls["txtProdName3"].Text = "hello"; When you set the 'Name property of a WinForm Control, you have created the Key (why MS documentation calls it a 'Key baffles me).
So, that is a way you could access the run-time created TextBoxes without keeping a direct reference to them in some data structure.
However, I strongly advise you not to rely on that, and suggest you do something like this:
public List<TextBox> ListOfTextBoxes = new List<TextBox>();
public void createTextBoxes(int howMany)
{
TextBox txtProdName;
TextBox txtProdDesc;
for(int i = 0; i < howMany; i++)
{
txtProdName = new TextBox();
txtProdDesc = new TextBox();
ListOfTextBoxes.Add(txtProdName);
ListOfTextBoxes.Add(txtProdDesc);
txtProdName.Name = "txtProdName " + i;
pn.Controls.Add(txtProdName);
txtProdDesc.Name = "txtProdDesc " + i;
pn.Controls.Add(txtProdDesc);
}
}
ListOfTextBoxes[2].Text = "blah";
pn.Controls["txtProdName3"].Text = "hello";
string tbContent1 = ListOfTextBoxes[0].Text; But, if you are going to use the Index to access the TextBoxes, you can see immediately that you'll have to remember which index to use for TextBoxes that are 'txtProdDesc, and which to use for 'txtProdName.
To make the code clearer, I would use two separate Lists. I would also consider using two Dictionaries of Type <string, TextBox>
If the two TextBoxes are designed to function as a "pair;" I'd consider another data structure, or consider creating a custom UserControl ... all depends on your Application and its scope, purpose, run-time load, etc.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 15-Oct-15 14:53pm.
|
|
|
|
|
Gentlemen,
I wanted to thank you for your help. Both of you got me going again.
|
|
|
|
|
I have a project that I have on TFS online. When Im trying to build the project, I get the following error:
Severity Code Description Project File Line Error The "Microsoft.CodeAnalysis.BuildTasks.Csc" task could not be loaded from the assembly C:\Users\Bryan\Source\Workspaces\TestProject\ContosoUniversity\packages\Microsoft.Net.Compilers.1.0.0\build..\tools\Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'file:///C:\Users\Bryan\Source\Workspaces\TestProject\ContosoUniversity\packages\Microsoft.Net.Compilers.1.0.0\tools\Microsoft.Build.Tasks.CodeAnalysis.dll' or one of its dependencies. Could not find the file. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. ContosoUniversity
Has this something to do with that Azure don't support ASP.NET 4.6?
|
|
|
|
|
Hi all.
I have a problem. really need some help.
I have a form contains 2 usercontrols name: usercontrol 1 and usercontrol 2. I use System.Timers.Timer.Elapsed to redraw usercontrol 1 and usercontrol 2 follow interval(interval of usercontrol 1 = 100ms. interval of usercontrol 2 = 1000ms). I measure time to draw usercontrol 1 and usercontrol 2 and I realize that time to draw usercontrol 1 is increase when usercontrol 2 has been drawing, when usercontrol 2 complete, time to draw usercontrol 1 is decrease. Is there anyway to solve it(time to draw usercontrol 1 is stable, it's not depend on the drawing usercontrol 2).
modified 14-Oct-15 23:20pm.
|
|
|
|
|
Why are you using a timer to re-draw ? Is this a game ?
Normally, if there's custom drawing being done in a UserControl (or other container), this is done by using the 'Paint event.
Please tell us what your goal is here. Is there any dependency (like accessing the same Database) between the two UserControls ? Are both of the UserControls instances of the same UserControl Object, or do they both inherit from a common Class or Interface ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
I create a usercontrol name: panel1, use timer.Elapsed += OnTimerTick; OnTimerTick will call draw a function. I add 2 panel1 name: panel11 and panel12 into a form. usercontrol 1 draw into panel11, usercontrol 2 draw into panel12.
modified 15-Oct-15 4:33am.
|
|
|
|
|
Hi, You still haven't answered the question about why you are using Timers to control painting/drawing, and what it is your painting/drawing. Please be specific.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
I want to redraw usercontrol 1 and usercontrol 2 follow cycle(usercontrol 1 and usercontrol 2 always redraw follow Timer.Interval of each control), so I use Timer.Elapsed event to do that.
It draws line, text, ...
Sorry for bad English.
|
|
|
|
|
Perhaps if you could show us a cut down version of the paint code for both controls.
|
|
|
|
|
usercontrol 1 will invoke 2 functions, one of it:
public void DrawControl(Graphics g)
{
int X = 599;
int Y = 10;
int Width = 225;
using (GraphicsPath path = GraphicsHelper.Create(X, Y, 224, 119, 5))
{
using (Pen frameWithBlackBgrd = new Pen(CWColors.FrameWithBlackBgrd))
{
g.DrawPath(frameWithBlackBgrd, path);
}
}
if (GuiResInfoMng.Instance.Lang != null && GuiResInfoMng.Instance.Param != null)
{
using (SolidBrush fixedTextWithBlackBgrd = new SolidBrush(CWColors.FixedTextWithBlackBgrd))
{
g.DrawString(GuiResInfoMng.Instance.Lang.GetStr(LangId.IDS_COM_TOTALCNT),
FontFactory.Instance.GetFont(LangId.FNT_M2), fixedTextWithBlackBgrd, X, Y);
g.DrawString(GuiResInfoMng.Instance.Lang.GetStr(LangId.IDS_COM_PASS), FontFactory.Instance.GetFont(LangId.FNT_M2),
fixedTextWithBlackBgrd, X, Y + 23);
g.DrawString(GuiResInfoMng.Instance.Lang.GetStr(LangId.IDS_COM_NG), FontFactory.Instance.GetFont(LangId.FNT_M2),
fixedTextWithBlackBgrd, X - 1, Y + 46);
}
}
using (SolidBrush variableText = new SolidBrush(CWColors.VariableText))
{
g.DrawString(ParamHelper.GetParamStr(ParamId.TOTALCOUNT).Trim(), FontFactory.Instance.GetFont(LangId.FNT_M2), variableText, X + Width - 60, Y);
g.DrawString(ParamHelper.GetParamStr(ParamId.PASSCOUNT).Trim(), FontFactory.Instance.GetFont(LangId.FNT_M2), variableText, X + Width - 60, Y + 23);
g.DrawString(ParamHelper.GetParamStr(ParamId.NGCOUNT).Trim(), FontFactory.Instance.GetFont(LangId.FNT_M2), variableText, X + Width - 60, Y + 46);
}
}
|
|
|
|
|
That's the paint routine for one control, not two.
|
|
|
|
|
usercontrol 2 will invoke draw function:
public void DrawGraph(Graphics g)
{
g.SmoothingMode = SmoothingMode.HighSpeed;
_frameXBar.DrawBackground(g, XbarPos);
_frameNumber.DrawBackground(g, NumberPos);
_frameRS.DrawBackground(g, RsPos);
DrawXBarBase(g);
DrawXbarGraph(g);
DrawRSBase(g);
DrawRSGraph(g);
DrawNumber(g);
}
DrawGraph function will call some function:
Ex:
private void DrawRSGraph(Graphics g)
{
if (IsAllLine)
{
if (IsR)
{
if (IsFullLine)
{
DrawPolyline(_dataAllLineRFullNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
else
{
DrawPolyline(_dataAllLineRPassNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
}
else
{
if (IsFullLine)
{
DrawPolyline(_dataAllLineSFullNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
else
{
DrawPolyline(_dataAllLineSPassNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
}
}
else
{
if (IsR)
{
if (IsFullLine)
{
DrawPolyline(_dataForLineRFullNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
else
{
DrawPolyline(_dataForLineRPassNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
}
else
{
if (IsFullLine)
{
DrawPolyline(_dataForLineSFullNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
else
{
DrawPolyline(_dataForLineSPassNumber, g, _frameRS.Width, _frameRS.Height, CalculateHeightRS, RsPos);
}
}
}
}
|
|
|
|
|
Hi, I'd like to suggest you now go back to your original question here and edit it so it includes the information/code you've shared.
cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
How many "cores" does your CPU have? Getting the most out of multi-threading (usually) requires multiple cores.
You want control 1 to be "stable" ... What about control 2?
If control 1 can be stable at the expense of control 2, then you could implement a "priority" system where control 1 is allowed to preempt control 2 (for drawing), but control 2 is not allowed to preempt control 1. There are various ways of blocking and testing for locks to accomplish this.
Be aware that "drawing" has always been a fairly slow operation relative to compute time (that's why it is not wise to update a "progress bar" too frequently when it comes to critical operations).
Also, 100ms is (usually) the limit at which a user can detect "lag" in a UI; you might consider raising the interval for control 1 to 200ms and checking the results.
And, since the interval of control 2 is a multiple of control 1, you might consider using a single timer, and "dispatching" control 2 once for every n times control 1 is dispatched; i.e. dispatch either control 1 or 2, but not so that both are running at the same time (when redrawing).
|
|
|
|
|
Thank for your reply.
My cpu has 2 core.
The first I want control 1 to be stable, control 2 still not care.
I measure time to draw control 1, AVG value = 15ms-> 20 ms, usually it ~ 10 ms, however when control 2 has been drawing, time to draw control 1 increase up to ~ 200ms. When control 2 complete it decrease down to ~ 10ms.(I want to both control are running at the same time, the drawing of controls is independence)
|
|
|
|
|
Your measurements seem to indicate that the controls cannot run at the same time if you expect control 1 to always draw at under 100ms without considering what control 2 is doing (or vice versa).
|
|
|
|
|
I can confirm that both control are draw at the same time.
|
|
|
|
|
|
I have a 20 records in a database table and I want to read a field in each record fieldA. For each record that fieldA contains data I want to do a count of that field. So if 10 records have data in fieldA then my count for fieldA will be 10.
Anyone know how to do this in C# ?
|
|
|
|
|
You can probably use some clever SQL to do it; try the database forum.
|
|
|
|
|
Well, assuming you are connecting to a database and want to use straightforward SQL, you could just execute the following scalar command:
SELECT COUNT(1) FROM MyTable WHERE FieldA IS NOT NULL So, you would have something like
private int GetCommand(IDbConnection dbConnection)
{
IDbCommand command = dbConnection.CreateCommand();
command.CommandText = "SELECT COUNT(1) FROM MyTable WHERE FieldA IS NOT NULL";
command.CommandType = CommandType.Text;
dbConnection.Open();
object output = command.ExecuteScalar();
dbConnection.Close();
int retValue;
int.TryParse(output, ref retValue);
return retValue;
}
|
|
|
|
|
Pete O'Hanlon wrote: int.TryParse(output, ref retValue);
That's not going to compile - TryParse expects the first parameter to be a string , not an object .
It should be safe to unbox the output variable directly to an int . Otherwise, Convert.ToInt32 should work.
Also, it would probably be a good idea to wrap the IDbCommand in a using block, and test whether or not you need to open the connection, since it might already be open.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Also, the CommandType "text" does not need to be set, it is (very conveniently) the default; since it is not a nullable enum it will always have a value, meaning a default must exist.
..but still bonuspoints, as the code is not tied to a specific IDbCommand implementation, and he's using the factory-method to create a command of the correct type
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
To be fair, I just coded this in the textblock as a minimal version. It's been a long time since I went away from the Db libraries I've built up that hide all this implementation.
|
|
|
|
|
As Richard says, this is easiest in SQL:
SELECT SUM(CASE WHEN fieldA IS NOT NULL THEN 1 ELSE 0 END) FROM MyTable
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|