I have a user control which contains a label called label1 , and I insert the user control into a panel using this code:-
var myControl = new WindowsFormsApp4.library();
panel1.Controls.Add(myControl);
So, the Label that is inside the user control will be registered into the panel.
How do I write a code that gets label1.Text value while the label is not generated into the panel yet?
Edit: I tried to use this code:-
var myControl = new WindowsFormsApp4.library();
string s = myControl.label1.Text
but it doesn't work because it creates a fresh user-control while the user control that is inserted into the panel value is changed in runtime.
Store your control in field instead of a local variable
public partial class Form1 : Form
{
MyControl _myControl; // Use the real type name for your control here.
public Form1()
{
InitializeComponent();
}
...
}
Then create it with
_myControl = new WindowsFormsApp4.library();
panel1.Controls.Add(_myControl);
and access it with
string s = _myControl == null ? "" : _myControl.label1.Text;
Related
I`m trying to modify on job the Text of a label (label.Text) that I have previously added to a panel (MyPanel.Controls.Add(MyLabel).
I add the label to the panel in a function:
public PanelEx nameoffunction()
{
.
.
MyPanel.Controls.Add(MyLabel);
return MyPanel;
.
.
}
MyPanelWithControl = nameoffunction();
Now I have in MyPanelWithControl the panel with a label. How can I now access to the label previously added to modify one of its fields?
You can find a control using its Name property:
MyLabel.Name = "label1";
MyPanel.Controls.Add(MyLabel);
...
MyPanel.Controls["label1"].Text = "updated text";
However, this approach can lead to problems later if you make changes to your user interface. If you moved the label to another panel, or changed its name, then the code which tries to find it using the name would still compile but cause an error at runtime. For long-term projects it's preferable to store references to controls as properties:
public class PanelEx : Panel {
...
public Label MyLabel { get; set; }
}
public PanelEx nameoffunction() {
...
MyPanel.MyLabel = MyLabel;
return MyPanel;
}
And then you can access the Label directly from the panel object:
MyPanel.MyLabel.Text = "updated text";
I have a C# Form that prints multiple instances of a User Control. Let's say that the form prints 5 instances of the User Control (Please see the link attached). How can I store/save the data inputted in all User Controls? Thanks
Here is the screenshot of the C# Form:
You'll have to store the User Controls when you instantiate them in a List or something.
You could have a class like this:
class SomeUC : UserControl
{
public SomeUC()
{
}
// A public method.
public string GetData()
{
return textBox1.Text;
}
}
Where textBox1 is the Name of a TextBox in your SomeUC
And then inside your main or something.
// Instantiate a List that will hold your UserControls, this has to be outside all methods
List<SomeUC> list = new List<SomeUC>();
// And now when you want to build your UCs
// Instantiate your UserControl
SomeUC uc1 = new SomeUC();
// Store your UserControl in a List or something (Can't help you with that)
list.Add(uc1);
Add as much as you want.
A List is not the only way you can do that, but since you don't know how many UserControls you're going to build beforehand, it makes since to use a List.
And then you can access them from the list by their index.
SomeUC uc1 = list[0];
string data = uc1.GetData();
This is an example of accessing one control (the TextBox) in your SomeUC. For other classes (such as the ComboBox) the interaction is different. Meaning you won't have a Text property in the ComboBox. You'll have to figure out things like that on youself. A little research is what it takes. You can always come back if you couldn't find a solution for something.
You can create a property like this for each item in user control.
public string DG
{
get
{
return txtDG.Text;
}
set
{
txtDG.Text = value;
}
}
Then you can access the control value by using following line in your form.
supposed you have created a usercontrol MyControl and you have placed some object of this control in FlowLayoutPenal (pnlFLP).
To get value from control
string DG = ((MyControl)pnlFLP.Controls[0]).DG;
To set value in control
((MyControl)pnlFLP.Controls[0]).DG = "1";
Try this code for accessing user control in the page
Dim txtName As TextBox = TryCast(UserControlName.FindControl("txtName"), TextBox)
In my project, I used a Windows Form and user controls. There is a panel on the form. And I put user controls on it.
When I click a button on the user control, I want to load another user control on the Windows Form panel. I set the panel's modifiers public.
The code I tried is below, this are the used variables:
myusercontrolpage1: this is my first user control
myusercontrolpage1: this is my second user control
FrmMain: this is my main form
pnlOrta: this is my panel I load user control in it
This is the code, which is not working:
Userclasses.myusercontrolpage1 page1 = new Userclasses.myusercontrolpage1();
Userclasses.myusercontrolpage2 page2 = new Userclasses.myusercontrolpage2();
FrmMain pnl = new FrmMain();
pnl.pnlOrta.Controls.Clear();
pnl.pnlOrta.Controls.Add(page2);
pnl.pnlOrta.Dock = DockStyle.Fill;
pnl.pnlOrta.BringToFront();
When I click a button on the user control, I want to load another user control on a Windows Form panel.
How can I access form's panel from user control and load another user control?
EDIT:
I replaced this:
FrmMain pnl = new FrmMain();
to
FrmMain f = (FrmMain)this.ParentForm;
This worked.
I'm not sure if I understand what you're trying to do, but answering this part of your question:
How can i access form's panel from
usercontrol...
If you know your UserControl is going to be load on Panel, then you can use .Parent property. You can do something like this in your UserControl (suppose it has a button):
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Panel pnl = this.Parent as Panel;
if (pnl != null)
{
pnl.BackColor = Color.Red;
}
}
}
I am really new to ASP and C#.
I have a User control which has Two Radio Buttons, and and Image control.
I want to load this control dynamically on click of a button and at the same time give ImageURL to the image control.
FileUpload is on the aspx page.
Can anyone help me??
Control MyUserControl = LoadControl("MyControl.ascx"); PlaceHolder1.Controls.Add(MyUserControl);
I was able to load the user control.
Now how to provide the imageURL.
Thanks in advance.
public partial class MyUserControl:UserControl
{
public string ImageUrl
{
set{ image1.ImageUrl=value;}
get{return image1.ImageUrl;}
}
}
var ctrl=LoadControl("MyControl.ascx") as MyUserControl;
if(ctrl!=null)
{
ctrl.ImageUrl = "image.mpg";
}
Expose the image URL in your MyControl.ascx as public
Then casting MyUserControl to your usercontrol and assign the image
example :
Control MyUserControl1 = LoadControl("MyControl.ascx");
MyUserControl temp = (MyUserControl) MyUserControl1;
temp.ImageURL = "urlhere.jpg";
I have created a WindowsFormControlLibrary porject. It works fine, I can drop it on the forms,call its methods,etc ...
but now as a property of it, I am passing the name of a Label to it. and I want this custom control to be able to use that label name and for example change its font to bold .
so the question is that if I have a WinForm and I have a Label on that form and my custom control on that form, then how can I tell my custom control to do something with that label which I am passing its name to it?
Instead of sending in the name of the label, send in a reference to the actual label and then the custom control can both read the name if it needs to and change the label's font and other properties.
Be careful though, it can quickly get messy to keep track of what's happening if various forms and controls change controls on other forms etc.
Edit: Added code to do what you ask for in the comments
Code isn't tested so it might not be completely correct, but something similar to this should work.
foreach (Control c in Parent.Controls)
{
if (c is Label)
{
Label l = (Label)c;
// do stuff to label l
}
}
First, if you wish to access a Control from your UserControl, you will need to use the FindForm() method.
Second, you will be required to expose your TextBox control, for example, through a property of your form.
Then, you would need to know the type of this Form returned by this FindForm() method.
Once you know it, you need to type-cast this result to the correct type.
So, here a sample untested pseudo-code to give you the idea:
public partial class MyMainForm {
private TextBox textBox1;
public MyMainForm() {
textBox1 = new Textbox();
textBox1.Name = #"textBox1";
textBox1.Location = new Point(10, 10);
textBox1.Size = new Size(150, 23);
this.Controls.Add(textBox1);
}
public Font MyTextBoxFont {
get {
return textBox1.Font;
} set {
if (value == null) return;
textbox1.Font = value;
}
}
}
Then, assuming you have dropped your control on your form, your UserControl could have a property like so:
public partial class MyUserControl {
private Form GetContainerForm {
get {
return this.FindForm();
}
}
// And later on, where you need to set your TextBox's font:
private void SetContainerInputFieldFont(Font f) {
if (GetContainerForm == null) return; // Or throw, depending on what you need to do.
((MyMainForm)GetContainerForm).MyTextBoxFont = f
}
}
cool :) I just added a get set public property of type Label... it automatically lists all the label on the form.