I want to popup/show a dialog when ever my user control is displayed/shown. But I couldn't find any such event. What options do I have and what would I have to do to implement it myself?
I'm using DevExpress XtraUserControl.
Thanks.
Register to the Activated event of the main form.
public Form1()
{
InitializeComponent();
this.Activated +=new EventHandler(Form1_GotFocus);
}
public void Form1_GotFocus(object sender, EventArgs e)
{
//your payload here
}
Note that if you close a dialog and return to the form, the event is fired again.
Related
I'm new to C# so this is my first task essentially, I'm hoping to make a simple login page.
I would like the text to disappear from the textbox once it has been clicked, here's what I have tried so far;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool hasBeenClicked = false;
private void textBox1_Focus(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
box.Text = string.Empty;
hasBeenClicked = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
This is from a similar post on here, It doesn't seem to work for me.
Here is something else I have tried;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "";
}
I understand it may be a silly mistake, I'm learning.
Any helps is massively appreciated :)
I'm using Winforms
Try using the event MouseClick on the TextBox:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
this.textBox1.Text = "";
}
In your screenshot, you are missing a reference. Click into the textbox events and add it like so:
Both of which should probably work but you've got to link the event to the function. You can either do this in the Designer or in code. For the Mouse click variant to work and link the event in code you can add the following in the constructor:
public Form1()
{
InitializeComponent();
this.MouseClick += textbox1_MouseClick;
}
Add Event Handler
You have not linked the given methods with the event, as you can see 0 refereces for both functions. You can add a mouse Click event as:
textBox1.Click += new System.EventHandler(textbox1_Mouseclick);
Similarly, you have to add event if you want to do with the focus event.
I have parent form called MainBackground and a user control named LoginUI. LoginUI is docked into the MainBackground.
I need to change enability of a button (InfoButton) located in parent form to "true" when user clicks on the button "Log in" in the control form.
But I can't access the parent button's properties.
Control form's button clicking event code:
private void button1_Click(object sender, EventArgs e)
{
MainBackground.infoButton.Enabled = true;
}
I tried solving it with parent controls, but still it doesn't seem to work.
Thanks for any help!
You can't access MainBackground.infoButton from LoginUI because infoButton is not static.
to solve this you could inject MainBackground trough a property like the example below
public partial class LoginUI : UserControl
{
public MainBackground MainBackground { get; set; }
...
}
in MainBackground you should initalize your LoginUI.MainBackground propery
loginUI1.MainBackground = this;
Make sure to make infoButton public
by setting the modifiers property to public
Now you can access MainBackground.loginUI1
private void login_Click(object sender, EventArgs e)
{
MainBackground.InfoButton.Enabled = true;
}
The method described in your question of Enabling the MainBackground forms InfoButton when the Login button is pressed is a common action. However, instead of directly binding the two items where the LoginUI Control is now forever bound to the MainBackground Form, you should de-couple the two by using Events.
The LoginUI Control should publish an event, perhaps called, LoginClicked. The MainBackground form can then subscribe to this event and execute whatever actions are required when the Login button is clicked.
In the LoginUI Control, declare an event:
public event EventHandler LoginClicked;
And, raise it whenever the Login button is pressed:
private void login_Click(object sender, EventArgs e)
{
OnLoginClicked(EventArgs.Empty);
}
protected virtual void OnLoginClicked(EventArgs e)
{
EventHandler handler = LoginClicked;
if (handler != null)
{
handler(this, e);
}
}
Finally, in the MainBackground form class, subscribe to the LoginClicked event
loginUI.LoginClicked += this.loginUI_LoginClicked;
Handle the LoginClicked event like this:
private void loginUI_LoginClicked(object sender, EventArgs e)
{
InfoButton.Enabled = true;
}
I have a user control with the class name PinPad. It contains a Button which I handled its click event btnGetAccess_Click(object sender, EventArgs e) and a property for reading the Password entered in a TextBox.
I put an instance of PinPad control on my Form1 and it's is supposed to open Form2 when the button is clicked and the password is correct.
since I put it in constructor currently checking the password does nothing because it will only check the password when Form1 loads and no password is entered yet.
So how can I make Form1 continuously check the password after the button in user-control class was clicked?
Here is my current code:
public Form1()
{
InitializeComponent();
if (pinPad1.Password == "123456")
{
// open form2
}
}
You don't need to check a button click continuously. Events are created to subscribe them. Then they notify you when an event raised.
You should create a new event in your user control and raise it for example when the button in your user control clicked. Then you can subscribe for that event in the form and run your logic there.
Example:
[System.ComponentModel.DefaultEvent("ButtonClicked")]
public partial class SampleControl: UserControl
{
public SampleControl()
{
InitializeComponent();
button1.Click += button1_Click;
}
public event EventHandler ButtonClicked;
protected virtual void OnButtonClicked(EventArgs e)
{
var handler = ButtonClicked;
if (handler != null)
handler(this, e);
}
private void button1_Click(object sender, EventArgs e)
{
//Do Stuff then raise event
OnButtonClicked(EventArgs.Empty);
}
}
You can put the control on the form and handle its ButtonClicked event. Since we set ButtonClicked as default event of the control, when you double click on it in designer, it creates the event handler and subscribes for event:
private void sampleControl1_ButtonClicked(object sender, EventArgs e)
{
MessageBox.Show("Button Clicked");
}
As a C# developer, you should learn about events as much as you know classes, methods, properties and etc. You can learn more about Handling and Raising Events.
Use Timer. There's a good example. Implement it, and don't forget to stop the timer after the password is correctly entered and checked.
I am developing a windows mobile application, and I want to do something after a form (a loading screen) is displayed to the user.
normally, I have a Form.Shown event, but with the .net compact framework v3.5, I can't find this event.
Does anyone know of an equivalent to the Shown event or a simple workaround I could use? I don't want to do my own multi-threading if I can help it.
The only thing I can think of would be a bit of a hack, when your form is shown, if it has a default control then it will receive the focus. The Focus event will fire during the initial load before the form is shown, but will be fired the second time when it is visible. put a boolean in the Activate event that is set with the first Activates, then test in the default controls Got Focus event.
Other option would be to use a Timer. Set the Interval to something like 10, start it at the end of your Form Load event and then run your startup specific code.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
//Do something
}
An example per Hans's Comment:
public partial class Form1 : Form
{
public delegate void DoWorkDelegate();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BeginInvoke(new DoWorkDelegate(DoWorkMethod));
}
public void DoWorkMethod()
{
//Do something
}
}
I have a form1.cs and in that form I have a panel1, in the load event of the form1.cs I am adding a control to the panel1. Now my issue is, I have a control called Numbers.cs, I need to add another control to that panel1 but from this control in a button event. How can I do this?
public partial class Number : UserControl
{
public Number()
{
InitializeComponent();
}
private void btnAcceptWelcome_Click(object sender, EventArgs e)
{
//HERE I NEED TO PASS A CONTROL TO THE PANEL1 IN FORM1.CS
//NOT SURE HOW TO DO THIS.
}
}
MORE INFO
So basically I have a folder called UserControls and in that folder I have
Numbers.cs
Letters.cs
Welcome.cs
All of them user controls, then i have a form
Form1.cs
Form1.cs instantiates Welcome and it is added to a Panel1 on the Form1.cs on form load. Then Welcome.cs has a button, when I click this button I need to swap to Numbers.cs. But I dont know how to do this from Welcome.cs
Another way would be to use a Custom Event raised by Numbers and handled by Form1 to pass the control and add it to your Panel's Control Collection.
This is an example of an Custom Event added to UserControl1
Form1
public partial class Form1 : Form
{
UserControl2 mySecondControl = new UserControl2();
public Form1()
{
InitializeComponent();
userControl11.AddControl+=new EventHandler(SwapControls);
}
private void SwapControls(object sender, EventArgs e)
{
panel1.Controls.Remove(userControl11);
userControl11.AddControl -= new EventHandler(SwapControls);
panel1.Controls.Add(mySecondControl);
}
}
UserControl
public partial class UserControl1 : UserControl
{
public event EventHandler AddControl;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.AddControl(this, new EventArgs());
}
}
Note:
Untested code
Assuming Form1 has (or can get) a reference to Number
Add an event handler to Number:
public partial class Number : UserControl
{
// event handler Form1 will subscribe to
public EventHandler<EventArgs> OnWelcomeAccepted = (o, e) => { };
public Number()
{
InitializeComponent();
}
private void btnAcceptWelcome_Click(object sender, EventArgs e)
{
// raise the event
OnWelcomeAccepted(sender, e);
}
}
...Form1 will have a subscription after InitializeComponent(); note the additional subscription to ControlAdded:
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
this.ControlAdded += Control_Added;
// subscribe to the event and provide the implementation
Number.OnWelcomAccepted += (o, e) => { Controls.Add(GetControl( )); }
}
private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
// process size and placement and show
}
}
No other control should be adding anything directly to Form1. Let Form1 control it's children.
One way is to have a reference to panel1 within numbers since both of them are created within form1 you can pass one as an argument to the other's constructor.
It's not very clear from your description but you can just pass the control you want in the constructor or a property. Since in C# objects are always by reference you will be action on the same control in the Button event. You can always write your own event and have the panel register for it. A more complete code sample would be great.
I'm still a little unsure of what you're doing exactly but that's OK. I think the most flexible approach is to create your own custom event. Outside of any class create a delegate:
public delegate void WelcomeClick(object sender, EventArgs e);
Inside Welcome you need to create the event handler, it can be either static or part of the instance:
public event WelcomeClick OnClick;
Inside the Button Click event in welcome you can just call that event with the same parameters:
if (OnClick != null)
OnClick(sender, e);