Playing around with buttons in Windows Forms - c#

I'm on to a small project where I try to make my own web browser.
I found out that a web browser is worthless without "New Tabs"-function, so I thought that I could use buttons as tabs and every time I press "ctrl + T" a new button appears.
The problems I encountered is:
-Array of buttons in a way that makes it possible for me to spawn a new button every time I press "ctrl + T"
-When the button is spawned it should be clickable and disabled when clicked until another tab (button) is click.
At the moment I focus on getting 1 tab to work, so here's an example:
private void TB_Address_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
{
Button tabButton = new Button();
tabButton = new System.Windows.Forms.Button();
tabButton.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
tabButton.Cursor = System.Windows.Forms.Cursors.Hand;
tabButton.ForeColor = System.Drawing.Color.Lime;
tabButton.Location = new System.Drawing.Point(154, 32);
tabButton.Name = "tabButton";
tabButton.Size = new System.Drawing.Size(152, 23);
tabButton.TabIndex = 13;
tabButton.Text = "Tab 2";
tabButton.UseVisualStyleBackColor = false;
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Controls.Add(tabButton);
}
}
I also have this click function:
private void tabButton_Click(object sender, EventArgs e)
{
tab_1.Enabled = true;
tabButton.Enabled = false;
}
"tab_1" is a button created in the design mode.
"tabButton.Enabled" is red marked because it cannot find tabButton.
I understand why it cannot be found. But I have no idea about how to solve the problem in a good way.

You are assigning the tabButton_Click to all buttons with this line:
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Just cast the sender to button and you will get the button who fired the event:
void tabButton_Click(object sender, EventArgs e)
{
Button buttonSender = (Button) sender;
buttonSender.Enabled=false;
}
You are not finding "tab_1" because it is not a valid name inside the tabButton_Click scope.
That's why you have to cast the sender object to WindowsForms Button, and then change its properties.

I'm going with a different method.
Creating all the buttons needed initially.
Sorry for wasting your time.

Related

C# WinForms contextmenu focus issue when textbox is added

In a C# WinForms application I need to create a ContextMenuStrip with dropdown and textbox:
private System.Windows.Forms.ContextMenuStrip ct1;
private void button_Click(object sender, EventArgs e)
{
var header = new ToolStripMenuItem("Header");
header.Enabled = false;
var options = new ToolStripMenuItem("Options");
for (int i = 0; i < 5; i++)
{
var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
checkoption.CheckOnClick = true;
options.DropDownItems.Add(checkoption);
}
var txt = new ToolStripTextBox();
txt.Text = "changeme";
options.DropDownItems.Add(txt);
options.DropDown.Closing += DropDown_Closing;
ct1.Items.Clear();
ct1.Items.Add(header);
ct1.Items.Add(options);
ct1.Show(this, button.Left, button.Top);
}
private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}
Now, e.Cancel will prevent closing the dropdown if the reason is ItemClicked, so I can select more items without having to open the menu again:
Please note that "changeme" is a ToolStripTextBox!
Once I focus it (click on it), I can edit the text inside:
After finish editing the textbox, I still can change the checkbox items, but there is no focus indicator:
How can I get back the focus indicator just as shown on the first picure?
Note: if I move the mouse onto "Header", the dropdown will close, and then moving it back to "Options", will reopen the dropdown and then the focus indicator is good again:
How can I do this without closing and reopening the dropdown?
I have tried Select() for the options item, but it did not help, neither Invalidate() on ct1.
Just have found it:
First needs to add a click handler on the dropdown:
options.DropDown.Click += DropDown_Click;
Then in the click handler it needs to be focused:
private void DropDown_Click(object sender, EventArgs e)
{
var dropdown = (ToolStripDropDown)sender;
dropdown.Focus();
}

NullReferenceException dynamically created controls, object seems to exist

In Win Forms I have these three test methods. First to create a button, second to create a tab control with two tabs and third to move created button to first tab.
private void button1_Click(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Location = new Point(24, 250);
przycisk.Name = "nowy";
przycisk.Text = "utworzony";
przycisk.Width = 131;
przycisk.Height = 23;
Controls.Add(przycisk);
}
private void button2_Click(object sender, EventArgs e)
{
TabControl zakladki = new TabControl();
zakladki.Location = new Point(208, 160);
zakladki.Name = "zakl";
zakladki.Height = 150;
zakladki.Width = 208;
zakladki.TabPages.Add("zakladka1", "pierwsza");
zakladki.TabPages.Add("zakladka2", "druga");
Controls.Add(zakladki);
}
private void button3_Click(object sender, EventArgs e)
{
TabControl zakladki = (TabControl)Controls.Find("zakl", false).FirstOrDefault();
int numerZakladki = 1;
foreach (TabPage zakladka in zakladki.TabPages)
{
Control kt = Controls["nowy"];
kt.Location = new Point(10, 10); // System.NullReferenceException
zakladka.Controls.Add(kt);
numerZakladki++;
}
}
I'm having a hard time to understand the behavior upon trying to change the referenced button location. The code above throws System.NullReferenceException, but when I do
if (kt != null)
{
kt.Location = new Point(10, 10);
}
it works as expected. Can anyone explain it to me ?
The new TabControl contains two Tabs.
If you move the button to the first Tab, the control at the Main form is null.
Code without Loop:
private void button3_Click(object sender, EventArgs e)
{
TabControl zakladki = (TabControl)Controls.Find("zakl", false).FirstOrDefault();
Control kt = Controls["nowy"];
kt.Location = new Point(10, 10);
zakladki.TabPages[0].Controls.Add(kt);
}
Your nowy controls are added to the root control (Form I guess), but you are trying to find them in the tab pages under zakl. You have to either add the controls to the tab page, or find them in the root control, just like you did with zakl.
You move the "nowy" button from the Form.Controls to the first TabPage's Controls collection. This removes the control from the first collection, so the code throws the exception on the next iteration. A Control can have only a singleParent.
Either create the Button for each tab separately (instead of moving it), or add the Button to the first tab only (without the foreach loop).

Adding actions to programmatically added buttons

I am creating a program where it is required to have a list of buttons which relate to different things on the host computer.
I have achieved this as shown in the code below (example code):
Button btn = new Button();
btn.Name = "Button1";
btn.Text = "Item 1";
this.formLayoutProject.Controls.Add(btn);
This will eventually go into a for loop and more buttons will be on the flow layout, but i am starting small scale and working up so i don't get problems later.
This produces the button as expected (formatting will be done here too but haven't got that far).
What I am stuck on is how would I add a click event/right click event context menu onto the button?
I can put the click events in the code for buttons and items which will be fixed on the form and they work but I don't know how to do this dynamically as the buttons will be different on each target machine.
I have searched on Google but I haven't found what im looking for (probably due to the fact im not sure what the keywords are for the search).
How would I accomplish this?
You can add a button click event handler by using += operator on the event of your new btn object:
// Add 10 buttons dynamically.
// Bind to the same method.
for (var i = 1; i <= 10; i += 1)
{
Button btn = new Button();
btn.Name = "Button" + i;
btn.Text = "Item " + i;
this.formLayoutProject.Controls.Add(btn);
// Add handler.
btn.Click += btn_Click;
}
Now you just need to define the handler method:
private void btn_Click(object sender, EventArgs e)
{
// This is the button which was clicked.
var button = (Button)sender;
var buttonName = button.Name;
// Do some stuff.
// To detect certain mouse click events, cast e as a MouseClick.
MouseEventArgs mouseEvents = (MouseEventArgs)e;
// Now use the mouseEvents object to handle specific events based on the button clicked.
if (mouseEvents.Button == MouseButtons.Right)
{
// Right button clicked.
}
else if (mouseEvents.Button == MouseButtons.Left)
{
// Left button clicked.
}
}

Accept Button doesn't work

I have a problem with the accept-button inside a Windows Form.
The Form contains two buttons (OK and Cancel). Inside the Form I set the properties of the cancel and accept - Button to the specific buttons.
In addition to that I also created a simple Click - Event for both buttons.
But when I run the application and press enter the breakpoint inside my Click-Method doesn't get hit und nothing happens. On the other hand, the cancel button just works fine. Even if I switch the accept- and cancelbutton the acceptbutton doesn't work and the application seems to ignore the enter-input.
I have looked up the designer several times but couldn't find anything which could lead to this behaviour.
The Click Event itself also works fine when clicking the button, it's just about the enter-input.
So my question is: Does someone have a clue where this strange behaviour comes from?
Designer:
//
// SearchForm
//
this.AcceptButton = this.BtnSearch;
this.CancelButton = this.BtnCancel;
//
//BtnSearch
//
this.BtnSearch.DialogResult = System.Windows.Forms.DialogResult.OK;
this.BtnSearch.Location = new System.Drawing.Point(12, 60);
this.BtnSearch.Name = "BtnSearch";
this.BtnSearch.Size = new System.Drawing.Size(75, 23);
this.BtnSearch.TabIndex = 1;
this.BtnSearch.Text = "Search";
this.BtnSearch.Click += new System.EventHandler(this.BtnSearch_Click);
//
// BtnCancel
//
this.BtnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.BtnCancel.Location = new System.Drawing.Point(108, 60);
this.BtnCancel.Name = "BtnCancel";
this.BtnCancel.Size = new System.Drawing.Size(75, 23);
this.BtnCancel.TabIndex = 5;
this.BtnCancel.Text = "Cancel";
this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
Form:
private void BtnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void BtnSearch_Click(object sender, EventArgs e)
{
//DoStuff
}
Check what control has the focus when you press Enter. If that's a button then the key stroke is going to click that button, not the AcceptButton.
That makes AcceptButton a pretty lame property for dialogs that have more than an OK and Cancel key. Favor doing it like this instead:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Enter) {
btnSearch.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Are you showing the form as modal dialog? I think accept and cancel buttons only works for modal dialog. The example given in MSDN article shows modal dialog.

DataGridView Right Click Specific Column for ContextMenuStrip

With C# I am trying to only show a ContextMenuStrip (CMS) when I right click a specific column in my DataGridView. I am confused as to whether I should be using a DataGridView_CellContentClick and/or dataGridView1.HitTest(). Then to finish off my problem I want to send the data from that right clicked cell to a new form window.
My current code has strange behavior. It will not show the CMS unless I have first left click or right clicked the 4th column. However once I have it will always show the CMS on a right click.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
//Create the ContextStripMenu for Creating the PO Sub Form
ContextMenuStrip Menu = new ContextMenuStrip();
ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
//Assign event handlers
MenuOpenPO.MouseUp += new MouseEventHandler(MenuOpenPO_Click);
Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
//Assign created context menu strip to the Datagrid
dataGridView1.ContextMenuStrip = Menu;
}
}
void MenuOpenPO_Click(object sender, MouseEventArgs e)
{
var ht = dataGridView1.HitTest(e.X, e.Y);
MessageBox.Show("Hello2");
PO_Form POWindow = new PO_Form();
POWindow.Show();
}
I was going to use the var ht = dataGridView1.HitTest(e.X, e.Y); to grab the cell value.
Any help would be appreciated, thanks!
Edit 1
So I updated dataGridView1_CellContentClick to this and it gets me very close to the behaviour I am looking for. If I first left click in column 4 then right click I get my CMS. If I left click any other cell in another column then right click the CMS is no longer there. However I want to be able to just right click a cell in column 4 without having to left click first to create the CMS.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
//MessageBox.Show("Hello1");
//Create the ContextStripMenu for Creating the PO Sub Form
ContextMenuStrip Menu = new ContextMenuStrip();
ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
//Assign event handlers
MenuOpenPO.MouseUp += new MouseEventHandler(MenuOpenPO_Click);
Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
//Assign created context menu strip to the Datagrid
dataGridView1.ContextMenuStrip = Menu;
}
else
dataGridView1.ContextMenuStrip = null;
}
I did this in VB ..
Private Sub DGV_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
DGV.CurrentCell = DGV.Rows(e.RowIndex).Cells(e.ColumnIndex)
CMS.Show(DGV, DGV.PointToClient(Windows.Forms.Cursor.Position))
End If
End Sub
I figured out my own problem and so will post the solution. Unfortunately I am very new at C# programming and really extremely rusty at it in general. I struggled with the concept of Event handlers (not sure if correct name?), how they were being called and how they all vary depending on if you are using MouseEventArgs, EventArgs, KeyEventArgs, etc. Anyway I digress.
My solution is below. I found the using dataGridView1.MouseUp gave me poor user interaction results requireing 2 of each action for a state change. IE create my ContextMenuStrip when the correct coloumn is right Clicked. Or make it disappear if any other column is right clicked. So use dataGridView1.MouseDown for better results.
dataGridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown);
private void dataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var ht = dataGridView1.HitTest(e.X, e.Y);
if (ht.ColumnIndex == 4)
{
//Create the ContextStripMenu for Creating the PO Sub Form
ContextMenuStrip Menu = new ContextMenuStrip();
ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
//Assign created context menu strip to the DataGridView
dataGridView1.ContextMenuStrip = Menu;
}
else
dataGridView1.ContextMenuStrip = null;
}
}
If you want to create a new form or do something else I used the following code when clicking your ContextMenuStrip I used the following after creating this event handler? MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
void MenuOpenPO_Click(object sender, MouseEventArgs e)
{
PO_Form POWindow = new PO_Form();
POWindow.Show();
}

Categories