I am trying to change the label and text of a label which is inside a UserControl from within a Parent Form method at runtime.
So within the method in parent Form I do the following to change the label properties which is inside a UserControl
public partial class Form : Form
{
public void Form_Method()
{
UserControl uc = new UserControl();
uc.UpdateLabel(true);
}
}
And the custom method inside my UserControl
public partial Class UserControl : UserControl
{
public void UpdateLabel(bool value)
{
if (value)
{
lbl.Text = "This";
lbl.Forecolor = Color.Green;
}
if (value == false)
{
lbl.Text = "That";
lbl.Forcolor = Color.Red;
}
}
}
However when I navigated to the UserControl the label properties have not changed as I was creating a new instance of the usercontrol on the fly which
technically disappears after the method ends.
So I tried creating a public property of the actual UserControl as follows
public partial class Form : Form
{
public UserControl _uc;
public void Form_Method()
{
UserControl uc2 = new UserControl();
uc2.UpdateLabel(true);
_uc = uc2;
}
}
However it has no effect whatsoever? I have come across info of using Events or Delegates am not sure if they are the correct process for what am trying to do?
Related
I'm trying to changing a user control informations (labels, pictures etc.) from auto added user control. But i cant do it.
Here's my code;
private void KitapButton_Click(object sender, EventArgs e)
{
BıtıkForm BForm = new BıtıkForm();
BForm.kitapGoruntuleme.Visible = true;
}
public partial class BıtıkForm : Form
{
//create controls public instance
public Label label;
public BıtıkForm()
{
InitializeComponent();
//initialize the control
label = new Label();
}
}
Now you can access it from other place like;
BıtıkForm BForm = new BıtıkForm();
BForm.label.Visible = true;
/////// But my Suggestion do not do it like that instead do it like below ///////
BıtıkForm BForm = new BıtıkForm(controlVisible);//Pass the bool value as parameter to the constructor of form
BForm.Show();
And then in form
public partial class BıtıkForm : Form
{
public BıtıkForm(bool controlVisible)
{
InitializeComponent();
//Set Control Visibility
someControl.Visible = controlVisible;
}
}
I didn't use C# too much but It's eventually object oriented. The mistake I made is; I was creating a new instance of 'BıtıkForm' everytime event fired. It could be solved by adding new property where event belongs, and property will carry 'BıtıkForm' object. So It can be managed trough all over the program.
I have a UserControl which contains a TabControl. I enabled the designer of the TabControl and its TabPages so the user can add/remove/modify tab pages and add controls to tab pages.
When I add my UserControl to the Form and add controls on tab pages it's OK and controls can be added/removed/changed properly until I build the project or save changes and close and reopen the form.
When I build the project, every changes that I'd made in tab pages and each controls which I added to tab pages are removed. The controls exists on designer.cs but they doesn't show on tab pages.
Why do the controls I added to tab pages of the TabControl of my UserControl disappear after rebuild?
My user control source code:
Note: tbBody is a TabControl
[Designer(typeof(MainDesigner))]
public partial class MZTabControl : UserControl
{
private bool isdesign = false;
private bool allowtbodyresize = false;
public MZTabControl()
{
InitializeComponent();
}
[Category("MZ TabControl"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TabControl TabControlArea
{
get
{
return tbBody;
}
}
}
My designer source code:
public class MainDesigner : ParentControlDesigner
{
private DesignerVerbCollection dvc = new DesignerVerbCollection();
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
if (this.Control is MZTabControl)
{
this.EnableDesignMode(((MZTabControl)this.Control).TabControlArea,
"TabControlArea");
var uc = (MZTabControl)component;
foreach (TabPage tbpg in uc.tbBody.TabPages)
{
EnableDesignMode(tbpg, tbpg.Name);
}
}
}
}
Before rebuild or clean: Screenshot
After rebuild or clean: Screenshot
The designer of Form cant't see those TabPag controls which you created in the UserControl.
You can remove all items from TabPages collection of the TabControl in your UserControl. If you need the TabControl have some pages after adding to Form you can add some tab pages in InitializeNewComponent method of designer of your user control. Then user can change those pages or add/remove pages to the TabControl of your UserControl and all tabs and their contents will be serialized because this way the Form designer can see those tabs.
UserControl1
Create a public property for the TabControl and decorate it with DesignerSerializationVisibility by passing DesignerSerializationVisibility.Content as value to say the designer serialize the content of control:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(UserControl1Designer))]
public partial class UserControl1 : UserControl
{
public UserControl1() { InitializeComponent(); }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TabControl MyTabControl
{
get { return this.tabControl1; }
}
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.SuspendLayout();
this.tabControl1.Name = "tabControl1";
this.tabControl1.Dock = DockStyle.Fill;
this.Controls.Add(this.tabControl1);
this.Name = "UserControl1";
this.ResumeLayout(true);
}
private System.Windows.Forms.TabControl tabControl1;
}
UserControl1Designer
Enable the designer of TabControl using EnableDesignMode method of the designer. Also initialize the TabControl by adding 2 TabPage like the job that the original control performs. To do so, you should use IDesignerHost service like below.
public class UserControl1Designer : ParentControlDesigner
{
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
this.EnableDesignMode(((UserControl1)this.Control).MyTabControl, "MyTabControl");
}
public override void InitializeNewComponent(System.Collections.IDictionary values)
{
base.InitializeNewComponent(values);
AddTab();
AddTab();
}
private void AddTab()
{
TabControl tabControl = ((UserControl1)this.Control).MyTabControl;
var svc = (IDesignerHost)this.GetService(typeof(IDesignerHost));
if (svc != null)
{
var tab1 = (TabPage)svc.CreateComponent(typeof(TabPage));
tab1.Text = tab1.Name;
tab1.UseVisualStyleBackColor = true;
tabControl.TabPages.Add(tab1);
var property = TypeDescriptor.GetProperties(tabControl)["Controls"];
base.RaiseComponentChanging(property);
base.RaiseComponentChanged(property, null, null);
}
}
}
Then if you drag UserControl1 from toolbox and add it to the form, you can see the user control contains an editable tab control which contains 2 editable tab pages and all changes will be serialized and be persisted.
Note
If you have a single or two pages in the TabContrl of your UserControl and you want to make them editabe, you should create public properties in your UserControl and enable designer of each tab page with name of corresponding property.
Consider this note from remarks part of EnableDesignMode:
The child does not directly participate in persistence, but it will if
it is exposed as a property of the main control.
UserControl
You should have a public property for each TabPage which you want to expose:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TabControl MyTabControl
{
get { return this.tabControl1; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TabPage T1
{
get { return this.tabPage1; }
}
Designer
public class MyUserControl2Designer : ParentControlDesigner
{
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
this.EnableDesignMode(((UserControl2)this.Control).MyTabControl, "MyTabControl");
this.EnableDesignMode(((UserControl2)this.Control).T1, "T1");
}
}
I have a AppViewModel, that contains a menu on Top of the Window. On the AppViewModel construct, I'm showing a UserControl. In this UserControl I have a button, that calls another viewmodel (UserControl).
The idea is to keep the menu and working on the content of window. So, I have 1 window and 2 UserControls. This is correct?
How can I call another ViewModel from a button that is inside of a UserControl? Or, I have to call it from the Window? But the button it's inside of the UserControl!
My code:
class AppViewModel : Conductor<object>
{
private bool _MenuIsVisible;
public bool MenuIsVisible
{
get { return _MenuIsVisible; }
set
{
if (_MenuIsVisible != value)
{
_MenuIsVisible = value;
NotifyOfPropertyChange(() => MenuIsVisible);
}
}
}
public AppViewModel()
{
MenuIsVisible = true;
_ShowTutorial();
}
private void _ShowTutorial()
{
ActivateItem(new FirstViewModel());
}
}
public class FirstViewModel : Screen
{
protected override void OnActivate()
{
base.OnActivate();
}
}
On the FirstViewModel I have a button that needs to call SecondViewModel.
To navigate from the first ViewModel to the second ViewModel you could have a method in the first ViewModel like this:
public void NavigateToSecond()
{
var conductor = this.Parent as IConductor;
conductor.ActivateItem(new SecondViewModel());
}
The parent refers to the conductor which will take care of navigating for you.
In a windows application project I have a form which used a user control. I want to hide a label and textbox on user control. In which event of form I can do this ?
This method in user control which named DoctorPermissionApprove:
public void LoadDoctorPermission(int fromWhere)
{
if (fromWhere == 0) // Başhekimden geldiyse?
{
labelDoctor.Visible = true;
editDoctorWithoutHead.Visible = true;
}
else if (fromWhere == 1) // Normal Hekimden geldiyse
{
labelDoctor.Visible = false;
editDoctorWithoutHead.Visible = false;
}
}
And in form:
private void ExistRequestAndNewEntryForm_Shown(object sender, EventArgs e)
{
var obj = new DoctorPermissionApprove();
obj.LoadDoctorPermission(0);
}
For example I tried in shown event. But it still visible
I want to hide or show this components when the anybody open the form
Thank you a lot
In the UserControl class add a public property to set the internal label visibility true or false. This can be accessed from your parent form where your usercontrol is added.
Example:
public class YourUserControl
{
//This code will be in designer class
private Label lblYourLabelToHide = new Label();
//Create this public property to hide the label
public bool IsLabelVisible
{
set { lblYourLabelToHide.Visible = value; }
}
}
public class YourParentForm
{
//This will be in designer
private YourUserControl userControl = new YourUserControl();
public void Form_Load()
{
//based on some criteria
userControl.IsLabelVisible = false;
}
}
I am writing an application which is going to allows users to change the properties of a text box or label and these controls are user controls. Would it be easiest to create a separate class for each user control which implements the properties I want them to be able to change and then bind those back to the user control? Or is there another method I am overlooking?
Create a custom Attribute, and tag the properties you want the user to edit with this attribute. Then set the BrowsableAttribute property on the property grid to a collection containing only your custom attribute:
public class MyForm : Form
{
private PropertyGrid _grid = new PropertyGrid();
public MyForm()
{
this._grid.BrowsableAttributes = new AttributeCollection(new UserEditableAttribute());
this._grid.SelectedObject = new MyControl();
}
}
public class UserEditableAttribute : Attribute
{
}
public class MyControl : UserControl
{
private Label _label = new Label();
private TextBox _textBox = new TextBox();
[UserEditable]
public string Label
{
get
{
return this._label.Text;
}
set
{
this._label.Text = value;
}
}
[UserEditable]
public string Value
{
get
{
return this._textBox.Text;
}
set
{
this._textBox.Text = value;
}
}
}