I'm looking to create 4 groups with 8 items in a group with check boxes. After these are checked I'd like individual webpages to open up. I'd like to be able to either click group button 1 to open up 8 pages or to individually check an item and have it open the webpage. My problem is when I click the group 1 button not all pages open up. Usually only 6 but I have had random number of pages open up. If i click individual the page opens up fine. Here is a snippet of the code.
private void btnGroup1_Click(object sender, EventArgs e)
{
foreach (Control c in groupBox1.Controls)
{
if (c is CheckBox)
{
CheckBox cb = (CheckBox)c;
if (cb.Checked == false)
{
cb.Checked = true;
btnGroup1.Text = "Uncheck 1";
}
else
{
cb.Checked = false;
btnGroup1.Text = "Check 1";
}
}
}
}
private void chkNYALL_CheckedChanged(object sender, EventArgs e)
{
if (chkNYALL.Checked == true)
{
chkNYALL.Checked = true;
System.Diagnostics.Process.Start("www.google.com");
}
else
{
chkNYALL.Checked = false;
}
}
private void NYBAT_CheckedChanged(object sender, EventArgs e)
{
if (chkNYBAT.Checked == true)
{
chkNYBAT.Checked = true;
System.Diagnostics.Process.Start("www.google.com");
}
else
{
chkNYBAT.Checked = false;
}
}
Here's a quick sample that could get you going.
In this snippet, I am dynamically creating 2 GroupBox, into which I am dynamically creating multiple CheckBoxes, and 1 Button. Each CheckBox is subscribed to a CheckedChanged event handler, and the button is subscribed to a Click event.
When checked, a Checkbox will trigger the opening of a page.
When clicked, the Button will trigger the opening of all checked pages.
You will notice I use the CheckBox.Tag property to host the value (aka URL)
using System;
using System.Drawing;
using System.Windows.Forms;
namespace OpenWebPageOnCheckboxCheck_46934789
{
public partial class Form1 : Form
{
GroupBox gb1 = new GroupBox();
GroupBox gb2 = new GroupBox();
Button btn1 = new Button();
Button btn2 = new Button();
public Form1()
{
InitializeComponent();
InitGroupBoxes();
AddCheckboxesToGroup("check1", "www.google.com", gb1);
AddCheckboxesToGroup("check2", "www.yahoo.com", gb1);
AddCheckboxesToGroup("check3", "www.bing.com", gb1);
AddCheckboxesToGroup("check4", "www.duckduckgo.com", gb1);
AddCheckboxesToGroup("check1", "www.wikipedia.com", gb2);
AddCheckboxesToGroup("check2", "www.stackoverflow.com", gb2);
InitButtons();
}
private void InitButtons()
{
btn1.Text = "Open checked";
btn1.Click += Btn1_Click;
btn1.Location = new Point(145, 5);
gb1.Controls.Add(btn1);
btn2.Text = "Open checked";
btn2.Click += Btn2_Click;
btn2.Location = new Point(145, 5);
gb2.Controls.Add(btn2);
}
private void Btn2_Click(object sender, EventArgs e)
{
foreach (Control item in gb2.Controls)
{
if (item is CheckBox)
{
if (((CheckBox)item).Checked)
{
LaunchPage(item.Tag.ToString());
}
}
}
}
private void Btn1_Click(object sender, EventArgs e)
{
foreach (Control item in gb1.Controls)
{
if (item is CheckBox)
{
if (((CheckBox)item).Checked)
{
LaunchPage(item.Tag.ToString());
}
}
}
}
private void AddCheckboxesToGroup(string cbText, string cbValue, GroupBox gb)
{
CheckBox cb = new CheckBox();
cb.CheckedChanged += Cb_CheckedChanged;
cb.Text = cbText;
cb.Tag = cbValue;
if (gb.Controls.Count > 0)
{
cb.Location = new Point(gb.Controls[gb.Controls.Count - 1].Location.X, gb.Controls[gb.Controls.Count - 1].Location.Y + gb.Controls[gb.Controls.Count - 1].Height + 2);
}
else
{
cb.Location = new Point(5, 5);
}
gb.Controls.Add(cb);
}
private void Cb_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
LaunchPage(((CheckBox)sender).Tag.ToString());
}
}
private void LaunchPage(string pageURL)
{
System.Diagnostics.Process.Start(pageURL);
}
private void InitGroupBoxes()
{
gb1.Dock = DockStyle.Top;
gb1.BackColor = Color.Aqua;
gb2.Dock = DockStyle.Bottom;
gb2.BackColor = Color.DarkRed;
this.Controls.Add(gb1);
this.Controls.Add(gb2);
}
}
}
Related
I'm trying to use autocomplete. The input text comes from a custom made keyboard, made from a form.
I tried autocomplete feature from a simple textbox and text input from my keyboard and works fine. But when I input text from the custom keyboard, it doesn't work. The custom keyboard adds the input from a key listener Key_Click.
I tried adding an extra 'a' and adding the text as txtInput.Text += 'o'; but it didn't work.
Any ideas?
keyboard code:
public partial class frmTextInput : Form
{
public string input_Text { get; set; }
public frmTextInput(string TEXT,bool CTRL)
{
InitializeComponent();
AlternarTeclas(chkShift.Checked);
AgregarListenerTeclas();
var source = new AutoCompleteStringCollection();
List<string> box = Data.Data.SourcePatente();
foreach (var item in box)
{
source.Add(item);
}
txtInput.AutoCompleteCustomSource = source;
txtInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
private void btnSpace_Click(object sender, EventArgs e)
{
txtInput.Text = txtInput.Text + " ";
}
private void btnBorrar_Click(object sender, EventArgs e)
{
string str = txtInput.Text;
if (!string.IsNullOrEmpty(str))
{
txtInput.Text = str.TrimEnd(str[str.Length - 1]);
}
}
private void btnVolver_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnEnter_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
input_Text = txtInput.Text;
}
private void frmTextInput_Load(object sender, EventArgs e)
{
}
private void chkShift_CheckedChanged(object sender, EventArgs e)
{
AlternarTeclas(chkShift.Checked);
}
private void Key_Click(object sender, EventArgs e)
{
string key = sender.ToString();
if (chkShift.Checked)
{
key = key.ToUpper();
}
else
{
key = key.ToLower();
}
txtInput.Text = txtInput.Text + key.Substring(key.Length - 1);
}
private void AgregarListenerTeclas()
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length == 1 && c.Text != "←")
{
c.Click += Key_Click;
}
}
}
foreach (Control c in tabSymbol.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length == 1 && c.Text != "←")
{
c.Click += Key_Click;
}
}
}
}
private void AlternarTeclas(bool estaShiftApretado)
{
if (estaShiftApretado)
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length < 2)
{
c.Text = c.Text.ToUpper();
}
}
}
}
else
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length < 2)
{
c.Text = c.Text.ToLower();
}
}
}
}
}
private void btnSymbol_Click(object sender, EventArgs e)
{
tabTeclado.SelectTab(tabTeclado.SelectedIndex + 1);
}
private void btnTecAlfanumerico_Click(object sender, EventArgs e)
{
tabTeclado.SelectTab(tabTeclado.SelectedIndex - 1);
}
private void button1_Click(object sender, EventArgs e)
{
txtInput.Text += 'o';
}
}
txtInput.AutoCompleteCustomSource = source;
txtInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
This should be put in the designer file of your textbox, not here. Try that, and can you see your textbox text value change when you use the custom keyboard? What I understand is you catch click event for your custom keyboard and change txtInput.Text?
I made it work. First of all, it didn't work with textbox multiline.
Then, the correct way to input new chars was emulate the keyboard:
I was triying in function "Key_Click":
== txtInput.Text = txtInput.Text + key.Substring(key.Length - 1); ==> I don't work
Instead I used:
== txtInput.Focus(); // IMPORTANT
SendKeys.Send(key.Substring(key.Length - 1));
I'm creating dynamically buttons into a repeater like somebody recommend in previous questions I make into a Page_Init and I make some conditions but the problem persist, the button don't fire the assigned event
this is the first condition
public bool AssignClicked
{
get
{
return Convert.ToBoolean(ViewState["AssignClicked"]);
}
set
{
ViewState["AssignClicked"] = value;
}
}
then this is the code when I make click in the button that create the buttons changing into true the condition:
protected void DButton(object sender, EventArgs e)
{
AssignClicked = true;
Page_Init(sender, e);
Page_Load(sender, e);
}
And start the creations of the buttons
protected void Page_Init(object sender, EventArgs e)
{
if (AssignClicked)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
Button Btn_clic = (Button)sender;
var name = Btn_clic.Text;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();
var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
foreach (var team in TeamFCH)
{
Button newButton = new Button();
newButton.CommandName = "Btn" + Convert.ToString(team);
newButton.ID = "Btn_" + Convert.ToString(team);
newButton.Text = team;
newButton.CommandArgument = name;
newButton.Click += new EventHandler(newButton_Click);
Repeater1.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
}
but when I click over the button created this doesn't do anything
I am doing a Windows Form Application in c# in which I create a panel for every click i do in "Panel_Inside".
But my problem is when I have to select one especific panel. I do not know how can I identify each panel.
Also I'm having problems when Dragging and Dropping for the same reason.
public void Coloco_Figura(string figura, int Punto_X, int Punto_Y)
{
panel_foto = new Panel();
panel_foto.BackColor = Color.Transparent; //saco fondo
panel_foto.BackgroundImage = Image.FromFile(figura); //asigno imagen al panel
panel_foto.BackgroundImageLayout = ImageLayout.Stretch;
panel_foto.Size = new Size(45, 45);
panel_foto.Location = new Point(Punto_X - 10, Punto_Y - 10);
panel_foto.BringToFront();
panel1.SendToBack();
panel_inside.Controls.Add(panel_foto);
dame_x = Punto_X;
this.panel_foto.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDown);
this.panel_foto.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUp);
this.panel_foto.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMove);
}
here are mouse events
private void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
# region Borrar Notas
if (Estado_Borro == true)
{
foreach (Panel p in panel_inside.Controls)
{
if (p is Panel)
{
panel_inside.Controls.Remove(p);
if (panel_inside.Controls.Count == 0)
{
listanotas.Clear();
}
else
{
for (int i = 0; i < listanotas.Count; i++)
{
Notas nota = listanotas[i];
if (nota.posX == dame_x)
{
listanotas.Remove(nota);
}
}
}
}
}
Estado_Borro = false;
}
if (e.Button == MouseButtons.Left)
{
drag = true;
x = e.X;
y = e.Y;
}
}
public void MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
drag = false;
}
public void MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (drag)
{
this.panel_foto.Location = new Point(Cursor.Position.X-this.Left, Cursor.Position.Y-this.Top);
}
To identify each panel, first you must set the Name or Tag property:
panel_foto.Name = "panel_foto1";
Then in your event handler,do something like this:
Panel p=(Panel)sender;
if (p.Name == "panel_foto1")
{
//Do your staff
}
Ok here is my question. I assign value to each checkboxes I created, and am trying to put them into listbox only when they are checked, after a button click. So here is the code that I have written so far, in which when the button is clicked, both the values are written into listbox, whether they are checked or not, how can I make it work as I explained?
public Form1()
{
InitializeComponent();
btnOne.Click += btnOne_Click;
chckOne.CheckedChanged += chckOne_CheckedChanged;
chckTwo.CheckStateChanged += chckTwo_CheckStateChanged;
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(number1 + number2);
}
string number1 = "ONE", number2 = "TWO";
void chckOne_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
if (chk.Checked == true)
{
lstOne.Items.Add(number1);
}
}
void chckTwo_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
if (chk.Checked == true)
{
lstOne.Items.Add(number2);
}
}
Just define one method:
void chkBox_CheckedChanged(object sender, EventArgs e)
{
var chkBox = sender as CheckBox;
if (chk.Checked == true)
{
lstOne.Items.Add(chkBox.Text);
}
else
{
lstOne.Items.Remove(chkBox.Text);
}
}
And attach it all your CheckBox CheckedChanged events:
chckOne.CheckedChanged += chkBox_CheckedChanged;
chckTwo.CheckStateChanged += chkBox_CheckedChanged;
Or if you want to add all checked values in your Button click change your method like this:
void btnOne_Click(object sender, EventArgs e)
{
this.Controls.OfType<CheckBox>()
.Where(c => c.Checked == true)
.Select(c => c.Text)
.ForEach(text => lstOne.Items.Add(text));
}
List<CheckBox> cbList=new List<CheckBox>();
public Form1()
{
InitializeComponent();
btnOne.Click += btnOne_Click;
cbList.Add(chckOne);
cbList.Add(chckTwo);
//All the checkbox should be added into cbList.
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Clear();
var checked_checkbox = cbList.Where(cb=>cb.Checked==true).ToList();
if(checked_checkbox.Count>0)
{
checked_checkbox.ForEach(x=>lstOne.Items.Add(x.Text));// Maybe you want put text of checkbox into listbox.
}
}
I have a ListBox and I want to add a context menu to each item in the list. I've seen the "solution" to have the right click select an item and suppress the context menu if on white space, but this solution feels dirty.
Does anyone know a better way?
Just to elaborate a little further to what Frans has said...Even though the ListBox owns the ContextMenuStrip, you can still customize the items in the menu strip at the time it's opening. Thus customizing it's contents based on the mouse position within the listbox.
The example below selects the item in the listbox based on a right mouse click and then customizes a context menu strip based on the item the user right-clicked on. This is a simple example but should get you going: Add a listbox to a form and add this code:
#region Private Members
private ContextMenuStrip listboxContextMenu;
#endregion
private void Form1_Load( object sender, EventArgs e )
{
//assign a contextmenustrip
listboxContextMenu = new ContextMenuStrip();
listboxContextMenu.Opening +=new CancelEventHandler(listboxContextMenu_Opening);
listBox1.ContextMenuStrip = listboxContextMenu;
//load a listbox
for ( int i = 0; i < 100; i++ )
{
listBox1.Items.Add( "Item: " + i );
}
}
private void listBox1_MouseDown( object sender, MouseEventArgs e )
{
if ( e.Button == MouseButtons.Right )
{
//select the item under the mouse pointer
listBox1.SelectedIndex = listBox1.IndexFromPoint( e.Location );
if ( listBox1.SelectedIndex != -1)
{
listboxContextMenu.Show();
}
}
}
private void listboxContextMenu_Opening( object sender, CancelEventArgs e )
{
//clear the menu and add custom items
listboxContextMenu.Items.Clear();
listboxContextMenu.Items.Add( string.Format( "Edit - {0}", listBox1.SelectedItem.ToString() ) );
}
Hope that help.
This way the menu will pop up next to the mouse
private string _selectedMenuItem;
private readonly ContextMenuStrip collectionRoundMenuStrip;
public Form1()
{
var toolStripMenuItem1 = new ToolStripMenuItem {Text = "Copy CR Name"};
toolStripMenuItem1.Click += toolStripMenuItem1_Click;
var toolStripMenuItem2 = new ToolStripMenuItem {Text = "Get information on CR"};
toolStripMenuItem2.Click += toolStripMenuItem2_Click;
collectionRoundMenuStrip = new ContextMenuStrip();
collectionRoundMenuStrip.Items.AddRange(new ToolStripItem[] {toolStripMenuItem1, toolStripMenuItem2 });
listBoxCollectionRounds.MouseDown += listBoxCollectionRounds_MouseDown;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
var info = GetInfoByName(_selectedMenuItem);
MessageBox.Show(info.Name + Environment.NewLine + info.Date);
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Clipboard.SetText(_selectedMenuItem);
}
private void myListBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
var index = myListBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
_selectedMenuItem = listBoxCollectionRounds.Items[index].ToString();
collectionRoundMenuStrip.Show(Cursor.Position);
collectionRoundMenuStrip.Visible = true;
}
else
{
collectionRoundMenuStrip.Visible = false;
}
}
There's no other way: the context menu isn't owned by the item in the listbox but by the listbox itself. It's similar to the treeview control which also owns the context menu instead of the treenode. So whenever an item in the listbox is selected, set the context menu of the listbox according to the selected item.
And Here it is My Solution :
listBox_Usernames.ContextMenuStrip = contextMenuStripRemove;
listBox_Usernames.MouseUp += new MouseEventHandler(listBox_Usernames_MouseUp);
void listBox_Usernames_MouseUp(object sender, MouseEventArgs e)
{
int index = listBox_Usernames.IndexFromPoint(e.Location);
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (index != ListBox.NoMatches)
{
if (listBox_Usernames.SelectedIndex == index)
{
listBox_Usernames.ContextMenuStrip.Visible = true;
}
else
{
listBox_Usernames.ContextMenuStrip.Visible = false;
}
}
else
{
listBox_Usernames.ContextMenuStrip.Visible = false;
}
}
else
{
listBox_Usernames.ContextMenuStrip.Visible = false;
}
}
In XAML it shows like this:
<ListBox>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Add User..."/>
<MenuItem Header="Edit..."/>
<MenuItem Header="Disable"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
In one line of code (more for binding):
var datasource = new BindingList<string>( new List<string>( new string[] { "item1" } ) );
listbox.DataSource = datasource ;
listbox.ContextMenu = new ContextMenu(
new MenuItem[] {
new MenuItem("Delete",
new EventHandler( (s,ev) =>
datasource.Remove(listbox.SelectedItem.ToString())
)
)
});
private void buttonAdd_Click(object sender, EventArgs e)
{
datasource.Add( textBox.Text );
}
If its just a question of enabling or disabling context menu items, it might be more efficient to only do it when the context menu is launched rather than every time the list box selection changes:
myListBox.ContextMenu.Popup += new EventHandler(myContextPopupHandler);
private void myContextPopupHandler(Object sender, System.EventArgs e)
{
if (SelectedItem != null)
{
ContextMenu.MenuItems[1].Enabled = true;
ContextMenu.MenuItems[2].Enabled = true;
}
else
{
ContextMenu.MenuItems[1].Enabled = false;
ContextMenu.MenuItems[2].Enabled = false;
}
}
this one is best...
using System.Windows.Forms;
ContextMenuStrip menu;
this.menu.Items.AddRange(new ToolStripItem[] { this.menuItem });
this.listBox.MouseUp += new MouseEventHandler(this.mouse_RightClick);
private void mouse_RightClick(object sender, MouseEventArgs e)
{
int index = this.listBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
menu.Visible = true;
}
else
{
menu.Visible = false;
}
}
//Create and Initialize the contextMenuStrip component
contextMenuStrip_ListaAulas = new ContextMenuStrip();
//Adding an Item
contextMenuStrip_ListaAulas.Items.Add("Modificar");
//Binding the contextMenuStrip with the ListBox
listBox_Aulas.ContextMenuStrip = contextMenuStrip_ListaAulas;
//The solution below
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//select the item under the mouse pointer
listBox_Aulas.SelectedIndex = listBox_Aulas.IndexFromPoint(e.Location);
//if the selected index is an item, binding the context MenuStrip with the listBox
if (listBox_Aulas.SelectedIndex != -1)
{
listBox_Aulas.ContextMenuStrip = contextMenuStrip_ListaAulas;
}
//else, untie the contextMenuStrip to the listBox
else
{
listBox_Aulas.ContextMenuStrip = null;
}
}
I do like this, this works great and fast for me.
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (Listbox.SelectedItem == null)
e.Cancel = true;
}