I have a dataGridView on the form, then I added a checkBox programically.
I added it to dataGridView.Controls list. Now I would like to run the method c_CheckedChanged every time when event of changing the checkbox is raised.
I did it in as in the code below, but it does nothing when I change the status of the checkBox.
How can add methods to the events of elements from dataGridView.Controls list?
I know I can use DataGridViewCheckBoxCell instead of standard checkBox, but it is not the solution I am looking for. I am trying to learn how to create events of custom controls in Controls.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var cb=new CheckBox();
dataGridView1.Controls.Add(cb);
(dataGridView1.Controls[0] as CheckBox).CheckedChanged += c_CheckedChanged;
}
private void c_CheckedChanged(object sender, EventArgs e)
{
var a=1;
}
}
I used the code var a=1; to put here breakpoint and check if the method is done.
But it is not.
Could you help, please?
Regards,
Kris
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;
}
My problem is more complex than you think!!
Let me explain first.
I have form say "Form1". It has one grid view that contains details of Items.
On the same form i have a button "Search". If i click on Search button another form opens say "SearchForm"
"SerchForm" has one textbox and button("Search").
Now i write the name of item in textbox of "SearchForm" and click on "Search", matching item should be shown in the grid view of "Form1".
Is it possible in windows form?? How??
Thanks in advance
If you want to know a call method in parent form, then using delegate and event.
SearchForm
: Make event and call it when 'Search' button is clicked.
// Make delegate and event
public delegate void DisplayData(string aMessage);
public event DisplayData ShowData;
private void btnSearch_Click(object sender, EventArgs e)
{
// Call event
ShowData(txtMessage.Text);
}
Form1
: Make method which you want to use and link it to event.
SearchForm searchForm = new SearchForm();
private void Form1_Load(object sender, EventArgs e)
{
// Add event
searchForm.ShowData += new SearchForm.DisplayData(Search);
}
private void Search(string aMessage)
{
// Input gridview add code here
}
Use a property in the SearchForm and retrieve it from the Form1
SearchForm :
public int GetSelectedItem { get; set; }
set the value of this property after you click the search button in SearchForm
Form1:
SearchForm searchForm = new SearchForm();
searchForm.ShowDialog();
int _selectedItem = searchForm.GetSelectedItem;
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);