Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How detect when user changes focus from multi-window application (alt+tab e.g.).
I want to detect when none of the app windows is active/focused.
First window is always shown but user can work with up to four windows(none of these is shown as dialog).
Form has a ContainsFocus property that indicates whether the form, or one of its child controls has the input focus. You can check this property for all open forms to detect if the application contains focus or not:
var isActive = Application.OpenForms.Cast<Form>().Any(x=>x.ContainsFocus);
Also as another option:
var isActive = (Form.ActiveForm != null)
If you want to be notified of the state of the application you can handle Activate and Deactivate event of your forms for all forms.
private void f_Deactivate(object sender, EventArgs e)
{
BeginInvoke(new Action(() =>
{
if (Form.ActiveForm == null)
Text = "App Deactivated.";
else
Text = "Still Active";
}));
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My ultimate goal is to make Combobox un-editable. Currently when user makes any selection then ComboBox shows selection and highlights it, therefore user can right-click and cut and delete the text that appears. I have added keyDown method, which prevents user from command such as ctrl + c, ctrl + v and delete. But User can still modify highlighted Text using right-click 'cut', 'copy' and 'paste'. How can i prevent user from modifying the current selection?
Ultimate goal is to make Combobox un-editable.
You could subscribe to KeyDown event and set SuppressKeyPress to true for all other actions other than Ctrl+c and create new ContextMenu to disable all default behavior.
comboBox2.KeyDown += comboBox2_KeyDown;
comboBox2.ContextMenu = new ContextMenu(); //disable right click
void comboBox2_KeyDown(object sender, KeyEventArgs e)
{
if (!(e.Control && e.KeyCode == Keys.C))
{
e.SuppressKeyPress = true;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My application has a global menu and some MDI children (in a Tab Manager). Each MDI Child should behave separately. I would like an MDI child to be able to show a modal dialog that only blocks its parent, and not the other MDIs in the application
When I display a modal form from an MDI child, all MDI children are blocked. How can I create a dialog that only blocks input on its parent?
You can disable the opening Form instead of making the modal child truly modal..
You can try this: Open the 'modal' children with
this.Enabled = false;
FormDlg yourModalChildForm= new FormDlg(this);
yourModalChildForm.Show();
In the constructor write :
Form myParent;
public FormDlg(Form myParent_)
{
InitializeComponent();
myParent = myParent_;
}
And in the FormClosed write:
private void FormDlg_FormClosed(object sender, FormClosedEventArgs e)
{
myParent.Enabled = true;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In C# Windows form application, how to find the label on the form which contains the text I'm looking for?
For example: I am trying to search a label whose Text property contains "25".
You can find it using linq this way:
var control = this.Controls.OfType<Control>().Where(x => x is Label && x.Text.Contains("25"));
or as #Sayse suggested just filter on Label type:
var Labelcontrol = this.Controls.OfType<Label>().Where(x => x.Text.Contains("25"));
Explanation:
If we want to fetch all controls of the form we have to do :
var AllControls = this.Controls.OfType<Control>();
and if we want to fetch only Controls of Type Label then:
var LabelControls = this.Controls.OfType<Label>();
Here this refers to current form of application.
UPDATE:
If you have label in nested controls, means inside some user control or some other control, then you need to check recrursively as in this SO post (How to get ALL child controls of a Windows Forms form of a specific type)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi Guys. I am working on a project using visual studio C#, and here is an image of my win form now what i an doing is entering the value in the fields o this form and saving the to my database but after reloading this form all the previous values are there. want i want is that when i click the SAVE button in this form it must save all the values in my database and then clear all the previous values in the text fields + radio buttons etc.
Please help me what will be the code for this process and where should i write this code?
private void btnSaveCick(object sender,EventArgs e)
{
//save your data here
//after saving your data call the CLearControls() function
ClearControls(this);
}
void ClearControls(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
else if (c is RadioButton)
((RadioButton)c).Checked = false;
else if (c is ComboBox)
((ComboBox)c).SelectedIndex = -1;
if(c.HasChildren)
ClearControls(c);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
in C# with DotNet 4 i have a form with a combobox which is filled with values when starting the program.
Now user can dropdown and select one of the values.
But: It is also possible to write something new into the combobox-field.
Question: What can i do that it is NOT possible to write something which is not part of the list?
Thanks
To make the text portion of a ComboBox non-editable, set the DropDownStyle property to "DropDownList".
It can be done by simply assigning a property to combobox .DropDownStyle = ComboBoxStyle.DropDownList. but, this property do not allow to edit text. means you have to select item either by mouse or by up/down arrow key. You cannot filter result by selecting this property. if you wish to filter result but don't allow to accept invalid value then you can do this by writing some code in cmb_Validating event
private void cmb_Validating(object sender, CancelEventArgs e)
{
if (cmb.SelectedValue == null && cmb.Text != string.Empty)
e.Cancel=true;
}