C
C#17mo ago
TheUnrealGuy

❔ How to can I create a TextChanged event handler for this current text box property?

55 Replies
TheUnrealGuy
TheUnrealGuy17mo ago
This CurrentTB property is the Current Text Box in the selected TabPage. How can I create a TextChanged event handler for the CurrentTB property?
Pobiega
Pobiega17mo ago
Winforms? You can't set an eventhandler on a property and have it follow the property reference, so you'll need to add the eventhandler to ALL the textboxes and filter the event source in the handler itself
TheUnrealGuy
TheUnrealGuy17mo ago
The CurrentTB property is the current selected text box in the selected tab page. Because I am making an HTML IDE with tabs and I need to make an event handler on the current selected text box.
Pobiega
Pobiega17mo ago
Well, either have all of them be subscribed and filter, or manually register/unregister the events as the user changes tabs those are your options there is no silver bullet here
TheUnrealGuy
TheUnrealGuy17mo ago
what do you mean by register/unregister?
Pobiega
Pobiega17mo ago
you can subscribe to events with code, not just in the designer and you can unsubscribe too
TheUnrealGuy
TheUnrealGuy17mo ago
subscribe? what is subscribing?
Pobiega
Pobiega17mo ago
textBox.TextChanged += YourHandler; that subscribes to the textchanged event on that box, with the method on the right being the handler that will get invoked you can do -= to remove the eventhandler
TheUnrealGuy
TheUnrealGuy17mo ago
Could I put this code in the load handler? Like MyApp_Load
Pobiega
Pobiega17mo ago
in theory, but will the tabs be created on load?
TheUnrealGuy
TheUnrealGuy17mo ago
the tabs will be created by pressing a toolstrip button
Pobiega
Pobiega17mo ago
so not at load then the tabs, and the textboxes, must exist when you register. so I'd think you'll have to register when a tab is activated, if you want to go with that approach alternatively just keep track of the current active tab (and its textbox) and check if the event is being emitted from the current active box
TheUnrealGuy
TheUnrealGuy17mo ago
@Pobiega The event handler thing worked, but my IDE has a split view of a browser which displays your work, and a tabcontrol that controls the current text boxes and such, when I try to make it so that the browser displays the current text box's HTML when you change the tabpage, it gives me an error.
Pobiega
Pobiega17mo ago
that means CurrentTB was null
TheUnrealGuy
TheUnrealGuy17mo ago
TheUnrealGuy
TheUnrealGuy17mo ago
the text in the CurrentTB is null?
Pobiega
Pobiega17mo ago
that, or webBrowser1 is and no not the text the CurrentTB itself
TheUnrealGuy
TheUnrealGuy17mo ago
I don't understand, when you press the button it should create a tab with a CurrentTB text box. But this error shows when I try to create a new tab
Pobiega
Pobiega17mo ago
use the debugger to figure out what is null
TheUnrealGuy
TheUnrealGuy17mo ago
It is stating that "Object reference not set to an instance of an object"
Pobiega
Pobiega17mo ago
yeah thats the text from "NullReferenceException" that means an object refernce was null, when you used it as if it wasnt
TheUnrealGuy
TheUnrealGuy17mo ago
Pobiega
Pobiega17mo ago
stack overflow usually means you have an infinite recursive loop ie, a method calling itself over and over
TheUnrealGuy
TheUnrealGuy17mo ago
It is not showing me what is null
Pobiega
Pobiega17mo ago
$debug
MODiX
MODiX17mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
TheUnrealGuy
TheUnrealGuy17mo ago
where do I go here to debug?
Pobiega
Pobiega17mo ago
read the guide use breakpoints inspect values
Pobiega
Pobiega17mo ago
TheUnrealGuy
TheUnrealGuy17mo ago
@Pobiega How do you make it so that when all tabs are closed, it closes the form? Like in an if statement.
Pobiega
Pobiega17mo ago
You can probably register an event handler on tab close event and count the number of tabs
TheUnrealGuy
TheUnrealGuy17mo ago
I tried doing something like this but it closes the form when I close one tab. (Items are the tab pages)
Pobiega
Pobiega17mo ago
thats... a very weird if statement you're checking if the collection is not null you want to check that the collection has 0 items
TheUnrealGuy
TheUnrealGuy17mo ago
how can I check that?
Pobiega
Pobiega17mo ago
Im sure you can figure that out without handholding
TheUnrealGuy
TheUnrealGuy17mo ago
this damn pack that I am using is just hurting my brain lmao
TheUnrealGuy
TheUnrealGuy17mo ago
I just don't understand why this won't work.
Pobiega
Pobiega17mo ago
what exactly does += do here, do you think?
TheUnrealGuy
TheUnrealGuy17mo ago
I have no clue to be honest. I don't know why there has to be == or != or += what do they mean exactly?
Pobiega
Pobiega17mo ago
they all do different things, suprisingly == is an equals comparison it checks if two values are equal != is the same, but checks for NOT equal += is the increment operator, it increases the left value by the right value
int val = 5;
val += 10;
val == 15 // true
int val = 5;
val += 10;
val == 15 // true
TheUnrealGuy
TheUnrealGuy17mo ago
ahhh I see what would I put in there for this situation?
Pobiega
Pobiega17mo ago
^
TheUnrealGuy
TheUnrealGuy17mo ago
in the If statement if (tsFiles.Items == 0)?
Pobiega
Pobiega17mo ago
yay you did it well, almost
TheUnrealGuy
TheUnrealGuy17mo ago
bummer
Pobiega
Pobiega17mo ago
tsFiles.Items isnt a numeric value its a collection
TheUnrealGuy
TheUnrealGuy17mo ago
oh
Pobiega
Pobiega17mo ago
you need to check a certain property ON the collection
TheUnrealGuy
TheUnrealGuy17mo ago
If I go to the collection there would be the items and such in the collection
TheUnrealGuy
TheUnrealGuy17mo ago
Pobiega
Pobiega17mo ago
sigh tsFiles.Items.SOMETHING == 0 figure out what SOMETHING should be...
TheUnrealGuy
TheUnrealGuy17mo ago
oh oh I am stupid as hell sometimes
TheUnrealGuy
TheUnrealGuy17mo ago
Pobiega
Pobiega17mo ago
yay
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.