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();
}
Related
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.
I have written a program in C# using a windows form in Visual Studio 2017. It displays in splitContainer a treeView displaying directory with subdirectories on the left panel and a listView showing the files in selected directory on the right. I made a button (deleteFiles) which I initially set the Enabled property to False. I want it to be not enabled until the user selects file from the listView and then resets to not enabled until the next file is selected. Here is my code:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems != null)
{
bntDeleteFile.Enabled = true;
}
else
{
bntDeleteFile.Enabled = false;
}
}
When I run the program the button becomes enabled after I select a file, but it remains enabled afterwards even if no files are selected. How do I have the button reset to the not enabled state until the next file is selected.
Take a look at the Order of Events in Windows Forms
For Windows Forms I recommend setting the control in the form load event. Setting the button enable to false will disable the button prior to the user seeing the form:
private void Form1_Load(object sender, System.EventArgs e)
{
bntDeleteFile.Enabled = false;
}
Also do you have a button click event that disables the button during the click event? Setting the button enable to false when the button is click will keep it disabled when the file is deleted.
private void Button_Click(object sender, EventArgs e)
{
bntDeleteFile.Enabled = false;
}
Your "listView1_SelectedIndexChanged" event will enable the button when you select another file.
Assuming that when another directory is selected, you are running a listView1.Items.Clear() (or clearing the bound DataSource), you probably want to run this just before clearing the items:
listView1.SelectedItems.Clear();
That should clear the selected item, which will trigger the event you want.
You can set the button to disabled when you select a treeview item:
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
bntDeleteFile.Enabled = false;
}
I have a splitcontainer. In Panel 1 I have 7 toolstripbuttons that opens a User Control in Panel 2 when I click on them. The user control consists of different textboxes that the user can fill in. The code looks like this:
private void toolStripButtonBaseLayers_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
BaseLayers BL = new BaseLayers();
splitContainer1.Panel2.Controls.Add(BL);
}
private void toolStripButtonSpatialCoverage_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
SpatialCoverage SC = new SpatialCoverage();
splitContainer1.Panel2.Controls.Add(SC);
}
private void toolStripButtonFloodMaps_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
FloodMaps FM = new FloodMaps();
splitContainer1.Panel2.Controls.Add(FM);
}
Now everytime I open another user control with the click event the user control will be empty because I clear the panel and open a new User Control. This is not what I want to do!
My question is how it is possible to save everything you filled in in the textboxes? And how it is possible that all the user control values will be still filled in when switching between them? For example I fill the user control BaseLayers in, I switch to the user control Spatial Coverage and Flood Maps and when I swith back to the user control BaseLayers everything still have to be filled in.
Create all your controls and hide all of them(Visible=false). Then in Click-Events just set Visible=true for the appropriate control.
Example:
private void toolStripButtonFloodMaps_Click(object sender, EventArgs e)
{
SetChildVisibleForPanel(typeof(FloodMaps), splitContainer1.Panel2);
}
static void SetChildVisibleForPanel(Type visChildType, Panel pnl)
{
if (pnl.Controls.Count==0)
{
//Create your controls with Visible=false;
}
foreach (Control ctl in pnl.Controls)
{
ctl.Visible = (ctl.GetType() == visChildType);
}
}
In my MainWindow.xaml i have a Frame. The content of this Frame will change when i click a Button. So if I click a button to show private customers, the Frame Content shows the site of the private customers:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Privatecustomer.privatecustomer_show();
}
After clicking this Button the Frame will change the Content.
The new Content shows Privatecustomer.privatecustomer_show.xaml now.
In this xaml i have another Button. If i click this Button, the Content of the Frame in MainWindow should change to "Privatecustomer.privatecustomer_add.xaml".
But How I can tell from privatecustomer_show.xaml to MainWindow.xaml, that the Button addPrivatecustomer in privatecustomer_show is clicked and that the Main.Content have to change to
Main.Content = new Privatecustomer.privatecustomer_add();
?
I hope I can get some help here
How about adding an event in privatecustomer_show.xaml that the MainWindow.xaml can subscribe to.
Like this
public event EventHandler AddPrivateCustomer;
protected virtual void OnAddPrivateCustomerEventArgs e)
{
if (AddPrivateCustomer!= null)
AddPrivateCustomer(this, e);
}
Update: Please note the updated code, I made a copy'n'paste mistake in my first version.
Change your:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
To:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
var privateCustomerContent=new Privatecustomer.privatecustomer_show();
privateCustomerContent.AddPrivateCustomer+=onClick_addPrivateCustomer;
Main.Content = privateCustomerContent;
}
private onClick_addPrivateCustomer(object o,EventArgs e)
{
// Change Main.Content
}
The idea is that your privatecustomer_show control sends events for when it want to change something outside of what it has access to.
You MainWindow which has full control of all its child windows will then subscribe to each event.
Set Page2.Tag property to the instance of your MainWindow (use Binding in Xaml, or simply set it in code). After the specified Button in Page2 is clickd, simply use the Tag property (which is now your MainWindow).
Another option is to use
App.Current.MainWindow
When the Button is clicked. (A warning. I don't know the structure your project and this might not be the instance you are looking for).
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