In my apps, I generated a bunch of DataGridView programmaticaly. I can add/remove rows to it and retrieve all the data from it and copy the data in another DataGrid.
for instance when Im creating it
public void Example(TabControl tab)
for(int i=0;i<tab.TabCount;i++)
{
tab.TabPages.Add("Panneau " + tab.TabCount);
DataGridView panGridView = new DataGridView();
panGridView.Name = "panGridView_" + tab.TabCount;
panGridView.Location = new System.Drawing.Point(0, 0);
panGridView.RowTemplate.Height = 24;
panGridView.Size = new System.Drawing.Size(1375, 458);
panGridView.Columns.Add("id", "id");
panGridView.Columns.Add("part_code", "part_code");
panGridView.Columns[0].Width = 100;
panGridView.Columns[1].Width = 150;
panGridView.Visible = true;
panIndex.Items.Add(tab.TabCount - 1);
tab.TabPages[tab.TabCount - 1].Controls.Add(panGridView);
}
Now, I want to attach Method to this Control. I think the best way would be to attach it when I initialize the Control. For example I would attach a Method like this one
public void Action(DataGridViewCellMouseEventArgs e)
{
if(e.RowIndex==2)
{
MessageBox.Show("Hello");
}
I tried a lot of this but cant figure out how do it.
Thanks
It seems to me that you want a special kind of DataGridView. You want a DataGridView with an attached method. You've learned, whenever you need a "class, very similar to another class, but with just a small thing different", you need to create a derived class, or make a composition, if you don't want to expose all methods of the base class.
class DataGridViewWithAttachedMethod : DataGridView // TODO: invent proper name
{
...
}
class MySpecialDataGridView : UserControl
{
private DataGridView dgv1;
...
}
The advantage of the first method is that users of your class (= code, not operators) have access to all DataGridView methods, so it will be very flexible to use. Disadvantage: they have access to all DataGridView methods, so they can mess up your DataGridView.
Whether you will use derivation or composition depends on how fool proof your class needs to be, in other words: do you want to expose methods that you prefer not to be used by others?
I want to attach Method to this Control.
This is not really clear. Do you want to give DataGridView an extra method, always the same one? Or do you want to Dynamically attach a method: dgv1 has another attached method than dgv2.
class DgvWithExtraMethod : ...
{
public void Action(DataGridViewCellMouseEventArgs e)
{
if(e.RowIndex==2)
{
MessageBox.Show("Hello");
}
}
}
All instances of this dgv will have the same extra method. All you have to do is create an object of this class, and you will have this method.
However, if you want to attach different methods to instances of the class, you need a property that contains this method.
class DgvWithMethod : ...
{
public Action<DataGridViewCellMouseEventArgs> ExtraMethod {get; set;}
}
If you want your class fool proof, consider to initialize the method with a "no operation (NOP)"
private static Action<DataGridViewCellMouseEventArgs> NOP = (e) => {};
public Action<DataGridViewCellMouseEventArgs> ExtraMethod {get; set;} = NOP;
Usage:
DgvWithMethod dgv1 = new DgvWithMethod
{
ExtraMethod = (e) =>
{
if(e.RowIndex==2)
{
MessageBox.Show("Hello");
}
}
}
This is the exact answer to your question. However, what I think that you want to know is: if the operator click on row 2, then I want to execute method F(), and if he clicks on row 3, I want to execute method G(), etc
If that is what you want, use visual studio designer to add an event handler on DataGridView.CellMouseClick, or if you want to reuse this class (derivation / composition) override DataGridView.OnCellMouseClick.
private void DataGridView1_CellMouseClick(Object sender,
DataGridViewCellMouseEventArgs e)
{
// find out which column is clicked
switch (e.ColumnIndex)
{
case 0: // column Id clicked
this.ProcessColumnIdClick(e);
break;
case 1: // column Name clicked
this.ProcessColumnNameClick(e);
break;
...
Take care though: if you allow column reordering, you should compare ColumnIndex with the DisplayIndex of each column.
if (e.ColumnIndex == this.columnId.DisplayIndex)
this.ProcessColumnIdClick(e);
else if (e.ColumnIndex == this.columnName.DisplayIndex)
...
Like #CurleD stated, you simply subscribe your method to specific EventHandler. So change your Action to this:
private void panGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if(e.RowIndex==2)
{
MessageBox.Show("Hello");
}
}
and then subscribe to the CellMouseClick event:
panGridView.CellMouseClick += panGridView_CellMouseClick;
Related
I'm having trouble manipulating forms when from another thread.
I've overcome the issue by loading the form at runtime, showing it then hiding it. This means the form is created on the right thread and can be manipulated using invokes.
This is not the right way to do it. I have 3 problems that come from using this method
I can't spawn another popup box I have to use the one I created at runtime
The forms flash briefly on load - now that I have 3 forms its pretty obvious what I'm doing.
I have to use a variable bool to hold if the popup is open or not.
If anyone could point me in the right direction It would be much appreciated. Currently my code looks like:
On Main form Load:
CallerIDfrm = new frmCallerID();
CallerIDfrm.Show();
CallerIDfrm.Hide();
to manipulate the popup Im using
delegate void StringArgReturningVoidDelegate1(string CallerIDnum, string CallerIDname, string ContactID);
private void CallerID(string CallerIDnum, string CallerIDname, string ContactID)
{
if (CallerIDfrm.InvokeRequired)
{
StringArgReturningVoidDelegate1 d = new StringArgReturningVoidDelegate1(CallerID);
CallerIDfrm.Invoke(d, new object[] { CallerIDnum, CallerIDname, ContactID });
}
else
{
if (ContactID != null || ContactID != "0")
{
CallerIDfrm.ContactID = ContactID;
}
CallerIDfrm.Mainfrm = this;
CallerIDfrm.TopLevel = true;
CallerIDfrm.TopMost = true;
CallerIDfrm.lblCallerIDname.Text = CallerIDname;
CallerIDfrm.lblCallerIDnum.Text = CallerIDnum;
CallerIDfrm.Show();
CallerIDOpen = true;
}
}
To Hide the popup until required again im using:
delegate void StringArgReturningVoidDelegate2();
private void CallerIDClose()
{
if (CallerIDfrm.InvokeRequired)
{
StringArgReturningVoidDelegate2 d = new StringArgReturningVoidDelegate2(CallerIDClose);
CallerIDfrm.Invoke(d, new object[] { });
}
else
{
try
{
CallerIDfrm.Hide();
CallerIDOpen = false;
}
catch
{
}
}
}
I've tried otherways but the Popup loads as if it is not responding and I loose access to the popup.
Ultimately I'd like to be able to spawn multiple popups and have the ability to close them from the Main Form.
What I gather from your question: You have an caller api/lib/class and you like to show CallerId on a popup form when a call is received. Have a look at Events and Event Driven programming.
The following codes has not been tested, I wrote it from top of my head. Might not compile, they are here to show an example:
Create an CallReceived event in api/lib class as follows:
public event EventHandler<CallReceivedEventArgs> CallReceived;
protected void OnCallReceived(EventArgs e)
{
var handler = CallReceived;
if (handler != null)
handler(this, e);
// Note: For C# 6.0 and later, above statements can be simplified to
// CallReceived?.Invoke(this, e);
}
Note: If you don't have access to this api/lib code, create a Gateway class and put your event in there along with mechanism to trigger it.
Also create a CallReceivedEventArgs, this will be used to transfer event data:
public class CallReceivedEventArgs : EventArgs
{
public string CallerIDnum {get; set;}
public string CallerIDname {get; set;}
public string ContactID {get; set;}
}
Now, in your api/lib class raise this event whenever a call is received:
// a call received, replace dummy values with actual values
OnCallReceived(new CallReceivedEventArgs() { CallerIDnum="5554443322", CallerIDname="SOME_NAME", ContactID="SOME_CONTACT" });
Finally in your GUI form, register to said event and process accordingly.
// inside your main form class
private CallerAPI callerApi = new CallerAPI();
// somewhere inside you main form class, register to event
// from your use case, I would place it inside Main Form's constructor
callerApi.CallReceived += callerApi_Callreceived;
// receive event
void callerApi_Callreceived(object sender, CallReceivedEventArgs e)
{
var callerIDnum = e.CallerIDnum;
// etc.
// show callerId form with details
// you need to change frmCallerID's constructor accordingly
CallerIDfrm = new frmCallerID(e.CallerIDnum, CallerIDname, ContantID);
// to be able to track opened popups, you can store them inside a list
// private List<Form> openPopupList = new List<Form>();
//
// alternatively, you can assign CallerIDnum to form's name property
// and store these inside a List<string> instead of List<Form>
openPopupList.add(CallerIDfrm);
CallerIDfrm.Show();
}
Don't forget to unregister from event.
callerApi.CallReceived -= callerApi_Callreceived;
To wrap it up:
I can't spawn another popup box I have to use the one I created at runtime
You can create and show multiple frmCallerID, independent from each other.
The forms flash briefly on load - now that I have 3 forms its pretty obvious what I'm doing.
Since new approach creates CallerID forms based on events, you won't see these form flashing. It'll open whenever a CallReceived event is received.
I have to use a variable bool to hold if the popup is open or not.
A better approach would be: Register to forms FormClosed event, and remove from openPopupList accordingly.
frmCallerID.FormClosed += frmCallerID_FormClosed;
void frmCallerID_FormClosed(object sender, EventArgs e)
{
// remove form from openPopupList
frmCallerID closedPopup = (frmCallerID) sender;
openPopupList.remove(closedPopup);
}
I had been playing around with an idea for a game, and implementation was going fairly well, but I have hit a stumbling block.
Basically, I have a form, which will show talent trees. I am just going to use labels to display the relevant details, and I want to create them programmatically. The display part is working fine, the part I am having trouble with is adding an event handler to the labels.
I want to be able to pass data during the event handling, so that I can identify which specific label was clicked, but I am hitting a brick wall. So when a particular label is clicked, the name of its associated skill (just passing a string) will be sent to the event handler. Any help would be appreciated. Here is the relevant code that I have:
public void DisplayTree()
{
int i=0;
startPoint.X = 40;
startPoint.Y = 125;
foreach(SkillNode s in tree.tier1)
{
for (i=0; i < s.labels.Count;i++ )
{
//Displays a label for each available rank for a skill
s.labels.ElementAt(i).Text = (i+1).ToString()+"/"+s.maxRank.ToString();
s.labels.ElementAt(i).Location = startPoint;
startPoint.Y += s.labels.ElementAt(i).Height + 2;
s.labels.ElementAt(i).Name = "lbl"+s.name+i.ToString();
//Only enable it to be clicked if the user is at the correct rank
if (s.rank == i)
{
s.labels.ElementAt(i).Enabled = true;
}
else
{
s.labels.ElementAt(i).Enabled = false;
}
//Add Event here
//I want to pass the name of the skill with the event
this.Controls.Add(s.labels.ElementAt(i));
}
startPoint.X += s.title.Width + 5;
startPoint.Y = 125;
}
}
public void LabelClick()
{
//Code here to pick out the name of the label
}
Try this:
public void LabelClick()
{
Console.WriteLine(((Control)sender).Name);
}
When you create an event and want to follow the official C# styleguide, you follow the following pattern:
public delegate void {YourName}EventHandler(object sender, {YourName}EventArgs args);
public event {YourName}EventHandler EventName;
Every information about what happened in the event or can be manipulated by the subscriber is stored in a class that inherits EventArgs. The delegate also contains a reference to the sender, which is the object that fires the event.
When you fire an event you do the following, regularly in a protected method that has the same name as the Event with an "On" as prefix:
EventName?.Invoke(this, new {YourName}EventArgs() { Initialize Stuff });
As you can see, you can work with the sender and identify the object. In your case you could also change object sender to UIElement sender (or similar) to make it easier to identify stuff without a cast.
I am now creating an app, that allows user to create and retrieve data from local database. There is one column named LIKE with a default value of int 0. I have a button, but is there anyway that I can press that button then the default value of int 0 will become 1 ? Everytime I press it will add one to the default value int. How can I do that ?
Assuming your database context is set as in the tutorial I mentioned in the comment, your button click event can look like the following:
private void IncrementLikes_Click(object sender, RoutedEventArgs e)
{
//where Likes is your ObservableCollection keeping data from database
LikeItem oneAndOnly = Likes.First() as LikeItem;
//you pulled one and only LikeItem object, now you increment the likes count
oneAndOnly.LikesCount += 1;
//and here you tell the database that the changes need to be saved.
//This call can be delayed to the OnNavigatedFrom event,
// depending on your needs.
LikeDB.SubmitChanges();
}
and here is some supporting code that belongs to the .cs file of the page:
//your database context
private YourDatabaseDataContext LikeDB;
// Define an observable collection property that controls can bind to.
private ObservableCollection<LikeItem> _likes;
public ObservableCollection<LikeItem> Likes
{
get
{
return _likes;
}
set
{
if (_likes != value)
{
_likes = value;
NotifyPropertyChanged("Likes");
}
}
}
// Constructor
public MainPage()
{
InitializeComponent();
LikeDB = new YourDatabaseDataContext(YourDatabaseDataContext.DBConnectionString);
this.DataContext = this;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Define the query to gather all of the to-do items.
var likesFromDB = from LikeItem like in LikeDB.Likes
select like;
// Execute the query and place the results into a collection.
Likes = new ObservableCollection<LikeItem>(likesFromDB);
// Call the base method.
base.OnNavigatedTo(e);
}
Using the database for just one value may be too much, so you might want to look at other alternatives, such as Windows Phone settings.
i'm just a newbie in programming and i made a function, just one stupid problem and a very stupid question.Please don't rude, How do i call this function from a different form or class. or even in the same form
public void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i <= dtInfo.Rows.Count - 2;i++ )
{
Battery = Convert.ToDateTime(dtInfo.Rows[i].Cells[5].Value.ToString());
Oil = Convert.ToDateTime(dtInfo.Rows[i].Cells[14].Value.ToString());
Fran = Convert.ToDateTime(dtInfo.Rows[i].Cells[12].Value.ToString());
lastkm = int.Parse(dtInfo.Rows[i].Cells[13].Value.ToString());
batt = Battery - DateTime.Now;
doil = Oil - DateTime.Now;
dfran = Fran - DateTime.Now;
if (batt.Days <= 7)
{
dtInfo.Rows[i].Cells[5].Style.BackColor = Color.Green;
}
if (doil.Days <= 7)
{
dtInfo.Rows[i].Cells[14].Style.BackColor = Color.Green;
}
if (dfran.Days <= 7)
{
dtInfo.Rows[i].Cells[12].Style.BackColor = Color.Green;
}
if (lastkm <= 500)
{
dtInfo.Rows[i].Cells[13].Style.BackColor = Color.Green;
}
}
}
EDITED
when i first open my form the color change then when i reopen it
it wont change but when i trace the code the value of the color was change but not the color in the cell
That's an event handler, and you can't raise the event yourself unless:
The class exposes a protected (or even public) method to fire it directly, but that doesn't happen usually in the .NET classes.
You trigger it by doing what the actual event represents -- In your case, when the databinding between your grid and your source is complete.
You can call that method, though, but it wouldn't have any relevant meaning, since you wouldn't raise the event. Still, if the event logic doesn't matter and you just want that code to execute, you can do it through:
dataGridView1_DataBindingComplete(null, null);
But in that case, you can just wrap that method's content in a simple method with a returning type of void and no parameters.
dataGridView1_DataBindingComplete() is just a plain old method
dataGridView1_DataBindingComplete(this, new RoutedEventArs());
Should do the trick if you want to call it from the same object.
if you want to reuse that section of code I would take everything within the method and create a separate public method so you can call it from other places and from other objects.
It all depends where the function is located. If you have it under the same class as your form, you can just call it in the same scope. But from what I see, this is a event handler, and you shouldn't need to call it, as it handles the dataGridView event "DataBindingComplete". For more info about it, visit this.
However if you need to call it for some reason you can just do:
dataGridView1_DataBindingComplete(null, null)
I have a simple question in asp.net.
I want to know if it is possible to get data from controls in my user control directly . I want to do it without using Session variable,Viewstate ...
EDIT: I now use the method of declaring public variables in the UC.
Here is a part of Page_load from my parent page:
this.plan_action = (UCPlan)Page.LoadControl("~/Association/UCPlan.ascx");
PlaceHolder1.Controls.Add(this.plan_action);
if (this.plan_action.Validate == true)
{
CheckBox1.Checked = true;
//String référence = Session["liste_action"].ToString();
for (int i = 0; i < this.plan_action.List1.Count; i++)
{
Label8.Text += this.plan_action.List1[i].Référence + "/";
//Label8.Text += "/";
}
}
but my variable validate stay to false.
Here is the code where I change the value of the validate variable with it declaration:
private bool validate;
public bool Validate
{
get { return validate; }
set { validate = value; }
}
protected void Button2_Click(object sender, EventArgs e)
{
//myCommand.Connection = myConnection;
//Session["liste_action"] = this.List;
this.Validate = true;
//Response.Redirect("../Risques_folder/AjouterRisque.aspx");
}
Thank you for your help,
Quentin
UPDATE due to new information
You need to learn about the sequence of events in ASP.NET.
The Load of the page happens a long time before the Click handler of Button2 in your UserControl... so the Validate property is always going to be set to false.
You have two obvious options (as I see it)...
Keep the creation of the UserControl in your Page_Load (or preferably, move it to your Page_Init, as this is normally the most appropriate place for it). Then place your check for the Validate property in a Page_PreRender.
Or, create an Event in your UserControl, Raise that event on the click of Button2, and handle the event in the Page.
ANOTHER UPDATE
For the 2nd of the two options above, in your UserControl class have the following...
public delegate void ButtonClickedDelegate(object sender, EventArgs e);
public event ButtonClickedDelegate ButtonClicked;
In the Button2_Click method of the UserControl (after setting the this.Validate = true;) call...
ButtonClickedDelegate(sender, e);
In the Page_Init of the Page, put something like...
ctrl1.ButtonClicked += new UCPlan.ButtonClickedDelegate(ctrl1_ButtonClicked);
And then have a new method called something like
void ctrl1_ButtonClicked(object sender, EventArgs e)
{
if (ctrl1.Validate)
{
...
}
}
Remember, as you control the delegate you can pass whatever information you want, including an entire class. So instead of calling the Validate property, create a new instance of the class you want, and pass that as a delegate parameter.
You can find more information on delegates and events on MSDN.
ORIGINAL ANSWER
Unless I've missed something, this is a very simple ASP.NET concept...
You can create properties and/or methods.
For example, as a property...
public string MyProperty
{
get { return "My Property Value"; }
}
Or as a method
public string MyMethod()
{
return "My Method Value";
}
If you're talking about passing the values between the UserControl and the ASP.NET Page that contains it, then in your Page, you can simply call the property or method. If your control was called (for example) myCtrl, then you can something like...
string prop = myCtrl.MyProperty;
string meth = myCtrl.MyMethod();
(On the back of the great comment from AHMED EL-HAROUNY)
If you're talking about passing the values to the client side page, then you can use the same properties / methods directly in the HTML markup. However, in this case, the properties / method can be declared as protected rather than public
For instance, to display the value...
<%=MyProperty%>
Or
<%=MyMethod()%>
Or if you're going to use the value in javascript, something like...
var myProp = "<%=MyProperty%>";
Yes That is possible, But exposing the controls in the UserControl as Public.