Question how do i delete or remove comments inside the button programatically?

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
hi. someone can give help? i need to finish my project this month :(
my problem is how can i delete comments (/*) inside of this button
        private void button3_Click(object sender, EventArgs e)
        {
            /*
            if (richbox1.Text.Equals(richbox2.Text))
            {
                MessageBox.Show("Equal");
            }
            else
            { 
                MessageBox.Show("Not Equal"):
            }
             */
        }

i want to delete it when i click the checkedbox. someone can help?
thanks in advance
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
No, no, no. You are under a serious misconception. Those comments don't exist at run time so how could you delete them? The source code doesn't even exist at run time. You don't edit source code at run time. You write source code that is going to make the appropriate decisions and perform appropriate tasks at run time. If you only want to execute some code when a CheckBox is checked then you put it inside an 'if' block that evaluates the Checked property of that CheckBox:
private void button3_Click(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        if (richbox1.Text.Equals(richbox2.Text))
        {
            MessageBox.Show("Equal");
        }
        else
        {
            MessageBox.Show("Not Equal"):
        }
    }
}
That's pretty elementary stuff so you ought to review whatever notes or reference text or tutorial you're learning from.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
the problem is.. i dont have richtextbox on form. i just create a rtb in code. like this..
RichTextBox rich = new RichTextBox { Parent = tab, Dock = DockStyle.Fill, Name = " richbox" + f.ToString(), Enabled = false }
it depends on openfiledialog.. it i browse multiple files example. 5 files.. then the rtb will automatic create depends on the browse. and may another problems i dont know how to know the name of each rtb that will create.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
That's not a problem. You could simply add your RichTextBoxes to a collection and then access them by index. Alternatively, you appear to be adding each RichTextBox to a TabPage so you can access the appropruate TabPage first, e.g. the SelectedTab of a TabControl, and then get the RichTextBox it contains via its Controls collection. You could even define your own class that inherits TabPage and creates its own RichTextBox and exposes it via a property.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
Here's an implementation of the last suggestion:
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    internal class RichTextTabPage : TabPage
    {
        public RichTextTabPage()
        {
            this.RichTextBox = new RichTextBox();
            this.RichTextBox.Dock = DockStyle.Fill;
            this.Controls.Add(this.RichTextBox);
        }

        public RichTextBox RichTextBox { get; private set; }
    }
}
Now you can add instances of that class to your TabControl instead of vanilla TabPages. You can access the RichTextBox on each one via the property, e.g.
this.tabControl1.TabPages.Add(new RichTextTabPage{Text = "RTF"});
var selectRichTextBox = ((RichTextTabPage) this.tabControl1.SelectedTab).RichTextBox;
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
As I have already said, their Name will be whatever you set it to but that is irrelevant because there is no corresponding variable. Look at this code for example:
Dim rtb1 As New RichTextBox

rtb1.Name = "MyRichTextBox"

Dim rtb2 As RichTextBox = rtb1
What's the name of that RichTextBox? Is it rtb1? "MyRichTextBox"? rtb2? You need to understand that the RichTextBox and a variable that you use to access the RichTextBox are not the same thing.

As I have already told you, if you use my example code then you will get the RichTextBox from the RichTextBox property of the appropriate TabPage. How you get that TabPage is up to you. Once you have a reference to the RichTextBox you can get its Text or Rtf or whatever. How you came by that reference is irrelevant.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
this is my full code. and my only problems is.. i cant call the richbox name.
        public void button1_Click(object sender, EventArgs e)
        {
            //Create opendialog            
            OpenFileDialog browse = new OpenFileDialog();
            browse.Filter = "Text File|*.txt";
            browse.Multiselect = true;
            browse.RestoreDirectory = true;
            
            if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //Conditional Statement                                                               
                    foreach (TabPage tabcon in tabControl1.TabPages)
                    {
                        if (tabcon.Text == browse.SafeFileName)
                        {
                            tabControl1.TabPages.Remove(tabcon);
                            MessageBox.Show("File(s) already Exist! - " + tabcon.Text);                                                       
                            break;                            
                        }                                 
                    }
                    
                    foreach (String file in browse.FileNames)
                    {
                        //Create Tab control and tab pages
                        TabPage tab = new TabPage() { Text = System.IO.Path.GetFileName(file) };
                        tabControl1.TabPages.Add(tab);
                        tabControl1.SelectedTab = tab;
                        //Create richtextbox                 
                        RichTextBox rich = new RichTextBox { Parent = tab, Dock = DockStyle.Fill, Name = "richbox" + f++, Enabled = false };
                        rich.LoadFile(file, RichTextBoxStreamType.PlainText);                        
                        string test1 = rich.Name;
                        //Remove the blank lines and comments                
                        rich.Text = Regex.Replace(rich.Text, @"/\*()-_+=,.:;'\[]~`{}|<>*(.*?)\*/", "", RegexOptions.Multiline);
                        rich.Text = Regex.Replace(rich.Text, @"/\*(.*?)\*/", "", RegexOptions.Multiline);
                        rich.Text = Regex.Replace(rich.Text, @"//*(.*?)//", "", RegexOptions.Multiline);
                        rich.Text = Regex.Replace(rich.Text, @"//()-_+=,.:;'\[]~`{}|<>*(.*?)//", "", RegexOptions.Multiline);
                        rich.Text = Regex.Replace(rich.Text, @"^\s*$(\n)", "", RegexOptions.Multiline);
                        rich.Text = Regex.Replace(rich.Text, @" . ", "", RegexOptions.Multiline);
                        //Create label
                        Label numchar = new Label { Parent = tab, Dock = DockStyle.Top };
                        Label charnum = new Label { Text = " Characters" };
                        Label test = new Label { Text = " %" };
                        Label space = new Label { Text = " " };
                        //Count all characters
                        int letter = 0;
                        int count1 = 0;
                        int line = rich.Lines.Count();
                        int sum = 0;
                        int count = rich.Lines[0].Length;                        
                        string letonly = rich.Text;
                        foreach (char c in letonly)
                        {
                            if (char.IsLetter(c))
                            {
                                letter++;
                            }
                            if (char.IsDigit(c))
                            {
                                letter++;
                            }
                            if (char.IsSymbol(c))
                            {
                                letter++;
                            }
                            if (char.IsPunctuation(c))
                            {
                                letter++;
                            }
                            
                        }
                        foreach(char c in rich.Lines[0])
                        {
                            if(char.IsLetter(c))                            
                            {                               
                                count1++;
                                sum = count1 / count * 100;
                            }                           
                        }
                        numchar.Text = letter.ToString() + charnum.Text + space.Text + line.ToString() + space.Text + count.ToString() + space.Text + sum.ToString() + test.Text + test1.ToString();


                    }   
                }            
         }




.if you know what is the wrong here plss tell me
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
It's important to understand the difference between objects and references to objects so I'm going to spend a bit more time on that. Programming objects are modelled on real-world objects so you generally should look at how real-world objects work to understand how programming objects work. Consider my father. His name is Tom. My sister and I call him Father, my mother calls him Husband, may grandparents call him Son, my uncles and aunts call him Brother and the players on his football team call him Captain. He's just one person though, and his name is Tom. Those others are just different ways to refer to the one man.

Your RichTextBox is the same. It's just one object and its name is whatever you set its Name property to. That doesn't magically create a member variable in your form though. A variable is simply a way to refer to an object. As I showed in my earlier code example, you can have multiple variables referring to the same object and none of those variables have to match the name of the control. You don't even have to set the Name and you normally don't when creating controls at run time.

The relevant point for you is that, if you use my code example, you can reference a RichTextBox object via the RichTextBox property of the appropriate RichTextTabPage. You get a reference to the TabPage first in whatever way is appropriate and then you get a reference to the RichTextbox from that. That rererence is exactly the same as if you had added a RichTextBox in the designer and then used a member variable to access it. A reference to a RichTextBox is a reference to a RichTextBox, no matter how you came by it. Once you have that reference, you use it however you like.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
this is my full code. and my only problems is.. i cant call the richbox name.

.if you know what is the wrong here plss tell me

What's wrong is the way you are thinking. STOP focusing on the name. The name IS NOT RELEVANT. It's the object, not the name of the object, that is relevant. I have told you how to access the RichTextBox object using my code example. If you're not going to use my code then, as I said earlier, you can access it via the Controls collection of its parent, e.g.
var rtb = (RichTextBox) myTabPage.Controls[0]
That assumes that there are no other controls on the TabPage with the RichTextBox. How you get a reference to the TabPage is up to you.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
ok i wait for it.. i need to finish that before the end of this day.
i need to pass it for my project. plss help :( sorry.. i dont know what is wrong to my code. that's my only problem the name of rtb. because it automatically create when i browse a text file. and i need the name of each for the compared button.. plss i need your help :(
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
Apparently you are determined not to understand what I'm saying because you keep insisting that you need the name despite my having told you repeatedly that the name is not relevant. You can't help those who refuse to be helped so I'm not going to try any further.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
aw.. sorry i did not see your post ealier sorry for that.
i just put
var rtb = (RichTextBox) myTabPage.Controls[0]
to my code? and remove this code
TabPage tab = new TabPage() { Text = System.IO.Path.GetFileName(file) };
tabControl1.TabPages.Add(tab);
tabControl1.SelectedTab = tab;
//Create richtextbox
RichTextBox rich = new RichTextBox { Parent = tab, Dock = DockStyle.Fill, Name = "richbox" + f++, Enabled = false };
rich.LoadFile(file, RichTextBoxStreamType.PlainText);
string test1 = rich.Name;
or i just put it inside?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
You still need to add the RichTextBox to the TabPage in the first place. The code I suggested is to get a reference to that RichTextBox later, when you want to use its contents.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
do you mean that i will put that in the other buttons? like this
private void button3_Click(object sender, EventArgs e)
{
var rtb = (RichTextBox) myTabPage.Controls[0]
}


what is the meaning of [0]? im confused to use it.. :(
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
do you mean that i will put that in the other buttons? like this
private void button3_Click(object sender, EventArgs e)
{
var rtb = (RichTextBox) myTabPage.Controls[0]
}


what is the meaning of [0]? im confused to use it.. :(

I don't know what button3 does. It if it had a name that actually gave a clue to what its purpose is then maybe I would. You put that code where you want to use tyhe RichTextBox in code. If you want to use the RichTextBox when the user clicks button3 then that's where you put it. Of course, you need more code after that to actually use the RichTextBox you just referenced to make it useful.

Controls is a collection and it contains all the children of a parent control. The TabPage is the parent and the RichTextBox is the child, so the RichTextBox is in the TabPage's Controls collection. You access items in a collection by index, so Controls[0] is the item at index zero, i.e. the first item.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
ok i understand.. i use that button3 for comparing the contents of each richtextbox. i.e
the one richtextbox contains "how to compare"
and the other richtextbox constaisn different.. how i should do that? that is possible? and give the result?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,219
Location
Sydney, Australia
Programming Experience
10+
I'm afraid that I'm not going to keep repeating myself. I've told you how to get a reference to one of your RichTextBoxes. Once you have a reference then you use it exactly as you usually would.
 

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
var rtb = (RichTextBox) myTabPage.Controls[0]
so this is all i need to put in button3? to access all the contents of richtextboxes.
example i compare 2 richtextboxes like this
if(rich.line[0].length.Equals(rich.line[0].length))
{
MessageBox.Show("Equal");
}

i think it will only give a Equals. whenever the richtextboxes are not equal?
 
Top Bottom