How to access 2 User Controls at a time?(1 from another) - c#

I have made 2 user controls. First one contains a textbox and a button. In the second one there is a panel and a repeater control is used. Now when I am writing in the textbox 2nd control will open as a popup and and after focusing to textbox I am unable to write. I have searched a lot about this but nothing works.
CustomPopup customPopup;
Popup popup;
popup = new Popup(customPopup = new CustomPopup());
private void txtSearch_TextChanged(object sender, EventArgs e)
{
popup.Width = Width;
popup.Show(this);
popup.AutoClose = false;
}
In this way I am opening the popup from the textbox text changed event.

You can use this textbox event.
private void textBox1_Enter(object sender, EventArgs e)
{
}
this event call whenever textbox get focus

Related

Click Event not fired in User Control when multiple instances are created

I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}

Disable Parent Control of a button in windows Forms

so what i want to do is, on Event ButtonClick disable parent Control of this button.
In my Form i have several Panel's in which those button actually are.
I am using following code:
private void button1_Click(object sender, EventArgs e)
{
Control control = button1.Parent;
control.Enabled = false;
}
This xode is working fine, but I wanted to use this.Parent instead of button1.Parent, so that each button would be able to disable its own parent Panel(in this case will disabled Panel of button1).
When i am using this.Parent I get a System.NullReferenceException.
Knows some one why i am getting this error ?
this is your current class you want to use the sender and cast it to Control than get it's parent something like this
private void button1_Click(object sender, EventArgs e)
{
var uc = sender as Control;
uc.Parent.Enabled = false;
}
this represents the Window, which has no Parent.
You can do the following:
private void button_Click(object sender, EventArgs e){
Control control = ((Button) sender).Parent;
control.Enabled = false;
}
Button[] buttons = {button, button2....};
foreach (Button button in buttons)
button.Click += button_Click;
Alternatively, you can create a class inheriting from Button. You can then add a Click event handler that disables it's parent control. You can then use this button instead of the default one.

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

WinForms TabControl

I noticed that by programmatically selecting a Tab in the Tab control selects a control contained in the tab page selected.Is it possible to change this behaviour. I have a control in a tabpage that I do not want to be selected when the its tab page is selected from a button click. I have a simple form with a tab control and two tab pages. When button1 is clicked the tab page 2 is selected but so is the datagridview contained in that tab page.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.GotFocus += DataGridView1_GotFocus;
}
private void DataGridView1_GotFocus(object sender, EventArgs e)
{
//this event is called from button1_click
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}
}
By default when you select a tab (or even when you start a form) the control which is the first in your tab order is automatically focused. I am assuming this is what is happening here.
You can solve this by simply unfocusing the datagridView in question. There are multiple ways to do this. Firstly you can set focus to the control that you wish to be selected instead of the dataGridView. This can be done by:
myControl.Focus = True;
Or alternatively if you want non of the controls to be selected you can set the active control to Null:
ActiveControl = NULL;
Note: ActiveControl is a property which contains the current active control.
As to where this code should be placed. That is totally dependent upon you. You can do it as soon as you change the tab in the button click event. This is what I would prefer.
I am sure there are other kludges as well to acheive the same. Hope this helps.
Here is code to select tab
private void button1_Click(object sender, EventArgs e)
{
// we can select tab by tab name
tabControl1.SelectTab("tabPage2");
tabControl1.SelectedIndex = 1;
tabControl1.TabPages[0].Hide();
tabControl1.TabPages[1].Show();
}

Preserve control's visibility on condition

This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}

Categories