0

im working on a code-editor(winforms) and Im just wondering if its possible to call a specific box from a form to another?

sample for this set of codes:

int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
label1.Text = "line: " + line.ToString() + ", column: " + column.ToString();

***code above was inside a timer which calls the count of line and column in a richtextbox like in lower rightpart of actual code editor .

now im just wondering if its possible to call the label that displays to the main form and will display to another but will still run .

like in mainform theres the code for richtextbox and on other form it should have the code of label that connects to the mainform .

my question is it possible to call a tool function from another form to another?

hope you could help me, really in need and thanks a lot!

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Elegiac
  • 366
  • 10
  • 25
  • I'm 99% sure there is a duplicate question here, but the short answer would be to use an event model and subscribe to that event – lc. May 09 '13 at 02:32
  • @Shimmy its winforms sir i include it in my question :/ – Elegiac May 09 '13 at 02:36

1 Answers1

1

As long as you have a reference to that form toolbox, just expose that Label/TextBox or whatever you want to change via a public property and set it from your context.

public class ToolBox : ToolBoxBase
{
   public CustomLabel
   {
      get
      {
         return label1.Text;
      }
      set
      {
         label1.Text = value;
      }
   }
}

private ToolBox toolbox;
void ShowToolBox()
{
   InitToolBox();
   toolbox.CustomLabel = "New label";
}

As I'm not even sure what technology the question refers to I added a poor pseudo example to get you the idea. The InitToolBox method initializes the toolbox and displays it, and sets the field toolbox with a reference to it.

If the other form runs on another thread, then you'll have to invoke the label setter asynchronously. See this question for more instructions.

Community
  • 1
  • 1
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • And from what you seem to be describing there the easiest way to have a reference, as well as being a good logical construct for the two forms, is to have one form be the MDIParent of the other form. – Adrian May 09 '13 at 02:36
  • @Shimmy even two forms are not mdi's? – Elegiac May 09 '13 at 02:44
  • @Elegiac yes as long as you have a reference to it and it runs in the same thread. Please see my updated answer. – Shimmy Weitzhandler May 09 '13 at 02:48
  • @Shimmy ok sir ill try to do it and make some logic .w8 – Elegiac May 09 '13 at 02:49
  • @Elegiac really it's not complicated, just copy the code above to your tooltip class and you're done. Good luck! Please don't forget to vote / accept answer if this post helped. – Shimmy Weitzhandler May 09 '13 at 02:50
  • @Shimmy, sir the ToolBox was missing :/ how to add toolbox in my form? – Elegiac May 09 '13 at 02:52
  • Create a field in your form then set that field with the toolbox. – Shimmy Weitzhandler May 09 '13 at 02:55
  • lol i thought toolbox was a reference but i think its the container of all tools that your referring . sorry sir i dont have anydesigner in my form tools are called by codes only . but still +1 for the help . – Elegiac May 09 '13 at 03:00
  • Then you can have a field that holds a reference to the label :) – Shimmy Weitzhandler May 09 '13 at 03:02
  • @Shimmy sir im thinking if i can use the using namespace; method? sample for mainform namespace (name of otherform); then i can call all tools from main is that possible? – Elegiac May 09 '13 at 03:05