I try to switch panels on tabControls on UserControl.
Like this
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible=true;
panel2.Visible=false;
.....
panelN.Visible=false;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible=false;
panel2.Visible=true;
.....
panelN.Visible=false;
}
private void buttonN_Click(object sender, EventArgs e)
{
panel1.Visible=false;
panel2.Visible=false;
.....
panelN.Visible=true;
}
but when N exceeds 6, switching panels doesn't work well.
Some panels don't visible ,even if the Button event occurs!
So could you tell me how to switch multi panels.
If possible, could you tell me the smart way to switch panels.
The above code seems to be bad readability.
You could make all of the buttons fire the same handler, then do something like this:
private void btn_Clicked(object sender, EventArgs e)
{
Button btn = (Button)sender;
int numButtons = 6;
int index = int.Parse(btn.Name.Replace("button", ""));
for(int i=1; i<=numButtons; i++)
{
Control ctl = this.Controls.Find("panel" + i, true).FirstOrDefault();
if (ctl != null)
{
ctl.Visible = (i == index);
if (i == index)
{
ctl.BringToFront();
}
}
}
}
Related
I need to focus TextCells one by one via a button click.
I tried listView.ScrollTo.
private void Button_Clicked_1(object sender, EventArgs e)
{
listViewJson.ItemTapped += ListViewJson_ItemTapped;
}
private void ListViewJson_ItemTapped(object sender, ItemTappedEventArgs e)
{
var focusing = e.Item;
listViewJson.ScrollTo(focusing, ScrollToPosition.MakeVisible, true);
}
Firstly, try to define an index to detect which cell to be selected. Then change the index via button click like:
int selectedIndex = 0;
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
when the TextCell is selected the ListView's ItemSelected event will fire, you can put your code in it like:
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
Here is my code behind for you referring to:
ObservableCollection<string> dataList = new ObservableCollection<string>();
int selectedIndex = 0;
public MainPage()
{
InitializeComponent();
for (int i=0; i<10; i++)
{
dataList.Add("item" + i);
}
myListView.ItemsSource = dataList;
myListView.ItemSelected += MyListView_ItemSelected;
MyBtn.Clicked += MyBtn_Clicked;
}
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
Is it possible to get any value from the currently displayed tabcontrol that is open (displayed)? Trying to highlight the adjacent / corresponding tab / link label.
I am using link labels as navigation for the tabs. The real (ugly top) tabs will hidden when the project is finished.
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
//COLOURS TO BE APPLIED WHEN THE CORRESPONDING TAB IS OPEN
linkLabel1.BackColor = Color.Black;
linkLabel1.ForeColor = Color.White;
linkLabel1.ActiveLinkColor = System.Drawing.Color.White;
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
}
After #Idle_Mind answer I still was not sure how to bind / wireup the event. This is for anyone else with same question:
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
labels_LinkClicked(sender, e);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
labels_LinkClicked(sender, e);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
labels_LinkClicked(sender, e);
}
//METHOD TO CALL ON EACH CLICK OF LINK LABELS
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach (LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Transparent;
}
}
Wire up the LinkClinked() events to the same event handler like below:
private List<LinkLabel> labels;
private void Form2_Load(object sender, EventArgs e)
{
labels = new List<LinkLabel>() { linkLabel1, linkLabel2, linkLabel3 };
}
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach(LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Gray;
}
}
Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.
if in a form I have 10 panels named in order from 1 to 10 and all of them registered with the same Event myPanel_Click
private void myPanel_Click(object sender, EventArgs e)
{
}
can I retrieve the name of the panel I clicked among those 10 panels?
int panelClicked;
private void myPanel_Click(object sender, EventArgs e)
{
//not a single clue
}
If I understand you correctly, you should be able to cast the sender as a panel and then take the name property.
private void myPanel_Click(object sender, EventArgs e)
{
Panel target = sender as Panel;
if(target != null)
MessageBox.Show(target.Name);
}
private void myPanel_Click(object sender, EventArgs e)
{
MessageBox.Show((Panel)sender.Name);
}
You can also us the Tag Property to reference your Panels, by assigning your panel number to the respective Tag.
private void myPanel_Click(object sender, EventArgs e)
{
Panel p = (Panel)sender;
switch ((int)p.Tag )
{
case 1:
// Your Code for Panel 1
break;
case 2:
// Your Code for Panel 2
break;
// Your other Panels here
default:
break;
}
}
Something like this:
private void secondTabPageInTabControl_Click(object sender, System.EventArgs e)
{
this.myTreeView.Enable = false;
} //then I chose other tabpages and it becomes enable
Use the TabControl's SelectedIndexChanged event instead. It tells you when a new tab is selected. Use code similar to this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
this.myTreeView.Enable = tabControl1.SelectedTab != tabPage2;
}
Or by index, less readable:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
this.myTreeView.Enable = tabControl1.SelectedIndex != 1;
}
Another Solution, but note that this could become bloated if you use too many tabpages:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
UpdateUI(e.TabPageIndex);
}
public void UpdateUI(int index)
{
switch (index)
{
case 0:
treeView1.Enabled = true;
break;
case 1:
treeView1.Enabled = false;
break;
case 2:
treeView1.Enabled = false;
break;
default:
treeView1.Enabled = false;
break;
}
}