remove chart controls dynamically - c#

I have created a chart dynamically, is there a way to remove it?
I want to create a button_click to remove the chart. Any advise? Especially this is created locally.
private void createchart()
{
var chartA = new Chart();
chartA.Size = new Size(50,50);
chartA.Left = coord_X2;
chartA.Top = coord_Y2;
var chartArea = new ChartArea();
chartA.ChartAreas.Add(chartArea);
var series = new Series();
series.Name = "TT";
series.ChartType = SeriesChartType.Column;
series.Color = System.Drawing.Color.Green;
chartA.Series.Add(series);
chartA.Series["TT"].Points.AddXY("score", dr["Score"]);
this.Controls.Add(chartA);
}
thanks a mil
Jeff

If you give it a Name like this:
chartA.Name = "myUniqueChartName";
you can remove it by that Name:
this.Controls.Remove(this.Controls["myUniqueChartName"]);
If the Name is not unique the Remove statement will first remove the first control of that name added and on each further call remove the next one added..

Related

WinForms: adding a control with c#

I'm new to c#, and I do know how to dynamically add controls, but I don't know how to set that control as this.[control_name]. Note that here, this is the Form.
This can be done statically with private System.Windows.Forms.[Control_type] [control-name];, but how would i do this from a method so that I can later declare this.[control-name] = [variable]?
Note here, variable would be something like new TextBox();
var txt = new TextBox(); //txt is the variable you are looking for
Form1.Controls.Add(txt); //added it to the form
Now you can access it by txt:
txt.Location = new Point(0,0);
txt.Visible = true;
If you create the control inside a method (as you mentioned in the comments), you can return and use it like below:
public TextBox AddTextBox()
{
var txt = new TextBox();
Form1.Controls.Add(txt);
return txt;
}
var newTxt = AddTextBox();
newTxt.Location = new Point(0,0);
newTxt.Visible = true;

Input data into a gridview with C#, ASP.net.

I am using a dataset that was created seperatley and is being used as a reference.
I need to create a gridview with data that comes from the dataset.
Coding is not the ASP.net code, but the C# code.
I just need to make one column of information.
Teacher has not taught us this and is on an assignment. If you can give me a link or type an example that would be great.
one way to doing is to bind the required columns with empty rows to the datatable and then binding the datatable to the girdview...i have given you a sample below
public void GenerateColumns()
{
dtblDummy = new DataTable("dtblDummy");
dtDummyColumn = new DataColumn("FirstName");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("LastName");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Email");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Login");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Password");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Role");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("RoleId");
dtblDummy.Columns.Add(dtDummyColumn);
}
public void GenerateRows(int intRow)
{
for(int intCounter = intRow; intCounter < intRow; intCounter++)
{
dtDummyRow = dtblDummy.NewRow();
dtDummyRow["FirstName"] = "";
dtDummyRow["LastName"] = "";
dtDummyRow["Email"] = "";
dtDummyRow["Login"] = "";
dtblDummy.Rows.Add(dtDummyRow);
}
dgrdUsers.DataSource = dtblDummy;
dgrdUsers.DataBind();
dtblDummy = null;
dtDummyRow = null;
dtDummyColumn = null;
}
in the above code dgrdUsers is the gridview control, and declare the dummy row, column and datatable above the page load function.
call the above two functions in your page load under ispostback....
dont forget the create the same no of columns as template column in your gridview...

how can I bind datagridview column to tooltip.text using gmap.net

I have a datagridview which have client name lat,long. I am able to point out all markers on map from datagridview but I want to display client name on marker tootltip text.
while (myReader.Read())
{
string Latitude = myReader["Latitude"].ToString();
string Longitude = myReader["Longitude"].ToString();
gMapControl1.Position = new PointLatLng(float.Parse(Latitude), float.Parse(Longitude));
GMarkerGoogle marker = new GMarkerGoogle(gMapControl1.Position, GMarkerGoogleType.red);
markersOverlay.Markers.Add(marker);
gMapControl1.Overlays.Add(markersOverlay);
marker.ToolTip = new GMapRoundedToolTip(marker);
marker.ToolTipText = this.dataGridView1.Columns[4].ToString;
}
}
I used this method to display name. You have already given the tooltiptext so you only need this:
marker.ToolTipMode = MarkerTooltipMode.Always;
marker.Size = new System.Drawing.Size(21,21);
marker.Offset = new System.Drawing.Point(-10,-10);
marker.ToolTip.Font = new Font("Arial",9,FontStyle.Bold);

Databinding---Inserting empty row in a combo-box with DropDownList

I'm new to Visual Studio and fixing a small bug in an application.
The combo box which exist is editable with DropDown eventhough it is has databinding. Because If I deleted a value, it would not save the change, I made it into a DropDownList; however, now I do not have the option of using null.
I have searched around and have been reading that I can insert an item before databinding.
I have the bits and peices of the codes from different functions for this combo box. Not sure where to exactly make the change.
if someone could point me in the right direction, that'd be great.
private System.Windows.Forms.ComboBox cmbSecCSR;
//----------------
this.cmbSecCSR = new System.Windows.Forms.ComboBox();
//---------------------
// cmbSecCSR
//
this.cmbSecCSR.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember = "Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112, 26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.MaxDropDownItems = 10;
this.cmbSecCSR.Name = "cmbSecCSR";
this.cmbSecCSR.Size = new System.Drawing.Size(184, 21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember = "Username";
this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered);
//
// csrBindingSource2
//
this.csrBindingSource2.DataMember = "CSR";
this.csrBindingSource2.DataSource = this.productionDS;
//-------------------------
//loadUnboundData();
cmbSecCSR.DataBindings.Add("SelectedValue", productionMasterBindingSource, "CSR2", true, DataSourceUpdateMode.OnPropertyChanged);
My Problem is similar to this: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fa277629-a65b-4799-9400-364f6f771739/
which is why I decided to change it to DropDownList;however, I don't know how to add the NULL Value in the DropDownList as it is bounded.

Getting full contents of a Datagrid using UIAutomation

I have need to retrieve all of the items in a Datagrid from an external application using UIAutomation. Currently, I can only retrieve (and view in UISpy) the visible items. Is there a way to cache all of the items in the Datagrid and then pull them? Here's the code:
static public ObservableCollection<Login> GetLogins()
{
ObservableCollection<Login> returnLogins = new ObservableCollection<Login>();
var id = System.Diagnostics.Process.GetProcessesByName("<Name here>")[0].Id;
var desktop = AutomationElement.RootElement;
var bw = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, id));
var datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "lv"));
var loginLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
foreach (AutomationElement loginLine in loginLines)
{
var loginInstance = new Login { IP = new IP() };
var loginLinesDetails = loginLine.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
for (var i = 0; i < loginLinesDetails.Count; i++)
{
var cacheRequest = new CacheRequest
{
AutomationElementMode = AutomationElementMode.None,
TreeFilter = Automation.RawViewCondition
};
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(AutomationElement.AutomationIdProperty);
cacheRequest.Push();
var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));
cacheRequest.Pop();
var myString = targetText.Cached.Name;
#region Determine data and write to return object
//Removed private information
#endregion
}
}
returnLogins.Add(loginInstance);
}
return returnLogins;
}
You can only retrieve the visible cells because you have table virtualization on.
Try disabling the virtualization (not always possible in all application but perhaps you want to move it into configuration and change it before testing)
I am 99% sure that this is not possible. UI Automation doesn't know about the data structures which are represented by the currently visible portion of a grid. It only sees what is visible. I think that you will have to page through the grid to get all the data (that is what I do).

Categories