It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have this code that allow the user to create a button array, any were in a from.
Here and here are two samples of what I am doing (on Create an account now? Click NO and then download the zip file).
The problem or questions is as follow:
I need to save what ever the user makes: for example if he added 5 buttons and saved it, next time when he opens his saved file he will see the 5 button in the same place he save them.
This will allow the user to send the file to another person with the same program and the other person take a look at the created file.
Because of the misunderstanding (comments below) I will post some code with is the second link above. BUT the problem is that I cant find any code to do what I am asking. guys i do not have any idea on how to even start, I mean I know how to save a image from a picture box or some text but the button array is another thing.
Here is the code Simple example code or you can download the second file posted by me and compile it.
ButtomArray.cs
using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ManageControls
{
public delegate void SendSelectedButton(object sender);
public class ButtonArray : Hashtable
{
private readonly Form HostForm;
public event SendSelectedButton SelectedButton;
Point buttonLocation;
int cntButton = 0;
public ButtonArray(Form host)
{
HostForm = host;
}
public void AddButton(int left, int top)
{
Button btnElement = new Button();
btnElement.Top = top;
btnElement.Left = left;
btnElement.Tag = cntButton;
btnElement.Cursor = System.Windows.Forms.Cursors.Default;
btnElement.FlatStyle = System.Windows.Forms.FlatStyle.System;
btnElement.Text = "Button " + cntButton.ToString();
btnElement.Click += new EventHandler(btnElement_Click);
btnElement.MouseDown += new MouseEventHandler(btnElement_MouseDown);
btnElement.MouseMove += new MouseEventHandler(btnElement_MouseMove);
this.Add(cntButton.ToString(), btnElement);
HostForm.Controls.Add(btnElement);
btnElement.BringToFront();
cntButton++;
}
public void RemoveButton(string btnIndex)
{
if (this.Count > 0)
{
HostForm.Controls.Remove((Button)this[btnIndex]);
this.Remove(btnIndex);
}
}
private void btnElement_Click(Object sender, System.EventArgs e)
{
if(null != SelectedButton)
SelectedButton(sender);
}
private void btnElement_MouseDown(object sender, MouseEventArgs e)
{
buttonLocation = e.Location;
}
private void btnElement_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Button)sender).Left += e.X - buttonLocation.X;
((Button)sender).Top += e.Y - buttonLocation.Y;
}
}
}
}
frmMain.cs
using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ManageControls
{
public partial class frmMain : Form
{
ButtonArray buttonArray;
bool isClicked = false;
Button btnSelected = new Button();
public frmMain()
{
InitializeComponent();
buttonArray = new ButtonArray(this);
this.MouseDown += new MouseEventHandler(frmMain_MouseDown);
buttonArray.SelectedButton += new
SendSelectedButton(buttonArray_SelectedButton);
}
private void buttonArray_SelectedButton(object sender)
{
btnSelected = sender as Button;
}
private void frmMain_MouseDown(object sender, MouseEventArgs e)
{
if (isClicked)
{
buttonArray.AddButton(e.X, e.Y);
this.Cursor = Cursors.Default;
isClicked = false;
}
}
private void tsButton_Click(object sender, EventArgs e)
{
isClicked = true;
this.Cursor = Cursors.Cross;
}
private void tsDelete_Click(object sender, EventArgs e)
{
buttonArray.RemoveButton(btnSelected.Tag.ToString());
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ManageControls
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
}
and the frmMain.Designer.cs
namespace ManageControls
{
partial class frmMain
{
/// <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.tsMain = new System.Windows.Forms.ToolStrip();
this.AddButton = new System.Windows.Forms.ToolStripButton();
this.toolStripButton1 = new System.Windows.Forms.ToolStripSeparator();
this.RemoveButton = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.openLabel1 = new System.Windows.Forms.ToolStripLabel();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.saveLabel2 = new System.Windows.Forms.ToolStripLabel();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.tsMain.SuspendLayout();
this.SuspendLayout();
//
// tsMain
//
this.tsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.AddButton,
this.toolStripButton1,
this.RemoveButton,
this.toolStripSeparator1,
this.openLabel1,
this.toolStripSeparator2,
this.saveLabel2,
this.toolStripSeparator3});
this.tsMain.Location = new System.Drawing.Point(0, 0);
this.tsMain.Name = "tsMain";
this.tsMain.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
this.tsMain.Size = new System.Drawing.Size(740, 25);
this.tsMain.TabIndex = 0;
this.tsMain.Text = "toolStrip1";
//
// AddButton
//
this.AddButton.Image = global::ManageControls.Properties.Resources.Button;
this.AddButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.AddButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.AddButton.Name = "AddButton";
this.AddButton.Size = new System.Drawing.Size(63, 22);
this.AddButton.Text = "Button";
this.AddButton.Click += new System.EventHandler(this.tsButton_Click);
//
// toolStripButton1
//
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(6, 25);
//
// RemoveButton
//
this.RemoveButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.RemoveButton.Image = global::ManageControls.Properties.Resources.Delete;
this.RemoveButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.RemoveButton.ImageTransparentColor = System.Drawing.Color.White;
this.RemoveButton.Name = "RemoveButton";
this.RemoveButton.Size = new System.Drawing.Size(57, 22);
this.RemoveButton.Text = "Delete";
this.RemoveButton.Click += new System.EventHandler(this.tsDelete_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
//
// openLabel1
//
this.openLabel1.Name = "openLabel1";
this.openLabel1.Size = new System.Drawing.Size(36, 22);
this.openLabel1.Text = "Open";
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
//
// saveLabel2
//
this.saveLabel2.Name = "saveLabel2";
this.saveLabel2.Size = new System.Drawing.Size(31, 22);
this.saveLabel2.Text = "Save";
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(740, 449);
this.Controls.Add(this.tsMain);
this.Name = "frmMain";
this.Text = "Manage Controls - Sample";
this.tsMain.ResumeLayout(false);
this.tsMain.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ToolStrip tsMain;
private System.Windows.Forms.ToolStripButton AddButton;
private System.Windows.Forms.ToolStripButton RemoveButton;
private System.Windows.Forms.ToolStripSeparator toolStripButton1;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripLabel openLabel1;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripLabel saveLabel2;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
}
}
You don't give much information and i'm not going to download your code. But you can create a class which represents the properties that a button can hold such as position / size / text and serialise a collection of this class. Google .NET serialisation and there are hundreds of links on the topic. You can serialise an array and deserialise it easily from a file to dynamically get back all the buttons, then loop through the deserialised collection and add them back to your form.
Related
I have some code that starts a new thread every time a button is clicked. Each new thread draws a randomly colored circle in a random location in a primitive graphics engine and is added to a Listview. The link to this engine is https://github.com/NAIT-CNT/GDIDrawer.
I need to find a way to remove and abort a thread based on whatever Listview item the user clicks.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GDIDrawer;
using System.Threading;
namespace ica7
{
public partial class Form1 : Form
{
CDrawer _gdi = new CDrawer();
List<Thread> _threads = new List<Thread>();
Random _rng = new Random();
Point _coords;
int _diameter;
bool _isRunning;
public Form1()
{
InitializeComponent();
_diameter = 10;
_isRunning = true;
btnStopThreads.Enabled = false;
}
private Color RandomColor()
{
return Color.FromArgb(_rng.Next(256), _rng.Next(256), _rng.Next(256));
}
private void DrawCircle(int diameter, Point coords, CDrawer gdi)
{
gdi.AddCenteredEllipse(coords, diameter, diameter, RandomColor());
}
private void btnStartThread_Click(object sender, EventArgs e)
{
btnStopThreads.Enabled = true;
Thread t = new Thread(() =>
{
int diameter = _diameter;
while (_isRunning)
{
_coords.X = _rng.Next(800);
_coords.Y = _rng.Next(600);
DrawCircle(diameter, _coords, _gdi);
Thread.Sleep(10);
}
});
_threads.Add(t);
ListViewItem lvi = new ListViewItem(t.ToString());
lboxThreads.Items.Add(lvi);
t.Start();
}
private void btnStopThreads_Click(object sender, EventArgs e)
{
_threads.ElementAt(lboxThreads.SelectedIndex).Abort();
lboxThreads.Items.RemoveAt(lboxThreads.SelectedIndex);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
_diameter = trackBar1.Value;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
_threads.ForEach(x => x.Abort());
}
}
}
Here is the designer file, in case it's needed:
namespace ica7
{
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.components = new System.ComponentModel.Container();
this.btnStartThread = new System.Windows.Forms.Button();
this.btnStopThreads = new System.Windows.Forms.Button();
this.lboxThreads = new System.Windows.Forms.ListBox();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.lblTbMin = new System.Windows.Forms.Label();
this.lblTbMax = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// btnStartThread
//
this.btnStartThread.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.btnStartThread.Location = new System.Drawing.Point(9, 11);
this.btnStartThread.Name = "btnStartThread";
this.btnStartThread.Size = new System.Drawing.Size(121, 23);
this.btnStartThread.TabIndex = 0;
this.btnStartThread.Text = "Start a Thread";
this.btnStartThread.UseVisualStyleBackColor = true;
this.btnStartThread.Click += new System.EventHandler(this.btnStartThread_Click);
//
// btnStopThreads
//
this.btnStopThreads.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.btnStopThreads.Location = new System.Drawing.Point(9, 40);
this.btnStopThreads.Name = "btnStopThreads";
this.btnStopThreads.Size = new System.Drawing.Size(121, 23);
this.btnStopThreads.TabIndex = 1;
this.btnStopThreads.Text = "Stop Selected Thread";
this.btnStopThreads.UseVisualStyleBackColor = true;
this.btnStopThreads.Click += new System.EventHandler(this.btnStopThreads_Click);
//
// lboxThreads
//
this.lboxThreads.FormattingEnabled = true;
this.lboxThreads.Location = new System.Drawing.Point(138, 11);
this.lboxThreads.Name = "lboxThreads";
this.lboxThreads.Size = new System.Drawing.Size(303, 134);
this.lboxThreads.TabIndex = 2;
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(9, 69);
this.trackBar1.Maximum = 50;
this.trackBar1.Minimum = 10;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(121, 45);
this.trackBar1.TabIndex = 3;
this.trackBar1.Value = 10;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// lblTbMin
//
this.lblTbMin.AutoSize = true;
this.lblTbMin.Location = new System.Drawing.Point(6, 101);
this.lblTbMin.Name = "lblTbMin";
this.lblTbMin.Size = new System.Drawing.Size(19, 13);
this.lblTbMin.TabIndex = 4;
this.lblTbMin.Text = "10";
//
// lblTbMax
//
this.lblTbMax.AutoSize = true;
this.lblTbMax.Location = new System.Drawing.Point(111, 101);
this.lblTbMax.Name = "lblTbMax";
this.lblTbMax.Size = new System.Drawing.Size(19, 13);
this.lblTbMax.TabIndex = 5;
this.lblTbMax.Text = "50";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(453, 157);
this.Controls.Add(this.lblTbMax);
this.Controls.Add(this.lblTbMin);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.lboxThreads);
this.Controls.Add(this.btnStopThreads);
this.Controls.Add(this.btnStartThread);
this.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.Name = "Form1";
this.Text = "Form1";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnStartThread;
private System.Windows.Forms.Button btnStopThreads;
private System.Windows.Forms.ListBox lboxThreads;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Label lblTbMin;
private System.Windows.Forms.Label lblTbMax;
private System.Windows.Forms.Timer timer1;
}
}
If there are superior ways of doing any of this, please let me know. This is my first foray into threads. This is homework.
I've just started to learn programming in C# and I have a problem. I made a simple application (just a window with two buttons). One button starts another program and the other button shows this window again in 2 minutes (it's a sort of reminder). There's no X button, but it can still be closed by ALTF4 which I want to disable.
I've tried e.Cancel = true;, but I am obviously doing something wrong. What I did was doubleclick on the main window, then void MainFormLoad(object sender, EventArgs e) appeared in the code and I changed EventArgs e to FormClosingEventArgs and pasted the above e.Cancel = true; in there.
I am getting the following error:
No overload for 'MainFormLoad' matches delegate 'System.EventHandler' (CS0123)
It refers me to this line in MainForm.Designer.cs:
this.Load += new System.EventHandler(this.MainFormLoad);
Here's the whole code:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Reminder
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
int duration = 0;
void Timer1Tick(object sender, EventArgs e)
{
duration++;
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = #"C:\ThisProgram.exe";
Process.Start(start);
Application.Exit();
if (duration == 1)
{
Timer.Stop();
}
}
void LaterClick(object sender, EventArgs e)
{
Timer.Enabled = true;
Timer.Start();
Hide();
}
void OKClick(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = #"C:\OtherExternalProgram.exe";
Process.Start(start);
Application.Exit();
}
void MainFormLoad(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
}
}
And here is the MainForm.Designer.cs code:
namespace Reminder
{
partial class MainForm
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Button OK;
private System.Windows.Forms.Timer Timer;
private System.Windows.Forms.Button Later;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.OK = new System.Windows.Forms.Button();
this.Timer = new System.Windows.Forms.Timer(this.components);
this.Later = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// OK
//
this.OK.Location = new System.Drawing.Point(12, 253);
this.OK.Name = "OK";
this.OK.Size = new System.Drawing.Size(75, 23);
this.OK.TabIndex = 0;
this.OK.Text = "OK";
this.OK.UseVisualStyleBackColor = true;
this.OK.Click += new System.EventHandler(this.OKClick);
//
// Timer
//
this.Timer.Interval = 24000;
this.Timer.Tick += new System.EventHandler(this.Timer1Tick);
//
// Later
//
this.Later.Location = new System.Drawing.Point(425, 253);
this.Later.Name = "Later";
this.Later.Size = new System.Drawing.Size(75, 23);
this.Later.TabIndex = 1;
this.Later.Text = "Later";
this.Later.UseVisualStyleBackColor = true;
this.Later.Click += new System.EventHandler(this.LaterClick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Window;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.ClientSize = new System.Drawing.Size(512, 288);
this.Controls.Add(this.Later);
this.Controls.Add(this.OK);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MainForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.TopMost = true;
this.Load += new System.EventHandler(this.MainFormLoad);
this.ResumeLayout(false);
}
}
}
Thank you in advance for your help.
I am working on a Visual Studio 2010 C# web form application. I have this working apart from a display message when the application looses its internet connection. As it stands, when an internet connection can ot be found it displays a blank screen. In my coding I have some code which should catch a comms disconnection and display "Trying to connect ...[{0}]" with a countdown in seconds. The issue I have is the message is not displaying. Can any one see where I am going wrong.
My MainForm.cs:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Dis_Browser
{
public partial class MainForm : Form
{
public MainForm()
{
if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet())
{
InternetExplorerBrowserEmulation.SetBrowserEmulationVersion();
}
InitializeComponent();
Cursor.Hide();
}
private void MainForm_Load(object sender, EventArgs e)
{
browser.DocumentTitleChanged += ValidateConnection;
//this.WindowState = FormWindowState.Maximized;
}
private bool offline;
private void ValidateConnection(object sender, EventArgs e)
{
if (offline)
return;
//if (browser.DocumentTitle == "Internet Explorer cannot display the webpage" || browser.DocumentTitle == "Navigation Canceled" || browser.DocumentTitle == "This page can’t be displayed" || browser.DocumentTitle == "Broadband Link - Error")
if (browser.DocumentTitle != string.Empty)
{
offline = true;
browser.Visible = false;
ThreadPool.QueueUserWorkItem(obj => WaitConnection());
return;
}
browser.Visible = true;
}
private int countdown = 20;
private void WaitConnection()
{
while (!Native.IsOnline("http://www.google.com"))
{
Invoke((MethodInvoker)(() => { connectLbl.Text = String.Format(" Trying to connect ...[{0}]", countdown--); }));
if (countdown == 0)
countdown = 20;
Thread.Sleep(TimeSpan.FromSeconds(5));
}
offline = false;
Invoke((MethodInvoker)(() =>
{
browser.Refresh();
browser.Visible = true;
countdown = 20;
}));
}
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void connectLbl_Click(object sender, EventArgs e)
{
}
}
public class Native
{
[DllImport("wininet.dll", SetLastError = true)]
static extern bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);
public static bool IsOnline(string url)
{
return InternetCheckConnection(url, 1, 0);
}
}
}
My MainForm.Designer.cs
namespace Dis_Browser
{
partial class MainForm
{
///
/// Required designer variable.
///
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.browser = new System.Windows.Forms.WebBrowser();
this.connectLbl = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// browser
//
this.browser.Dock = System.Windows.Forms.DockStyle.Fill;
this.browser.Location = new System.Drawing.Point(0, 0);
this.browser.Margin = new System.Windows.Forms.Padding(0);
this.browser.MinimumSize = new System.Drawing.Size(1920, 1080);
this.browser.Name = "browser";
this.browser.ScriptErrorsSuppressed = true;
this.browser.ScrollBarsEnabled = false;
this.browser.Size = new System.Drawing.Size(1920, 1080);
this.browser.TabIndex = 0;
this.browser.Url = new System.Uri("http://www.google.com", System.UriKind.Absolute);
this.browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.browser_DocumentCompleted);
//
// connectLbl
//
this.connectLbl.Dock = System.Windows.Forms.DockStyle.Fill;
this.connectLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.connectLbl.Location = new System.Drawing.Point(0, 0);
this.connectLbl.MaximumSize = new System.Drawing.Size(1920, 1080);
this.connectLbl.MinimumSize = new System.Drawing.Size(1920, 1080);
this.connectLbl.Name = "connectLbl";
this.connectLbl.Size = new System.Drawing.Size(1920, 1080);
this.connectLbl.TabIndex = 1;
this.connectLbl.Text = " Trying to connect ...[20] Please check your Internet router";
this.connectLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.connectLbl.Click += new System.EventHandler(this.connectLbl_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1920, 1080);
this.Controls.Add(this.browser);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximumSize = new System.Drawing.Size(1920, 1080);
this.MinimumSize = new System.Drawing.Size(1920, 1080);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Dis Systems";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.WebBrowser browser;
private System.Windows.Forms.Label connectLbl;
}
}
I know there are many ways to write this but this is my attempt. Many thanks in advance for your time.
I'm using Nlog in my project, and now there's a problem, here it is:
I want to write logs into a RichTextBox, everything is OK at first, but when I clear the lines in RTB, and then restart to write logs, it goes wrong, it will not show any line in the RTB.
But, if I use
log.Error("Error\n"); // there is a "\n" in the end
instead of
log.Error("Error");
it will be OK, I don't know why is that...
any clue of this? thanks very much for any suggestion.
BTW I'm using Win7 X64, VS 2013, C# WinForm, .NET 4.0, NLog 3.2.0
using System;
using System.Windows.Forms;
using NLog;
using NLog.Targets;
namespace RTBLogDemo
{
public partial class LogForm : Form
{
#region Designer
/// <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.components = new System.ComponentModel.Container();
this.logRichTextBox = new System.Windows.Forms.RichTextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.logTimer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// logRichTextBox
//
this.logRichTextBox.Dock = System.Windows.Forms.DockStyle.Bottom;
this.logRichTextBox.Location = new System.Drawing.Point(0, 173);
this.logRichTextBox.Name = "logRichTextBox";
this.logRichTextBox.Size = new System.Drawing.Size(656, 299);
this.logRichTextBox.TabIndex = 0;
this.logRichTextBox.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(100, 48);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 39);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(261, 48);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 39);
this.btnStop.TabIndex = 2;
this.btnStop.Text = "stop";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(417, 48);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 39);
this.btnClear.TabIndex = 3;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// logTimer
//
this.logTimer.Tick += new System.EventHandler(this.logTimer_Tick);
//
// LogForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(656, 472);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.logRichTextBox);
this.Name = "LogForm";
this.Text = "Log Form";
this.Load += new System.EventHandler(this.LogForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox logRichTextBox;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Timer logTimer;
#endregion
public LogForm()
{
InitializeComponent();
}
private Logger log;
private void logTimer_Tick(object sender, EventArgs e)
{
log.Error("Error"); // WRONG
// log.Error("Error\n"); // OK
}
private void btnStart_Click(object sender, EventArgs e)
{
logTimer.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
logTimer.Stop();
}
private void btnClear_Click(object sender, EventArgs e)
{
logRichTextBox.Clear();
}
private void InitLogger()
{
RichTextBoxTarget target = new RichTextBoxTarget();
target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
target.ControlName = logRichTextBox.Name;
target.FormName = this.Name;
target.UseDefaultRowColoringRules = true;
target.AutoScroll = true;
target.MaxLines = 50;
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
log = LogManager.GetCurrentClassLogger();
}
private void LogForm_Load(object sender, EventArgs e)
{
InitLogger();
}
}
}
I have pasted two function from my code. They are for adding a tab in tabcontrol and removing a tab in tabcontrol. Both the functions are inside the same form where tabcontrol resides.
I am able to add the tabs in the tabcontrol with ease. I am calling AddTab() from a another class. And it works perfectly.
I am trying to do the same thing for Removing a tab from another class. But tabpage tp always returns null even though there are still two tabs in my tabcontrol.
what am i missing ??
public void AddTab(string strProcessName)
{
try
{
Global.ExistingTabProcessNames.Add(strProcessName);
this.Show();
//this below line dosent makes duplicate tabs.
TabPage tp = new TabPage();
tp.Text = strProcessName;
tp.Name = strProcessName;
tabControl1.TabPages.Add(tp);
//Activate the newly created Tabpage.
tabControl1.SelectedTab = tp;
tabControl1.ItemSize = new Size(200, 32);
tp.Height = tp.Parent.Height;
tp.Width = tp.Parent.Width;
}
catch (Exception ex)
{
}
}
public void RemoveUnusedTabs(string strTabToRemove)
{
TabPage tp = tabControl1.TabPages[strTabToRemove];
tp.Controls.Remove(this);
tabControl1.TabPages.Remove(tp);
}
I am calling the RemoveUnusedTabs from another class like below..
//create a instance for that class.
Taskbar RemoveTabs = new Taskbar();
RemoveTabs.RemoveUnusedTabs(strTabtoRemove);
I would recommend taking a look at this tab page example for adding/removing tabs.
Here is a simple example:
Form1.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TabPageExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void mAddTabButton_Click(object sender, EventArgs e)
{
TabPage new_tab_page = new TabPage();
if (!mTabNameTextBox.Text.Equals(""))
{
new_tab_page.Text = mTabNameTextBox.Text;
new_tab_page.Name = mTabNameTextBox.Text;
mTabControl.TabPages.Add(new_tab_page);
mTabNameTextBox.Text = "";
}
}
private void mRemoveTabButton_Click(object sender, EventArgs e)
{
if (mTabControl.TabPages.Count > 0)
{
TabPage tab_page = mTabControl.TabPages[mTabNameTextBox.Text];
if (tab_page != null)
{
mTabControl.TabPages.Remove(tab_page);
}
}
mTabNameTextBox.Text = "";
}
}
}
Here is Form1.Designer.cs
namespace TabPageExample
{
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.mTabControl = new System.Windows.Forms.TabControl();
this.mAddTabButton = new System.Windows.Forms.Button();
this.mRemoveTabButton = new System.Windows.Forms.Button();
this.mTabNameTextBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// mTabControl
//
this.mTabControl.Location = new System.Drawing.Point(12, 12);
this.mTabControl.Name = "mTabControl";
this.mTabControl.SelectedIndex = 0;
this.mTabControl.Size = new System.Drawing.Size(499, 379);
this.mTabControl.TabIndex = 0;
//
// mAddTabButton
//
this.mAddTabButton.Location = new System.Drawing.Point(162, 408);
this.mAddTabButton.Name = "mAddTabButton";
this.mAddTabButton.Size = new System.Drawing.Size(75, 23);
this.mAddTabButton.TabIndex = 1;
this.mAddTabButton.Text = "Add Tab";
this.mAddTabButton.UseVisualStyleBackColor = true;
this.mAddTabButton.Click += new System.EventHandler(this.mAddTabButton_Click);
//
// mRemoveTabButton
//
this.mRemoveTabButton.Location = new System.Drawing.Point(243, 408);
this.mRemoveTabButton.Name = "mRemoveTabButton";
this.mRemoveTabButton.Size = new System.Drawing.Size(75, 23);
this.mRemoveTabButton.TabIndex = 2;
this.mRemoveTabButton.Text = "Remove Tab";
this.mRemoveTabButton.UseVisualStyleBackColor = true;
this.mRemoveTabButton.Click += new System.EventHandler(this.mRemoveTabButton_Click);
//
// mTabNameTextBox
//
this.mTabNameTextBox.Location = new System.Drawing.Point(194, 444);
this.mTabNameTextBox.Name = "mTabNameTextBox";
this.mTabNameTextBox.Size = new System.Drawing.Size(100, 20);
this.mTabNameTextBox.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(30, 447);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(158, 13);
this.label1.TabIndex = 4;
this.label1.Text = "Enter tab name to Add/Remove";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(523, 476);
this.Controls.Add(this.label1);
this.Controls.Add(this.mTabNameTextBox);
this.Controls.Add(this.mRemoveTabButton);
this.Controls.Add(this.mAddTabButton);
this.Controls.Add(this.mTabControl);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TabControl mTabControl;
private System.Windows.Forms.Button mAddTabButton;
private System.Windows.Forms.Button mRemoveTabButton;
private System.Windows.Forms.TextBox mTabNameTextBox;
private System.Windows.Forms.Label label1;
}
}