The only way I could duplicate this was to scroll the panel - which makes sense, because the coordinates are relative to the top of the control, not the top of it's scrolled position.
Try including the scroll position in your location:
ComboBox txtRun = new ComboBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new Point(30, 18 + (30 * c) + panel1.AutoScrollPosition.Y); ;
panel1.Controls.Add(txtRun);
"can you explain sir what this will do
panel1.AutoScrollPosition.Y);
i am confused a little bit"
The Location of a control on any container is relative to the top left hand corner of that container - be it a form, or a panel, or a splitter. Otherwise it you would have to refer to the containing control TLHC when you set position, and all control locations would have to be updated when you moved the container.
A panel is an example of a ScrollableControl (i.e. it derives from that rather tna from the vanilla Control)
When you scroll it, the amount of pixels difference between the unscrolled position of a contained control and the amount it has moved relative to the TLHC of the container is given to you as the AutoScrollPosition - it is the offset between the on-screen position of the TLHC of the container, and the logical position. If you scroll it five pixels down, the Y coordinate of the AutoScrollPosition goes to -5.
The more you scroll the control down, the move negative the value in the relevant
All the code does is include the amount of the scroll, to make the offset relative to the real TLHC rather than the displayed TLHC.
Try it for yourself, and it will probably make more sense. Handle the Panel.Scroll event and add the following code:
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
Console.WriteLine(panel1.AutoScrollPosition);
}
As you scroll it, you will see the number change, and it should be clearer.
"sir can you tell me one more thing relating to dynamic validation
if i am using this code for validation
public bool valun()
{
if (combobox.Text == string.Empty)
{
return true;
}
else
{
return false;
}
}
public void validation_username()
{
bool stat = valun();
if (stat == true)
{
label1.visible =true
}
else
{
label1.visible =true
}
}
then how can i validate dynamic controlwhich i have added?"
You could add an event handler, and use the sender parameter to identify which comboBox it was. If you handle the Leave Event you can do it immediately the user exits the control for any reason:
txtRun.Leave += new EventHandler(txtRun_Leave);
panel1.Controls.Add(txtRun);
}
...
private void txtRun_Leave(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
}
}
Or you can do it on a button press, and iterate through the panels Controls in a similar way:
foreach (Control c in panel1.Controls)
{
ComboBox cb = c as ComboBox;
if (cb != null)
{
}
}
"sir but the first method of validation is not working in mine.
i have written this in the button click event
txtRun.Leave += new EventHandler(txtRun_Leave);
panel1.Controls.Add(txtRun);
and this
private void txtRun_Leave(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
label1.Visible = false;
}
else
{
label1.Visible = true;
}
}
as outside.. whats the problem there is no error coming"
No, no, no...
You need to learn what "as" does: it is a conditional cast operator. If the object being cast is of the appropriate type, then it is cast, and the reference is set into the variable. If it isn't, then
null
is assigned instead.
So, if the object
sender
is a ComboBox (or a control derived from ComboBox), then the reference to
sender
is assigned to
cb
. If it isn't, then null is assigned. The first test in the method is to make sure that the method has not been called from a button, or a DataGridView, or some other control.
Your code will always assign
false
to the
label.Visible
property if the handler is only ever added to a ComboBox control. You still need to check the ComboBox content!
"And stop adding controls to two different Controls lists ???
what does this means?
and i will clear my problem, lets start from start. i am making a combobox and label dynamically, label is set to visible = false. and just as same what you have done validation, when user dosent enter enything and leaves the combobox then the label is set to true. i just want to do that but the label will be dynamically generated, i have done this
Label label = new Label();
label.Name = "dynamiclabel" + c++;
label.Location = new Point(160, -5 + (20 * c));
label.Visible = false;
label.Text = "labelname";
ComboBox txtRun3 = new ComboBox();
txtRun3.Name = "txtDynamic" + c++;
txtRun3.Location = new Point(30, 18 + (20 * c));
this.Controls.Add(txtRun3);
this.Controls.Add(label);
txtRun3.Leave += new EventHandler(txtRun_Leave);
panel1.Controls.Add(label);
panel1.Controls.Add(txtRun3);
this code in the button click event and
private void txtRun_Leave(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
Label lb = sender as Label;
if (cb != null)
{
if (cb.Text == string.Empty)
{
lb.Visible = true;
}
else
{
lb.Visible = false;
}
}
}
but the error which is coming is this
"Object reference not set to an instance of an object."
where i am wrong."
"stop adding controls to two different Controls lists ???
what does this means?"
Look at your code:
this.Controls.Add(txtRun);
panel1.Controls.Add(txtRun);
And
this.Controls.Add(label);
panel1.Controls.Add(label);
In both cases you are adding the same control to two different Controls Lists - this.Controls (the form) and panel1.Controls (the panel). You only want to add it to the panel, not the form.
"where i am wrong."
If a Control is a ComboBox, then it can't also be a Label! So when you execute
ComboBox cb = sender as ComboBox;
Label lb = sender as Label;
You are guaranteed that one of the two variables will be null - so when you try to use both, you are certain to get the error.
A suggestion:
Did you know that every control has a Tag property? No? It's really handy when you want to relate two controls, because it can contain any object. If you set the Tag property of the ComboBox to the relevant Label, you could access exactly that one in your Leave event...
Label label = new Label();
label.Name = "dynamiclabel" + c++;
label.Location = new Point(160, -5 + (20 * c));
label.Visible = false;
label.Text = "labelname";
ComboBox txtRun3 = new ComboBox();
txtRun3.Name = "txtDynamic" + c++;
txtRun3.Location = new Point(30, 18 + (20 * c));
txtRun3.Tag = label;
private void txtRun_Leave(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
Label lb = cb.Tag as Label;
if (lb != null)
{
...