Event handler function isn't firing up for created dynamically checkboxes - c#

First of all, I have to mention that i'm a newbie at c#. I'm developing an windows 8 metro app with xaml and c#.
I created an array of checkboxes dynamically and assigned a pointerpressed event to each of them.
the weird problem is that the pointerpressed event is not firing up the registerd function.
If i create the same checkboxes using design tool(I'm using visual Studio 2012), strangely the function does work this time.
As i don't know the number of checkboxes to be created till runtime, using design tool for handling checkboxes isn't an option.The code i'm running is as below:
CheckBox[] chk=new CheckBox[count];
int x=20; //Width of each checkbox(to be incremented for every iteration)
for( int i = 0; i < count ; i++ )
{
chk[i] = new CheckBox();
chk[i].HorizontalAlignment = HorizontalAlignment.Left;
chk[i].Margin = new Thickness(1100, x, 0, 0);
chk[i].VerticalAlignment = VerticalAlignment.Top;
chk[i].Height = 30;
chk[i].Width = 35;
chk[i].PointerPressed += new PointerEventHandler(chkhandler);
gridit.Children.Add(chk[i]);
x = x + 35;
}
private static void chkhandler(object obj, PointerRoutedEventArgs arg)
{
textblock1.Text="Testing"; //Sample Textblock
}
The checkboxes get checked during runtime but the associated function doesn't work.Thanks in advance

Try;
making the event handler public & non-static,
make sure nobody is eating up the events.
what about this:
chk[i].AddHandler(PointerPressedEvent,
new PointerEventHandler(SomeButton_PointerPressed), true);

Related

Dynamically Generate Groupboxes

I'm working on an inventory program and have finished the main functionality as a command line console app. I am now working on a version for winforms. I want to enable it to dynamically generate a Groupbox that holds some textboxes. I'd rather not design 50+ lines of multiple textboxes. Keep in mind I'm rather new to programming, having started with C# a year ago. I know next to nothing on Winforms.
I've tried to use dynamic item = new Groupbox();as a similar method allowed generation of objects at runtime. In the command line app, the way it works is that based on information given, a certain amount of objects are passed into the list _AllItems. I was thinking of generating the Groupboxes by using:
private void InitializeGroupBox()
{
foreach (Product product in Product._AllItems)
{
dynamic Item = new GroupBox();
}
}
But I have the feeling I'm nowhere near the correct method. Thanks to anybody who helps.
You will need to learn a bit more, but here is what I usually do to achieve what you asked.
internal class DynamicForm : Form
{
private FlowLayoutPanel mFlowLayoutPanel;
public DynamicForm()
{
mFlowLayoutPanel = new FlowLayoutPanel();
mFlowLayoutPanel.Dock = DockStyle.Fill;
// Add to this Form
this.Controls.Add(mFlowLayoutPanel);
InitializeGroupBox();
}
private void InitializeGroupBox()
{
mFlowLayoutPanel.SuspendLayout(); // Performance
for (int i = 1; i <= 20; i++) {
var groupBox = new GroupBox();
groupBox.Text = "GroupBox #" + i;
groupBox.Size = new Size(200, 50);
var textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
// Add the TextBox to GroupBox
groupBox.Controls.Add(textBox);
// Add to this Form
mFlowLayoutPanel.Controls.Add(groupBox);
}
mFlowLayoutPanel.ResumeLayout(); // after suspend, resume!
}
}

Why not getting `ButtonPressEvent`

I am experimenting a bit with monodevelop/c#/gdk and I was able to create a window with a DrawingArea correctly handling the expose event.
The mouse down events however are not dispatched and I don't understand why. The delegates have been set up in the code autogenerated by the gui designer:
this.da.ExposeEvent += new global::Gtk.ExposeEventHandler (this.OnDAExposeEvent);
this.da.ButtonPressEvent += new global::Gtk.ButtonPressEventHandler (this.OnDAButtonPressEvent);
this.da.MotionNotifyEvent += new global::Gtk.MotionNotifyEventHandler (this.OnDAMotionNotifyEvent);
and this is my initialization code:
public MainWindow () : base (Gtk.WindowType.Toplevel)
{
Build ();
Gdk.Color col = new Gdk.Color();
col.Red = col.Green = col.Blue = 0x8888;
da.ModifyBg(StateType.Normal, col);
var p = "wr/wn/wb/wq/wk/wp/br/bn/bb/bq/bk/bp".Split ('/');
for (int i = 0; i < p.Length; i++) {
pieces[p[i]] = new ImageSurface("/home/agriffini/x/chessboard/i" + p [i] + ".png");
}
da.Events |= (Gdk.EventMask.ButtonPressMask
| Gdk.EventMask.ButtonReleaseMask
| Gdk.EventMask.KeyPressMask
| Gdk.EventMask.PointerMotionMask);
}
however the handler function OnDAButtonPressEvent never gets called (checked by placing a breakpoint there).
What is the part that is missing?
Found the problem by looking at console output.
The issue is that event mask must be set before the widget is realized.
This means that when using monodevelop and the gui designer you must set event handlers using the gui in widget->properties->events because the autogenerated Build method will set the event mask at the proper time before realization.
Setting the event mask in the code before calling Build wouldn't work (widget have not been created yet); setting the mask after that call wouldn't work either (they've been already realized).

How to trigger a created checkbox change event c#

I created these checkboxes in my gridview to allows my users to select several rows. My problem is when the checkboxes are clicked they are suppose to store info for each one clicked. I placed breakpoints inside and the event is never triggered. Is this something I need to call in order to have this event run? I was under the impression it was like any other event where it just ran upon triggering it. I haven't found a generic help for these type of issue, it seems to be more specific problems. Do you have nay reference to help or any suggestions on how to allow my event to trigger properly?
On form load this is established
ckBox = new CheckBox();
//Get the column header cell bounds
Rectangle rect = this.dropdeadGridView.GetCellDisplayRectangle(0, -1, true);
ckBox.Size = new Size(18, 18);
//Change the location of the CheckBox to make it stay on the header
ckBox.Location = rect.Location;
ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
//Add the CheckBox into the DataGridView
this.dropdeadGridView.Controls.Add(ckBox);
Then this is where I declare what happens in the chkbox_CheckChanged Event
var rows = dropdeadGridView.Rows;
for (int j = 0; j < this.dropdeadGridView.RowCount; j++)
{
this.dropdeadGridView[0, j].Value = this.ckBox.Checked;
bool checkBoxValue = Convert.ToBoolean(dropdeadGridView.Rows[5].Cells[1].Value);
if (checkBoxValue)
{
values += rows[j].Cells[2] + ",";
CurrentOrders = values;
}
}
this.dropdeadGridView.EndEdit();
Hope you are using a windows application
http://csharp.net-informations.com/datagridview/csharp-datagridview-checkbox.htm
this link is telling how to add checkboxes in gridview. When changing selection the grids events like afteredit,validateedit etc will fire.
We are coding at there by checking the column number(if the column number matches with check box column number) do the operations which you mentioned in event at there
I'm less experianced in winforms and i used C1FlexGrid only(Equivalent will be there in windows grid also)
Add an unhook event call, before hooking the event.
ckBox.CheckedChanged -= new EventHandler(ckBox_CheckedChanged);
Mostly your problem will get solved

Contacting a dynamically created control

I have created a textBox control on run-time for my winform application. The control appears just find once the form loads up, and works great too. However, I have just run into a problem as I realize I do not know how to write the code to write to a dynamically created control.
Let's assume I have created a button (named "Button1") on design time. In Button1's click event, (Button1_Click), I would like to write the word "Hello" to a textBox control that won't be created until the application is executed. Some code below:
C# Code:
// Create the textBox control
TextBox new_textBox = null;
int x = 10;
int y = 10;
int xWidth = 300;
int yHeight = 200;
new_textBox = new TextBox();
new_textBox.Text = controlText;
new_textBox.Name = "textBox" + controlName;
new_textBox.Size = new System.Drawing.Size(xWidth - 10, yHeight - 10);
new_textBox.Location = new Point(x, y);
new_textBox.BringToFront();
new_textBox.Multiline = true;
new_textBox.BorderStyle = BorderStyle.None;
// Add the textBox control to the form
this.Controls.Add(new_textBox);
The Problem:
From Button1_Click event, I cannot get in contact with a control that has not even been created yet. Thus, Visual Studio will throw an obvious error that the control does not exist (because it doesn't).
So, is there some way to dynamically call a control, and more
specifically, a textBox control?
Thank you for any help on the matter,
Evan
Declare the new_textBox at class scope. Then the compiler can access it. For example:
class MyForm
{
TextBox new_textBox;
void InitializeTextBox()
{
new_textBox = new TextBox();
// initialization code here
// Add it to the form
this.Controls.Add(new_textBox);
}
void Button1_Click(...)
{
new_textBox.Text = "clicked";
}
You can make the new_textBox a class member (member of the form). You can again assign it a value and add to the forms controls later dynamically.
It would be a good practice to check if is null in the buttonClick event, though.

how to Creating buttons in a running application (runtime) plus its onclick event function using C#?

I want to create buttons or list of items on the basis of number of items in my database or from list of items in my array and for each item create a onclick function for either buttons or any list of items
How about:
int y = 10;
foreach (string name in names)
{
Button button = new Button();
button.Text = name;
button.Position = new Point(10, y);
y += 20;
button.Click += HandleButtonClick;
Controls.Add(button);
}
You might also store the buttons in an array or a list... there's nothing particularly special about GUI controls that stops you from creating them at execution time just like any other object.
If that doesn't help, please give more information about what you need to do that the above doesn't help you with.
I have done it also by looking at Visual Studio code.

Categories

Resources