private void tabPage2_Click(object sender, EventArgs e)
{
}
hello i have one tabconrol in my form this name is "tabControl1" & i
have 4 tabpage in tabControl1 i want when i click on tabpage1 run some
code when i click tabpage2 run some code too & ...
how can i ?
i think if condition is not true or i dont know i must wite code in
which event
i use tabPage1_Click event
tnx for help
I believe you're looking for the SelectedIndexChanged event:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(tabControl1.SelectedIndex.ToString());
}
This will display 0, 1, 2, 3 etc. depending on which tab page is selected. First one is 0, second is 1 and so on.
You can register on tabControls clicked event
private void tabControl1_Click(object sender, EventArgs e)
{
MouseEventArgs args = e as MouseEventArgs;
if (args.Button == MouseButtons.Left)
{
if (tabControl1.GetTabRect(0).Contains(args.Location))
{
//tabPage1 header clicked
}
else if (tabControl1.GetTabRect(1).Contains(args.Location))
{
//tabPage2 header clicked
}
}
}
You can try this code:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
int indexOfSelectedTab = tabControl1.SelectedIndex;
switch(indexOfSelectedTab)
{
case 0:
//code for tabPage0
break;
case 1:
//code for tabPage1
break;
default:
break;
}
}
Edit:
You need to add event to your TabControl too.
tabControl1.SelectedIndexChanged += tabControl1_SelectedIndexChanged;
Related
how can i show different message box depend on the tab control.
if i click on tab header of xtrapage1 messagebox.show("page1") and if i click on tab header of xtrapage2 messagebox.how("page2")
the code i use was in event mouse down
private void xtraTabControl1_MouseDown(object sender, MouseEventArgs e)
{
DevExpress.XtraTab.ViewInfo.XtraTabHitInfo hi = xtraTabControl1.CalcHitInfo(e.Location);
if (hi.HitTest == DevExpress.XtraTab.ViewInfo.XtraTabHitTest.PageHeader)
{
MessageBox.Show("a");
}
}
it keeps showing "a"
Try this
private void xtraTabControl1_MouseDown(object sender, MouseEventArgs e)
{
DevExpress.XtraTab.ViewInfo.XtraTabHitInfo hi = xtraTabControl1.CalcHitInfo(e.Location);
if (hi.HitTest == DevExpress.XtraTab.ViewInfo.XtraTabHitTest.PageHeader)
{
MessageBox.Show(hi.Page.Text.ToString()) );
if(hi.Page.Name == xtraTabPage1.Text.ToString())
MessageBox.Show("a");
}
}
Try this
if (YourTabControl.SelectedTab.Name == "tabName" )
{
// do stuff
}
Or you can achieve the same functionality on selectedIndexChanged event of tab.
private void YourTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (YourTabControl.SelectedTab == YourTabControl.TabPages["YourTabName"])
{
// your matched condition.
}
}
Im trying to open a new form when a label is double clicked. Im able to drag and drop the label .Im trying to open a new form on double click of label now.
private void control_MouseDown(object sender, MouseEventArgs e)
{
var control = sender as Control;
this.DoDragDrop(control.Name, DragDropEffects.Move);
}
private void control_DoubleClick(object sender, EventArgs e)
{
frm = new Frm();
frm.ShowDialog();
frm.Dispose();
}
EDIT 1:
I have tried both possible answers below, and they have not worked for me?
A more cleaner way is (note I changed Frm to Form1):
private void control_DoubleClick(object sender, EventArgs e)
{
using (Form1 frm = new Form1())
{
frm.ShowDialog();
}
}
You can't add DragDrop on MouseDown and then DoubleClick. That won't work.
I don't think there's an easy way to get around that, but once a control is being dragged, it won't respond to double click messages.
I've made some quick tests, and there's a "hacky" way. It'll make your dragging look weird (since it'll start after some time, instead of immediately after you press the mouse button), but here it goes:
private bool _willDrag = false;
private bool control_MouseUp(object sender, MouseEventArgs e)
{
// disable dragging if we release the mouse button
_willDrag = false;
}
private bool control_DoubleClick(object sender, EventArgs e)
{
// disable dragging also if we double-click
_willDrag = false;
// .. the rest of your doubleclick event ...
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
var control = sender as Control;
if (control == null)
return;
_willDrag = true;
var t = new System.Threading.Timer(s =>
{
var callingControl = s as Control;
if (callingControl == null)
return;
// if we released the mouse button or double-clicked, don't drag
if(!_willDrag)
return;
_willDrag = false;
Action x = () => DoDragDrop(callingControl.Name, DragDropEffects.Move);
if (control.InvokeRequired)
control.Invoke(x);
else
x();
}, control, SystemInformation.DoubleClickTime, Timeout.Infinite);
}
In the form.Designer right click on your label then properties, in the properties window click in events (the thunder icon), in the double_Click event dropdown select the event handler (control_DoubleClick) this method must have two parameters an object and a eventArgs
This is tricky as the DoDragDrop will eat up any further mouse events, and MSDN posting a rather stupid example doesn't help much.
Solution: Do not start the D&D in the MouseDown if you want to still receive click or double click events but use the MouseMove instead:
Replace this
private void control_MouseDown(object sender, MouseEventArgs e)
{
var control = sender as Control;
this.DoDragDrop(control.Name, DragDropEffects.Move);
}
by this:
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
DoDragDrop((sender as Control), DragDropEffects.Move);
}
Don't forget to hook up the new event!
I'm trying to get the tabpage that was clicked by right button of mouse,in another words the tabpage that opened the contextmenustrip.
There's a toolstripmenuitem called Close which I used to close the tab that was clicked on.
I used this code :
public partial class USBrowser : Form
{
private Point lastpoint;
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < browserTabControl.TabCount; i++)
{
Rectangle rec = browserTabControl.GetTabRect(i);
if (rec.Contains(this.PointToClient(lastpoint)))
closeTab(i);//this function closes the tab at specific index
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (e.Button == MouseButtons.Right)
lastpoint = Cursor.Position;
}
I also added(when adding the tabpage) :
browserTabControl.TabPages.Insert(browserTabControl.TabCount - 1,WebPage);
browserTabControl.SelectTab(WebPage);
browserTabControl.SelectedTab.MouseClick += SelectedTab_MouseClick;
void SelectedTab_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
lastpoint = Cursor.Position;
}
The problem is that the lastpoint is always (0,0) !!
Why ?
Any other suggested idea is welcomed
thanx in advance
None of these event handlers will actually run. Not the form's OnMouseClick() method since you are not actually right-clicking the form. And not the tab page's MouseClick event handler since you gave the TabControl a context menu. So lastpoint being empty is the expected outcome.
It is not clear how you want this context menu to work. If you use it by right-clicking the tab page then it is simple, just destroy the selected page:
private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
tabControl1.SelectedTab.Dispose();
}
If you activate it by right-clicking a tab, one that isn't selected, then it gets more complicated. You have to memorize which tab was clicked on, do so by using the context menu's Opening event:
private TabPage RightClickedTab;
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
RightClickedTab = tabControl1.SelectedTab;
var pos = tabControl1.PointToClient(Cursor.Position);
for (int tab = 0; tab < tabControl1.TabCount; ++tab) {
if (tabControl1.GetTabRect(tab).Contains(pos)) {
RightClickedTab = tabControl1.TabPages[tab];
break;
}
}
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
if (RightClickedTab != null) RightClickedTab.Dispose();
}
Situation is as follows:
I have a ButtonA, which is currently not enabled.
I have 5 Radiobuttons of which one is always checked.
I want to enable ButtonA when a different Radiobutton gets selected.
I thought about doing something like
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
... and so on.
There is probably a more elegant solution and im missing it.
You can use a single method as an event handler for all radiobuttons:
private void RadioButtonChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void HandleEvents()
{
this.RadioButton1.CheckedChanged += RadioButtonChanged;
this.RadioButton2.CheckedChanged += RadioButtonChanged;
this.RadioButton3.CheckedChanged += RadioButtonChanged;
}
Or a loop to do the same thing:
private void RadioButtonChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void HandleEvents()
{
foreach(var rb in new[] {RadioButton1, RadioButton2, RadioButton3})
rb.CheckedChanged += RadioButtonChanged;
}
Or even a lambda event handler set in a loop:
private void HandleEvents()
{
foreach(var rb in new[] {RadioButton1, RadioButton2, RadioButton3})
rb.CheckedChanged += (o,e) => ButtonA.Enabled = true;
}
How about just having 1 event handler like this:
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
Then you can use the form designer to assign this method name to the CheckedChanged events of all the radio buttons.
This will save you having to repeat the code for each radio button.
Another trick is to add all your radio buttons to a group box or panel in the designer, then they will all be children of that control.
Then you can add the following code to your Load event handler for the form:
foreach(var radioButton in radioGroupBox.Controls.Cast<Control>()
.Where(i => i is RadioButton)
.Cast<RadioButton>())
{
radioButton.CheckedChanged += RadioButton_CheckedChanged;
}
This way, if you add more RadioButtons to the group, you don't need to change any code.
I createbtn1_MouseDown Event.
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
btn1.BackgroundImage = Properties.Resources.click;
}
I want to disable that btn1_MouseDownt Event behind this RadioButton when it checked. I mean to say when Radiobutton checked then this btn1_MouseDown event should not work.
private void r1_CheckedChanged(object sender, EventArgs e)
{
if (r1.Checked == true)
clock = 5;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
}
Is that possible ?
inside r1_checkeChanged add:
btn1.MouseDown -=btn1_MouseDown;
You can remove the event:
btn1.MouseDown -= btn1_MouseDown
Of course don't forget to add it back again when you need it.
However, instead of doing that I would change your event handler's code to check the state of the radio button:
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (!r1.Checked)
btn1.BackgroundImage = Properties.Resources.click;
}
1st option can be to remove the event at radio button check_change event like this:
btn1.MouseDown -=btn1_MouseDow;
but i dont prefer this way.
Second is to add a flag at start of this event and check whether to execute the event or not
like this:
bool flag;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (flag) return;
btn1.BackgroundImage = Properties.Resources.click;
}