8 Replies - 1283 Views - Last Post: 22 November 2013 - 12:30 PM Rate Topic: -----

#1 xterminal01   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Adding controls with the same name dynamically

Posted 19 November 2013 - 03:51 PM

I have a tabbed document window (like Visual Studio) and add inside a TextEditor control. I am creating the texteditor control and use its name throughout the application to search, copy the text.


In order for me to be able to copy the text from the textbox control i need to know the currently active tab and name it accordingly.

The following code gives me the error "Cannot implicitly convert type 'string' to Scintilla"

 Scintilla TextEditor = new Scintilla();
 public Mainfrm()
    { ...



 // Determine the currently active tab
 void Dock1_ActiveWindowChanged(object sender, DockWindowEventArgs e)
    {          
        Scintilla TextEditor = e.Dockwindow.Name;
    }
"Cannot implicitly convert type 'string' to Scintilla"

 private void AddNewTab_Click(object sender, EventArgs e) 
{

 Docking.DocumentWindow doc = new Docking.DocumentWindow();   
 Scintilla TextEditor = new Scintilla();

 TextEditor.Name = tabnumber;
 doc.Controls.Add(TextEditor); 
}

 private void button18_Click(object sender, EventArgs e)
    {
        TextEditor.Clipboard.Copy();
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Adding controls with the same name dynamically

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14244
  • View blog
  • Posts: 57,141
  • Joined: 12-June 08

Re: Adding controls with the same name dynamically

Posted 19 November 2013 - 03:56 PM

10	        Scintilla TextEditor = e.Dockwindow.Name;

What exactly are you expecting to happen? You are creating an object of type "Scintilla" and asking it to be assigned a string object.
Was This Post Helpful? 0
  • +
  • -

#3 xterminal01   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Adding controls with the same name dynamically

Posted 19 November 2013 - 04:06 PM

View Postmodi123_1, on 19 November 2013 - 03:56 PM, said:

10	        Scintilla TextEditor = e.Dockwindow.Name;

What exactly are you expecting to happen? You are creating an object of type "Scintilla" and asking it to be assigned a string object.


Commenting out the line still does not produce the expected behavior.
What I expect to happen is that when a new tab is added i wish to be able to edit the textbox that has the focused active tab.

Exp:
TextEditor.Clipboard.Copy();


This works; however, only when I remove the
Scintilla TextEditor = new Scintilla();
in the button event.
The problem then is that the second tab does NOT contain the texteditor.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Adding controls with the same name dynamically

Posted 19 November 2013 - 04:40 PM

You want the 2nd tab to contain the *same* text editor? Or do you want a new one with the same Text value? Or What?
Was This Post Helpful? 0
  • +
  • -

#5 xterminal01   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Adding controls with the same name dynamically

Posted 19 November 2013 - 05:29 PM

View PostMomerath, on 19 November 2013 - 04:40 PM, said:

You want the 2nd tab to contain the *same* text editor? Or do you want a new one with the same Text value? Or What?

Sorry, Yes I would like to have a new Texteditor in each tab. Which works fine, but i cant do the operations if more than one is added. Because I don't know what editor the user has open & how to access the correct name of currently opened/focused editor.

If I assign a new name to each editor how can I access the active tab which has the TextEditor41 or whatever the name is in all button events and change it.

The scenario would go like this:
Step 1: User creates a new tab which adds the TextEditor1
Step 2: They press the button to copy the content of Texteditor
TextEditor1.Clipboard.Copy();

This all works:

Problem is when the user creates a second tap with TextEditor2.
It should say
TextEditor2.Clipboard.Copy();

The button does not fireup because the name is different.
I hope this makes sense.

This post has been edited by xterminal01: 19 November 2013 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Adding controls with the same name dynamically

Posted 20 November 2013 - 06:46 AM

I'd ignore the TextEditor name.

You should get the current page. Then look for a control on that page that is a TextEditor. I'm assuming each tab only has one TextEditor. I also didn't include anything that might happen if a text editor can't be found.
private TextEditor GetCurrentTextEditor()
{
   var currentTab = tabControl.SelectedTab;

   if (currentTab == null)
   {
      return null;
   }

   foreach(var control in currentTab.Controls)
   {
      if (control is TextEditor)
      {
         return (TextEditor)control;
      }
   }

   return null;
}

private void CopyButton_Clicked(object sender, EventArgs e)
{
   var editor = GetCurrentTextEditor();
   if (editor != null)
   {
      editor.Clipboard.Copy();
   }
}


This obviously isn't a solution, but should point you in a different direction that doesn't involve knowing the specific TextEditor name.

This post has been edited by Robin19: 20 November 2013 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Adding controls with the same name dynamically

Posted 20 November 2013 - 06:32 PM

View Postxterminal01, on 19 November 2013 - 05:06 PM, said:

10	        Scintilla TextEditor = e.Dockwindow.Name;


To be clear... TextEditor is of type Scintilla. It is an instance of the Scintilla class.
Like saying that my dodge ram with name 'Betty' is an instance of the Dodge Ram class.

e.DockWinow.Name is a string... A piece of text. The two object are not at all the same type.

You are effectively saying: Banana George = Pepsi.Can;
Can you see how these two things simple are not all all alike an therefore can't be used like this?
You probably should work through the tutorial linked in my signature block about classes.



View Postxterminal01, on 19 November 2013 - 05:06 PM, said:

The problem then is that the second tab does NOT contain the texteditor.

Why would it? Where do you ever add the next texteditor to the tab? It doesn't magically know its home is supposed to be on that tab just because you created one in memory. Going back to the vehicle analogy. If I make a new Dodge Ram at the factory, does it just magically appear in my garage? No. I have to expressly put it in the garage.

View Postxterminal01, on 19 November 2013 - 05:06 PM, said:

If I assign a new name to each editor how can I access the active tab which has the TextEditor41 or whatever the name is in all button events and change it.


You don't. You are getting very caught up in trying to micro-manage every control, and not working in an Object Oriented way. If you want your TextEditor and Button to work together, as if they were part of a single control then that is what you need to do. Make a single UserControl that consists of the texteditor and the copy button. That button knows to copy from that editor. Now place 20 of those UserControls where you like. Each one is a new instance. Each button knows to talk to its editor.
I have another tutorial all about making WinForms UserControls like this. I think it would help you a lot.
http://www.dreaminco...ister-part-one/
Was This Post Helpful? 0
  • +
  • -

#8 xterminal01   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Adding controls with the same name dynamically

Posted 20 November 2013 - 09:30 PM

Thanks for the reply. It was suppose to say TextEditor.name = tostring not the class itself. I understand that the string can't be assigned to the class. This; however, was a typo and is not the problem that I have.

Using a simple, pure native .NET example of the issue I have:
The tabs gets added with the textbox inside. Everything works besides that only the content of the last Textbox is copied not the currently selected textbox.

    public partial class Form1 : Form
    {
        int tabcount = 0;
        TextBox TextE;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create a new textbox to add to each control
            TextE = new TextBox();

            // Fill the tabpage 
           TextE.Dock = DockStyle.Fill;
           TextE.Multiline = true;

             // Create new tab page and increment the text of it 
            TabPage tab = new TabPage();
            tab.Text = "Tab" + tabcount.ToString();

            // Add Textbox to the tab
            tab.Controls.Add(TextE);

            // Add tabpage to tabcontrol
            tabControl1.Controls.Add(tab);
            tabcount++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Text content to messagebox...
  
            MessageBox.Show(TextE.Text);
        }


    }




Posted Image

This post has been edited by Curtis Rutland: 21 November 2013 - 07:39 AM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#9 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Adding controls with the same name dynamically

Posted 22 November 2013 - 12:30 PM

Update TextE every time a new tab is selected. Or simply ask the TabContainer for the current tab every time you want the current tab and don't save TextE. Look at the code I wrote above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1