Hello,
I am a New leaner, the Book, am using give an Assignment, and I have try to work it out. In the process, I Run to little Issue, It is all about Replacing Old- Word with New-Word.... But if the Old word is empty it will bring an error of "String cannot be of zero length.".... All, I Notice that each I press F5, the program I design Pop up along with MSDOS Console, I am having a feeling that this is not Right, please do help me out, Thank...
Below is my Code: Thanks

using System;
using System.Windows.Forms;

public class frmMain: Form
{
    private TextBox txtInputWord;
    private Label label2;
    private TextBox txtSearchFor;
    private Label label3;
    private TextBox txtReplaceWith;
    private TextBox txtOriginalWord;
    private TextBox txtReplacedWord;
    private Label label4;
    private Button btnReplace;
    private Button btnClear;
    private Button btnExit;
    private Label label5;
    private Label label1;
    #region Windows code
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.txtInputWord = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.txtSearchFor = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.txtReplaceWith = new System.Windows.Forms.TextBox();
        this.txtOriginalWord = new System.Windows.Forms.TextBox();
        this.txtReplacedWord = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.btnReplace = new System.Windows.Forms.Button();
        this.btnClear = new System.Windows.Forms.Button();
        this.btnExit = new System.Windows.Forms.Button();
        this.label5 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label1.Location = new System.Drawing.Point(12, 18);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(100, 23);
        this.label1.TabIndex = 0;
        this.label1.Text = "Write Your Note";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // txtInputWord
        // 
        this.txtInputWord.Location = new System.Drawing.Point(119, 20);
        this.txtInputWord.Name = "txtInputWord";
        this.txtInputWord.Size = new System.Drawing.Size(222, 20);
        this.txtInputWord.TabIndex = 1;
        this.txtInputWord.TextChanged += new System.EventHandler(this.txtInputWord_TextChanged);
        // 
        // label2
        // 
        this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label2.Location = new System.Drawing.Point(12, 54);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(100, 23);
        this.label2.TabIndex = 2;
        this.label2.Text = "Search For:";
        this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // txtSearchFor
        // 
        this.txtSearchFor.Location = new System.Drawing.Point(119, 56);
        this.txtSearchFor.Name = "txtSearchFor";
        this.txtSearchFor.Size = new System.Drawing.Size(222, 20);
        this.txtSearchFor.TabIndex = 3;
        // 
        // label3
        // 
        this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.label3.Location = new System.Drawing.Point(12, 86);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(100, 23);
        this.label3.TabIndex = 4;
        this.label3.Text = "Replace With:";
        this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // txtReplaceWith
        // 
        this.txtReplaceWith.Location = new System.Drawing.Point(119, 88);
        this.txtReplaceWith.Name = "txtReplaceWith";
        this.txtReplaceWith.Size = new System.Drawing.Size(222, 20);
        this.txtReplaceWith.TabIndex = 5;
        // 
        // txtOriginalWord
        // 
        this.txtOriginalWord.Location = new System.Drawing.Point(13, 145);
        this.txtOriginalWord.Multiline = true;
        this.txtOriginalWord.Name = "txtOriginalWord";
        this.txtOriginalWord.ReadOnly = true;
        this.txtOriginalWord.Size = new System.Drawing.Size(328, 52);
        this.txtOriginalWord.TabIndex = 6;
        // 
        // txtReplacedWord
        // 
        this.txtReplacedWord.ForeColor = System.Drawing.Color.Red;
        this.txtReplacedWord.Location = new System.Drawing.Point(13, 227);
        this.txtReplacedWord.Multiline = true;
        this.txtReplacedWord.Name = "txtReplacedWord";
        this.txtReplacedWord.ReadOnly = true;
        this.txtReplacedWord.Size = new System.Drawing.Size(328, 68);
        this.txtReplacedWord.TabIndex = 7;
        this.txtReplacedWord.Visible = false;
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.label4.Location = new System.Drawing.Point(15, 209);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(251, 15);
        this.label4.TabIndex = 8;
        this.label4.Text = "Message After Replace with Space being Remove:";
        // 
        // btnReplace
        // 
        this.btnReplace.Location = new System.Drawing.Point(13, 302);
        this.btnReplace.Name = "btnReplace";
        this.btnReplace.Size = new System.Drawing.Size(75, 23);
        this.btnReplace.TabIndex = 9;
        this.btnReplace.Text = "&Replace";
        this.btnReplace.UseVisualStyleBackColor = true;
        this.btnReplace.Click += new System.EventHandler(this.btnReplace_Click);
        // 
        // btnClear
        // 
        this.btnClear.Location = new System.Drawing.Point(133, 302);
        this.btnClear.Name = "btnClear";
        this.btnClear.Size = new System.Drawing.Size(75, 23);
        this.btnClear.TabIndex = 10;
        this.btnClear.Text = "&Clear";
        this.btnClear.UseVisualStyleBackColor = true;
        this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
        // 
        // btnExit
        // 
        this.btnExit.Location = new System.Drawing.Point(266, 302);
        this.btnExit.Name = "btnExit";
        this.btnExit.Size = new System.Drawing.Size(75, 23);
        this.btnExit.TabIndex = 11;
        this.btnExit.Text = "E&xit";
        this.btnExit.UseVisualStyleBackColor = true;
        this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(12, 120);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(105, 13);
        this.label5.TabIndex = 12;
        this.label5.Text = "Message Before Edit";
        // 
        // frmMain
        // 
        this.ClientSize = new System.Drawing.Size(353, 337);
        this.Controls.Add(this.label5);
        this.Controls.Add(this.btnExit);
        this.Controls.Add(this.btnClear);
        this.Controls.Add(this.btnReplace);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.txtReplacedWord);
        this.Controls.Add(this.txtOriginalWord);
        this.Controls.Add(this.txtReplaceWith);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.txtSearchFor);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.txtInputWord);
        this.Controls.Add(this.label1);
        this.Name = "frmMain";
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    #endregion
    public frmMain()
    {
        InitializeComponent();
    }
    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

    private void btnReplace_Click(object sender, EventArgs e)
    {
        string Temp;
        
        Temp = txtInputWord.Text;
        txtReplacedWord.Text = Temp.Replace(txtSearchFor.Text, txtReplaceWith.Text);
        //txtReplacedWord.Text = txtReplacedWord.Text.Replace("  ", " ");
        txtReplacedWord.Visible = true;
        
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtInputWord.Clear();
        txtReplaceWith.Clear();
        txtSearchFor.Clear();
        txtInputWord.Focus();
        txtReplacedWord.Clear();
        txtReplacedWord.Visible = false;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void txtInputWord_TextChanged(object sender, EventArgs e)
    {
        txtOriginalWord.Text = txtInputWord.Text;
    }
}

The error makes sense. How do you find "" to replace? Are you trying to replace nothing? Every character? It's ambiguous. In the case where the search string is empty, I'd simply do nothing:

private void btnReplace_Click(object sender, EventArgs e)
{
    if (txtSearchFor.Text.Length == 0) {
        txtReplacedWord.Text = txtInputWord.Text.Replace(
            txtSearchFor.Text, 
            txtReplaceWith.Text);
        txtReplacedWord.Visible = true;
    }
}

Hello,
Thank so much, I have been thinking about the if Command since, but I don't get it well, Well, I have got it right with the code below, but I still have one more Question.
Well, if I press F5, MSDOS console usually start with my testing program, I think this is not Right, Please what is the way out of this... thank.... Below is the way, I resolve the Issue.. thanks

if (txtSearchFor.Text.Length == 0)
        {
            txtReplacedWord.Text = txtOriginalWord.Text;
            txtReplacedWord.Visible = true;
        }
        else
        {
            txtReplacedWord.Text = Temp.Replace(txtSearchFor.Text, txtReplaceWith.Text);
            txtReplacedWord.Text = txtReplacedWord.Text.Replace("  ", " "); // I want This to Replace any Double space with single space
            txtReplacedWord.Visible = true;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.