How to change DataGridViewComboBoxCell to DataGridViewTextBoxCell? - c#

I am trying to change one cell inside a DataGridViewComboBoxColumn from
DataGridViewComboBoxCell into a DataGridViewTextBoxCell
here is the line i am typing in order to change it:
dataGridView1[0,3] = new DataGridViewTextBoxCell();
after running this line the cell remains in its DataGridViewComboBoxCell type.
Thanks in advance,
Nadav

I cannot distinguish any problems. The following form creates a DataGridView and provides a DataGridViewComboBoxColumn with three items asdf qwer and yxcv as possible combinations. During the Load event of the Form the grid is filled with four valid rows. Afterwards your line of code is performed, which performs as expected.
Am i missing something?
Here is the code:
public partial class Form1 : Form
{
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add("asdf");
dataGridView1.Rows.Add("qwer");
dataGridView1.Rows.Add("yxcv");
dataGridView1.Rows.Add("asdf");
dataGridView1[0, 3] = new DataGridViewTextBoxCell();
}
}
Designer File's code:
partial class Form1
{
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1});
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(470, 262);
this.dataGridView1.TabIndex = 0;
//
// Column1
//
this.Column1.HeaderText = "Column1";
this.Column1.Items.AddRange(new object[] {
"asdf",
"qwer",
"yxcv"});
this.Column1.Name = "Column1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(470, 262);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewComboBoxColumn Column1;
}

Related

Code executing after the form it is in has been closed and disposed

I have a Windows Forms App that is functioning in a way that I do not understand. I have created a simple app to reproduce the functionality to minimum logic for clarity. It has two forms.
Form1:
There is a button1 that when clicked adds rows to a datatable.
There is a button2 that displays a form and sends the datatable to the form.
Form2:
Form2 creates a dataview from the datatable and binds the dataview to a listbox. Form2 also has a button1 that does a this.Close() on the form. I read that anything opened with formName.Show() will be disposed of during .Close.
Here is where things are get weird. Form1 button1 can be clicked over and over each time clearing out the datatable and adds the rows again. Once from1 button2 is clicked and the form2 is displayed and then closed, going back to form1 and clicking button1 raises an error. The error is from the form2 (which has been closed) listBox1_SelectedIndexChanged event.
The question is, why is an event firing for a control that is gone on a form that is gone?
I have found several ways to avoid this but I am wanting to understand why it is happening such as setting the listbox datasource = null but would like to know what is going on.. Spent half a day trying to figure this out. SO community, please educate me here.
Form1 code
public partial class Form1 : Form
{
Boolean bInitialLoad = true;
DataTable dtHardware = new DataTable("Hardware");
Form2 multiServerView = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dtHardware.Clear();
if (bInitialLoad == true)
{
dtHardware.Columns.Add("ServerName", typeof(String));
dtHardware.Columns.Add("Environment", typeof(String));
}
DataRow drNewRow = dtHardware.NewRow();
drNewRow["ServerName"] = "SomeName";
drNewRow["Environment"] = "SomeEnvironment";
dtHardware.Rows.Add(drNewRow);
drNewRow = dtHardware.NewRow();
drNewRow["ServerName"] = "SomeName2";
drNewRow["Environment"] = "SomeEnvironment2";
dtHardware.Rows.Add(drNewRow);
bInitialLoad = false;
}
private void button2_Click(object sender, EventArgs e)
{
OpenMultiServerView();
}
private void OpenMultiServerView()
{
multiServerView = new Form2(dtHardware);
this.AddOwnedForm(multiServerView);
multiServerView.Show();
}
}
Form1 Designer
namespace WindowsFormsApp4
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(308, 390);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(107, 34);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(437, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(102, 33);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(679, 482);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
Form2 code
public partial class Form2 : Form
{
DataView dvServers = null;
public Form2()
{
InitializeComponent();
}
public Form2(DataTable dt1)
{
InitializeComponent();
dvServers = new DataView(dt1);
}
private void Form2_Load(object sender, EventArgs e)
{
listBox1.DisplayMember = "ServerName";
listBox1.DataSource = dvServers;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
System.Windows.Forms.FormCollection fc = Application.OpenForms;
foreach (System.Windows.Forms.Form frm in fc)
{
if (frm.Name == "Form2")
{
return;
}
}
MessageBox.Show("I am the listbox event from Form2");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
Form2 Designer
namespace WindowsFormsApp4
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(23, 113);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(749, 287);
this.dataGridView1.TabIndex = 0;
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(291, 31);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(171, 69);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(653, 415);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(118, 25);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
}
}
It's because the instance of Form2 isn't actually gone. You are correct in assuming that it has been disposed, since you called Show, but it has not been collected by the garbage collector. In fact, it can't be collected because in the code example provided Form1 has two references to the instance of Form2 that it created.
The first is in Form1.OwnedForms. The second is the field Form1.multiServerView.
You said you had a few ways to fix it, such as breaking the binding when Form2 closes, but I thought I'd just throw out this suggestion. If you don't actually need to create a new instance of Form2 every time you show it, you could just create one instance of it in the constructor of Form1, handle Form2.Closing and just hide Form2 when the user closes it.
So, something like...
public partial class Form1 : Form
{
//Removed bInitialLoad, we'll set up the data table in the constructor.
DataTable dtHardware = new DataTable("Hardware");
Form2 multiServerView; //No longer initalizing here, we'll do that in the constructor.
public Form1()
{
InitializeComponent();
dtHardware.Columns.Add("ServerName", typeof(String));
dtHardware.Columns.Add("Environment", typeof(String));
multiServerView = new Form2(dtHardware);
AddOwnedForm(multiServerView);
}
private void button1_Click(object sender, EventArgs e)
{
dtHardware.Clear();
//Removed the check of bInitialLoad.
DataRow drNewRow = dtHardware.NewRow();
drNewRow["ServerName"] = "SomeName";
drNewRow["Environment"] = "SomeEnvironment";
dtHardware.Rows.Add(drNewRow);
drNewRow = dtHardware.NewRow();
drNewRow["ServerName"] = "SomeName2";
drNewRow["Environment"] = "SomeEnvironment2";
dtHardware.Rows.Add(drNewRow);
//Removed setting bInitialLoad.
}
private void button2_Click(object sender, EventArgs e)
{
OpenMultiServerView();
}
private void OpenMultiServerView()
{
multiServerView.Show(); //Just show it.
}
}
public partial class Form2 : Form
{
DataView dvServers = null;
//Removed the empty constructor since Form1 no longer needs it.
public Form2(DataTable dt1)
{
InitializeComponent();
dvServers = new DataView(dt1);
}
private void Form2_Load(object sender, EventArgs e)
{
listBox1.DisplayMember = "ServerName";
listBox1.DataSource = dvServers;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("I am the listbox event from Form2");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_Closing(object sender, FormClosingEventArgs e)
{
//Don't forget to wire it up in the designer!
//If the user clicks "button1" or the "X" then just hide the form.
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
}
This way, there is only ever one instance of Form2 for Form1. You don't have to worry about instances of Form2 that are disposed but not collected yet triggering events when something on Form1 alters the data source.

How to delete record from database with checkbox in telerik radgridview C#

I want to delete record from database with checkbox in telerik radgridview
...
Please Help Me!
Thanks
See the code below. That should do what you want!!
namespace WindowsFormsApplication6
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button2 = new System.Windows.Forms.Button();
this.Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(113, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Import Data";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1});
this.dataGridView1.Location = new System.Drawing.Point(13, 43);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(692, 209);
this.dataGridView1.TabIndex = 1;
//this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(144, 13);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(113, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Export Data";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Column1
//
this.Column1.HeaderText = "Column1";
this.Column1.Name = "Column1";
//
// button3
//
this.button3.Location = new System.Drawing.Point(331, 13);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(183, 23);
this.button3.TabIndex = 3;
this.button3.Text = "Delete If CheckBox Checked";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(717, 264);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGridViewCheckBoxColumn Column1;
private System.Windows.Forms.Button button3;
}
}

Errors in C#, Visual Studio

I am extremely new to coding but I have interest so decided to take a class at my community college. I am completing a basic assignment creating a language translator for three words of my choosing. I have followed my book but still getting the errors. I have copied my code and errors below and appreciate any guidance.
-- using System;
namespace Language_Translator_Latin
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(21, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(85, 21);
this.button1.TabIndex = 0;
this.button1.Text = "Hello";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(149, 112);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(85, 21);
this.button2.TabIndex = 1;
this.button2.Text = "Happy";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(288, 112);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(85, 21);
this.button3.TabIndex = 2;
this.button3.Text = "Good night";
this.button3.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(81, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(201, 13);
this.label1.TabIndex = 3;
this.label1.Text = "Select a word and I will convert it to Latin";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(198, 76);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 164);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}
private void HelloButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Salve";
}
private void HappyButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Laeta";
}
private void GoodnightButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Bonum nox"
Errors:
Not too sure what errors you are getting, that part of your question is blank right now.
I do see some issues with the placement of the "private void HelloButton" and downward, it appears to be outside the class itself.
You have two lines which are
}
}
right above the section I mentioned, and it should only be one } followed by your private void HelloButton. Then at the end you are missing one }, plus the closing }.
Here is something closer to what you want. I don't have all of your code so only cleaned up the end part. You need to be more careful about matching up the { and }. Another thing is the placement of #endregion, which should stay up with the automated code which it refers to.
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 164);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private void HelloButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Salve";
}
private void HappyButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Laeta";
}
private void GoodnightButton_Click(object sender, EventArgs e)
{
translationLabel.Text = "Bonum nox";
}
}
}

How to pass value from one cs-file (void!) to another?

my little program works very well, reading an xml file with 3 config values, reading a json-http-page and so on.
now there is a little thing i want to do: display the values out of the xml in my settings dialogue. i got this (Program.cs):
using ...
//*****************************************************************************
namespace t2bl
{
abstract class TANSS2BL
{
public static NotifyIcon notico;
//==========================================================================
public static void Main(string[] astrArg)
{
ContextMenu cm;
MenuItem miCurr;
cm = new ContextMenu();
miCurr = new MenuItem();
miCurr.Index = 0;
miCurr.Text = "&Settings";
miCurr.Click += new System.EventHandler(SettingsClick);
cm.MenuItems.Add(miCurr);
miCurr = new MenuItem();
miCurr.Index = 1;
miCurr.Text = "Beenden";
miCurr.Click += new System.EventHandler(ExitClick);
cm.MenuItems.Add(miCurr);
notico = new NotifyIcon();
notico.Icon = new Icon("tanss.ico");
notico.Text = "TANSS Busylight Connector";
notico.Visible = true;
notico.ContextMenu = cm;
notico.DoubleClick += new EventHandler(NotifyIconDoubleClick);
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();
Application.Run();
}
public static void bw_DoWork(object sender, DoWorkEventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("C://bll.config.xml");
XmlNode xmlurl = doc.SelectSingleNode("/settings/url");
string url = xmlurl.FirstChild.Value;
XmlNode xmluid = doc.SelectSingleNode("/settings/userID");
string userid_string = xmluid.FirstChild.Value;
XmlNode xmlrefresh = doc.SelectSingleNode("/settings/refresh");
string refresh_string = xmlrefresh.FirstChild.Value;
int userid = Convert.ToInt32(userid_string);
int refresh = 1000 * (Convert.ToInt32(refresh_string));
var controller = new BusylightUcController();
while (true)
{
WebClient client = new WebClient();
var downloadString = client.DownloadString(url + "/module/busylight.php? user=" + userid);
JObject colors = JObject.Parse(downloadString);
//Console.WriteLine(colors["color"]); //Debug
var colorStatus = Convert.ToInt32(colors["color"]);
switch (colorStatus)
{
check the colors and set the light
}
Thread.Sleep(refresh);
GC.Collect();
}
}
//==========================================================================
protected static void ExitClick(Object sender, EventArgs e)
{
notico.Dispose();
Application.Exit();
}
//==========================================================================
protected static void SettingsClick(Object sender, EventArgs e)
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
// This should open the "Settings"-Popup, containing 3 textboxes and a button to save them to xml.
SettingsDiag form1 = new SettingsDiag();
form1.Show();
}
//==========================================================================
protected static void NotifyIconDoubleClick(Object sender, EventArgs e)
{
// ...
}
}
}
and on the other side this (Form1.Designer.cs):
namespace Settingsdialog
{
partial class SettingsDiag
{
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(50, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(163, 13);
this.label1.TabIndex = 0;
this.label1.Text = "TANSS Busylight connector v1.0";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(53, 65);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(159, 20);
this.textBox1.TabIndex = 1;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(271, 117);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "TANSS Busylight connector";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}
How do i pass the values "url", "userid_string", "refresh_string" from the backgroundworker void to the "textbox1.Text=" Output?
Thanks!
There are many ways to accomplish that, one you had in other answers, I can suggest another one:
Because your worker is set to run async, you can for example, report the update progress using the worker event and then in the handler update the textbox
bw.WorkerReportsProgress = true;
bw.ProgressChanged += (o, args) => { textBox1.Text = args.UserState.ToString();}
It's clear that in your bw.DoWorkevent handler you need to report the progress operation passing the UserState as the string value you want to report and update, so inside the event handler you need to call
bw.ReportProgress(0, "you value to report");
hope this helps
If you want to communicate to your form the result of the background worker you could define your form as a member of TANSS2BL (and init it in Main).
Then in bw_DoWork you can call a method on your form with the information you want to display as parameters.
As bw_DoWork is on another thread don't forget to test InvokeRequired and BeginInvoke in your form before setting your textBox value.

webbrowser component print qplot graph with an offset

does anyone has an idea concerning the webbrowser component that always print jqplot graphs with a weird offset ?
This very simple exemple loading a jqplot exemple page in a webbrowser component, showes the page correctly, but when you try to print it you get an offset in the graph only.
The same web page show and print correctly from within IE !
I also tryied this exemple programed in Delphi2007 and DelphiXE3 and got exactly the same results.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testwebbrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
wb.Navigate("http://www.jqplot.com/tests/bar-charts.php");
}
private void button2_Click(object sender, EventArgs e)
{
wb.ShowPrintPreviewDialog();
}
}
}
Form1.Designer.cs
namespace testwebbrowser
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.wb = new System.Windows.Forms.WebBrowser();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(597, 71);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(93, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// wb
//
this.wb.Dock = System.Windows.Forms.DockStyle.Fill;
this.wb.Location = new System.Drawing.Point(0, 71);
this.wb.MinimumSize = new System.Drawing.Size(20, 20);
this.wb.Name = "wb";
this.wb.Size = new System.Drawing.Size(597, 370);
this.wb.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(597, 441);
this.Controls.Add(this.wb);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.WebBrowser wb;
}
}

Categories