I extended the button control to have also LabelName. When I press the button I need to write the name of the button in the label.
My first idea was using events - easy and simple.
The question is: Is there more elegant way to do it? (I've been asked to bind the button and the label)...
I think that the best way to do it would be to use an action listener and the best way to use the action listener would be to build it into your class that extends the button control so that the user doesn't have to do this on their own. It would look like this.
class Button2 : Button
{
public string LabelName = "";
public Button2()
{
this.Click += this.SetLabelName;
}
private void SetLabelName(object sender, EventArgs e)
{
this.LabelName = "Something?";
}
//You could also do this instead.
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
}
}
If you're talking about changing the Text property of an external Label control, then simply create a property in your Button to hold a reference to a Label. You can set this via the IDE like any other property:
Here's the Button class:
public class MyButton : Button
{
private Label _Label = null;
public Label Label
{
get { return _Label; }
set { _Label = value; }
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if (this.Label != null)
{
this.Label.Text = this.Name;
}
}
}
Here's the Label after I clicked the Button:
Related
I want to display message that button is unclickable (I have used Enabled option, button1.Enabled = false/true; ). Does anyone know how to detect if button is clicked, when it is unclickable in order to display error message "Button is unclickable...".
Windows forms, C#
Whenever a standard control doesn't behave in exactly the way that need it to, all we usually have to do is make our own version that inherits the standard control so that we can make it do whatever we want. Your question offers a great reason for doing that because a normally a disabled Button is not going to fire a Click or a MouseDown event.
Here is a guideline example for a custom Button that:
Intercepts the Enabled property by declaring it new.
Leaves the base class button always responsive because it's always enabled.
Paints the control as dimmed if disabled.
Suppresses the firing of the Click event if disabled, and fires DisabledClick instead.
Look in the Title Bar to see when the button is clicked.
ButtonWithDisabledOption class
class ButtonWithDisableOption : Button
{
bool _enabled = true;
public new bool Enabled
{
get => _enabled;
set
{
if (!Equals(_enabled, value))
{
_enabled = value;
OnEnabledChanged(EventArgs.Empty);
}
}
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
if(Enabled)
{
ForeColor = SystemColors.ControlText;
BackColor = SystemColors.Control;
FlatStyle = FlatStyle.Standard;
}
else
{
ForeColor = Color.FromArgb(191, 191, 191);
BackColor = Color.FromArgb(204, 204, 204);
FlatStyle = FlatStyle.Flat;
}
}
protected override void OnClick(EventArgs e)
{
if (Enabled)
{
base.OnClick(e);
}
else
{
DisabledClick?.Invoke(this, EventArgs.Empty);
}
}
public event EventHandler DisabledClick;
}
MainForm.Designer.cs
Be sure to replace Button references with ButtonWithDisabledOption.
private void InitializeComponent()
{
// this.buttonWithDisableOption = new System.Windows.Forms.Button();
this.buttonWithDisableOption = new button_with_disabled_option.ButtonWithDisableOption();
...
}
// private System.Windows.Forms.Button button1;
private button_with_disabled_option.ButtonWithDisableOption buttonWithDisableOption;
TEST
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
buttonWithDisableOption.Enabled = checkBoxButtonEnabled.Checked = true;
// Button events
buttonWithDisableOption.Click += buttonWithDisableOption_Click;
buttonWithDisableOption.DisabledClick += buttonWithDisableOption_DisabledClick;
// CheckBox events
checkBoxButtonEnabled.CheckedChanged += checkBoxButtonEnabled_CheckedChanged;
}
private void checkBoxButtonEnabled_CheckedChanged(object sender, EventArgs e)
{
buttonWithDisableOption.Enabled = checkBoxButtonEnabled.Checked;
}
private void buttonWithDisableOption_Click(object sender, EventArgs e)
{
// Title bar
Text = $"Button Click {_tstcount++} (Enabled)";
}
private void buttonWithDisableOption_DisabledClick(object sender, EventArgs e)
{
// Title bar
Text = $"Button Click {_tstcount++} (Disabled)";
MessageBox.Show("Button is unclickable");
}
int _tstcount = 1;
}
I can highlight the text in an individual MaskedTextBox when it gets focus using:
this.myTextBox.SelectAll();
But, I want to do it for all MaskedTextBox when a mouse click event occurs. Instead of adding 30 individual event method for each MaskedTextbox, I want to select all MaskedTextBox and have one event method to take care of it, ie:
private void MouseClickedForMaskedTextBox(object sender, MouseEventArgs e)
{
this.ActiveControl.SelectAll();
}
But SelectAll is not available for this.ActiveControl. Is there a way to get around it?
sender will be the target of the event.
You could cast sender:
MaskedTextBox maskedTextBox = sender as MaskedTextBox;
if (maskedTextBox != null) { maskedTextBox.SelectAll(); }
Or in C# 7,
if (sender is MaskedTextBox maskedTextBox)
{
maskedTextBox.SelectAll();
}
Another improvement is to use TextBoxBase and it will work with TextBox and RichTextBox as well.
Put the following code in the form's constructor:
foreach (Control c in Controls)
{
if (c is TextBox)
{
TextBox tb = c as TextBox;
tb.GotFocus += delegate { tb.SelectAll(); };
}
}
Simply do that:
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker) delegate() {
maskedTextBox1.SelectAll();
});
}
I found another way by creating or edit your User Control that inherits MaskedTextBox. In the designer you set true the property "OnEnterSelectAll".
public partial class MaskedTextBoxX : MaskedTextBox
{
public MaskedTextBoxX()
{
InitializeComponent();
Inicializar();
}
// ===============================
// Campos AƱadidos
// ===============================
public bool OnEnterSelectAll { get; set; } = false;
// ===============================
// Metodos
// ===============================
private void Inicializar()
{
// *** SELECCIONAR TODO el MarkedTextBox
Click += delegate { if (OnEnterSelectAll) SelectAll(); };
}
}
In visual studio how do you access a control on a form hosting a user control? For example, when text changes in a text-box in a user control, I want text in another text-box in another user control to change. Both these user controls are hosted on the same form. Thanks in advance!
If you need different UI for data entry, I prefer to have 2 controls with different UI, but I will use a single data source for them and handle the scenario using data-binding.
If you bind both controls to a single data source, while you can have different UI, you have a single data and both controls data are sync.
The answer to your question:
You can define a property in each control which set Text of TextBox. Then you can handle TextChanged event of the TextBox and then find the other control and set the text property:
Control1
public partial class MyControl1 : UserControl
{
public MyControl1() { InitializeComponent(); }
public string TextBox1Text
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (Parent != null)
{
var control1 = Parent.Controls.OfType<MyControl2>().FirstOrDefault();
if (control1 != null && control1.TextBox1Text != this.textBox1.Text)
control1.TextBox1Text = this.textBox1.Text;
}
}
}
Control2
public partial class MyControl2 : UserControl
{
public MyControl2() { InitializeComponent(); }
public string TextBox1Text
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (Parent != null)
{
var control1 = Parent.Controls.OfType<MyControl1>().FirstOrDefault();
if (control1 != null)
control1.TextBox1Text = this.textBox1.Text;
}
}
}
i have a textbox on .aspx page..On this page there is a user control .Inside this user contrl there is a button .I want o get the value of text box on button click which is not inside the user control .How can i do this
Please Help me .
write this line in you button click event of user control
protected void Button_Click(sender obj,EventArgs arg)
{
TextBox txtbox= (((MyPage)parent).FindControl("TextBoxid") as TextBox);
if(txtbox!=null)
(((MyPage)this.Page).FindControl("TextBoxid") as TextBox).Text;
//or
//(((MyPage)this.Parent).FindControl("TextBoxid") as TextBox).Text;
}
or
alternative is create the property in your page and access it in your user control
public string txtValue
{
get
{
return TextboxID.Text;
}
}
in button click event of user control
protected void Button_Click(sender obj,EventArgs arg)
{
string txtvalue = ((Mypage)this.Page).txtValue;
//or
//((MyPage)this.Parent).txtValue;
}
protected void MyButton_Click(object sender, EventArgs e)
{
string TextBoxValue;
TextBoxValue = MyTextBox.Text;
}
Is it what you want ?
Try use the following method,
((TextBox)USerControl.Parent.FindControl("txtbox")).Text
((TextBox)USerControl.Page.FindControl("txtbox")).Text
or
((YourPageType)USerControl.Page).TextBox.Text
With de-coupling in mind, I would recommend that if your user control needs to access information outside of it, then that information should passed in, not vice versa. The control shouldn't be responsible for where the information comes from, it just knows there is information. With this in mind, I would recommend bubbling the event to get the required information.
Event Bubbling
This will involve creating a new delegate, and then triggering it once the Button has been clicked, thus bubbling the event and allowing us to return the desired value, which in this case is the textbox value.
Step 1: Declare the delegate
// declare a delegate
public delegate string MyEventHandler(object sender, EventArgs e);
Step 2: Update the user control
// update the user control
public class MyUserControl : UserControl
{
// add the delegate property to your user control
public event MyEventHandler OnSomeButtonPressed;
// trigger the event when the button is pressed
protected void MyButton_Click(object sender, EventArgs e)
{
string someString = string.Empty;
if (this.OnSomeButtonPressed != null)
{
someString = this.OnSomeButtonPressed(this, e);
}
// do something with the string
}
}
Step 3: Update the page
// be sure to register the event in the page!
public class MyPage : Page
{
protected override void OnLoad(object sender, EventArgs e)
{
base.OnLoad(sender, e);
myUserControl.OnSomeButtonPressed += this.HandleUserControl_ButtonClick;
}
public string HandleUserControl_ButtonClick(object sender, EventArgs e)
{
return this.SomeTextBox.Text;
}
}
I want to pass one of the textbox's(on the master page) value into the user control(.ascx) page. Here is my code shows how to open user control..
Control usrCnt= LoadControl("userControl.ascx");
usrCnt.ID = "usrCnt";
ASPxPanel1.Visible = true;
ASPxPanel1.Controls.Clear();
ASPxPanel1.Controls.Add(userCnt);
How can post the textbox's value to the user control? I can't do like this..
Control usrCnt= LoadControl("userControl.ascx?param=" + textbox.Text);
Create a method for your usercontrol like SetText
and then
usrCnt.SetText("textValue");
if it is your webusercontrol code behind
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetText(string theText)
{
this.Label1.Text = theText;
}
}
and if you've been added the control to the page
in page call it as
this.WebUserControl11.SetText(TextBox1.Text);
put these on the upper part of your usercontrol
private string _TextBoxValue = string.Empty;
public string TextBoxValue {
get { return _TextBoxValue; }
set { _TextBoxValue = value; }
}
then on your masterpage
usrCnt.TextBoxValue = TextBox1.Text;
For the quickest and dirty way is on your MasterPage
ViewState["TextBoxValue"] = TextBox1.Text();
and on UserControl, access ViewState["TextBoxValue"] to get the value.