Guest User

Untitled

a guest
Aug 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. Background worker not working
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. main ma = new main();
  5. ma.AddType(txtName.Text,txtURL.Text,12);
  6. this.Close();
  7. }
  8.  
  9. public void AddType(string name, string url, int interval)
  10. {
  11.  
  12. string path = Application.StartupPath + @"sites.xml";
  13. //create new instance of XmlDocument
  14. XmlDocument doc = new XmlDocument();
  15. //load from file
  16. doc.Load(path);
  17. //create node and add value
  18. XmlNode node = doc.CreateNode(XmlNodeType.Element, "site", null);
  19. node.InnerXml = "<Name>"+name+"</Name><URL>"+url+"</URL><Status></Status><Response-Time></Response-Time><Last-Checked></Last-Checked>";
  20. //add to elements collection
  21. doc.DocumentElement.AppendChild(node);
  22. //save back
  23. doc.Save(path);
  24. bwLoadXML.RunWorkerAsync();
  25. }
  26.  
  27. /////////////////////////////////
  28. ////Populate Grid from XML
  29. /////////////////////////////////
  30. private void bwLoadXML_DoWork(object sender, DoWorkEventArgs e)
  31. {
  32. gridPopulate();
  33. }
  34. private void gridPopulate()
  35. {
  36.  
  37. DataSet data = new DataSet(); string p = System.IO.Path.Combine(Application.StartupPath, "sites.xml");
  38. data.ReadXml(p);
  39. if (this.dataGrid.InvokeRequired)
  40. {
  41. this.dataGrid.Invoke(new MethodInvoker(delegate
  42. {
  43. this.dataGrid.DataSource = data;
  44. this.dataGrid.DataMember = "site";
  45. }));
  46. }
  47. else
  48. {
  49. this.dataGrid.DataSource = data;
  50. this.dataGrid.DataMember = "site";
  51. }
  52. int i = 0;
  53. foreach (DataGridViewColumn column in this.dataGrid.Columns)
  54. {
  55. if (i != 0)
  56. {
  57. if (column.Name == "Name" || column.Name == "Status" || column.Name == "URL" || column.Name == "Response-Time" || column.Name == "Last-Checked")
  58. {
  59. //column.AutoSizeMode
  60. column.Visible = true;
  61. //column.Width = (int)(dataGrid.Width * .2) + (column.Name.Length / 2)-9;
  62. /*if (column.Name == "URL")
  63. {
  64. ColumnHeader ch = new ColumnHeader();
  65. //ch.
  66. }*/
  67. }
  68. else
  69. {
  70. column.Visible = false;
  71. //dataGrid.Columns[i+1].CellType = new DataGridViewButtonColumn();
  72. //dataGrid.Columns[i+1].HeaderCell.
  73. }
  74. }
  75. i++;
  76. }
  77. if (this.dataGrid.InvokeRequired)
  78. {
  79. this.dataGrid.Invoke(new MethodInvoker(delegate
  80. {
  81. // If column 3 is the checkbox column, we sit it's resize mode to none:
  82. dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
  83. // Then we set the width:
  84. dataGrid.Columns[0].Width = 25;
  85. dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
  86. // If column 3 is the checkbox column, we sit it's resize mode to none:
  87. dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  88. // Finally we set the rest of the grid to fill or what ever resizing you need:
  89. dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  90. }));
  91. }
  92. else
  93. {
  94. // If column 3 is the checkbox column, we sit it's resize mode to none:
  95. dataGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
  96. // Then we set the width:
  97. dataGrid.Columns[0].Width = 25;
  98. dataGrid.Columns[0].DefaultCellStyle.Padding = System.Windows.Forms.Padding.Empty;
  99. // If column 3 is the checkbox column, we sit it's resize mode to none:
  100. dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  101. // Finally we set the rest of the grid to fill or what ever resizing you need:
  102. dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  103. }
  104. }
  105.  
  106. public class Form2 : ... {
  107. main ma;
  108.  
  109. public Form2(main ma) {
  110. this.ma = ma;
  111. }
  112.  
  113. private void Button1_Click(object sender, EventArgs e) {
  114. this.ma.AddType(txtName.Text, txtUrl.Text, 12);
  115. this.Close();
  116. }
  117. }
  118.  
  119. void DoingSomething() {
  120. Form2 form = new Form2(this); // <-- this is where you pass in main
  121. form.ShowDialog();
  122. }
Add Comment
Please, Sign In to add comment