P
P
programmist_derevo2017-07-24 17:50:25
PHP
programmist_derevo, 2017-07-24 17:50:25

How to block new line transition on pressing Enter in RichTextBox(multiline)?

Hello! Such a problem: there is a RichTextBox element on the form, which serves as a message entry field. By pressing Enter, it sends the text in the sting line to the right place. But in addition to the action I programmed, another default one is performed - a transition to a new line. I need only 1 action to be performed (sending a message) and not a line break.
Thus if the user presses shift+enter - transition to a new line occurs.
So how do I block the default function Enter'a? I've been looking for an answer for a long time and I can't find it, it seems to be such a simple action.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FanatPHP, 2019-02-14
@djamali

What am I doing wrong?

Everything.
Nowhere in the error message does it say what "it" returns. The error message tells you in a human voice that the DB class does not have a query() method. And if you look at the class code in your question, then there really is no such method in it. And you can only call existing methods on the class, no matter who returns it.
Worse, even if you make your class execute queries, it will create a million connections to the database.
Because unlike mysql_connect, each call to new mysqli creates a new connection. And you yourself need to take care that there is only one connection to the database. And it wasn't created every time you write $db=new DB();. Fw tf it's gonna be like writing in every method of every class
Well, the fact that mysqli is used in the DB and not PDO means that you will have all the code in SQL injections.
The correct way would be to first create an instance of the PDO class, and then when creating each object that needs to work with the database, and pass it to the constructor.
We create PDO as it is written here , we put this code in a file, so that later we can include it where necessary.
class Categories
{
    public function __construct($db){
        $this->db = $db;
    }
    public function getCategories()
    {
        $query="SELECT * FROM categories";
        return $this->db->query($query)->fetchAll();
    }
}

and then something like
include 'pdo.php';
$category = new Categories($pdo);
$list = $category->getCategories();

A
Andrey Prozorov, 2019-02-15
@i_d_1

Return in the constructor does not work. Object instance always rises

A
Alexander Morgushin, 2017-07-24
@nightwolf_du

The only way that comes to my mind is not very simple.
Determine which of the events (KeyPress, KeyDown, KeyUp, PreviewKeyDown) this action falls on, after which
1) remove all default handlers with reflection,
2) process them manually.
You can remove the handler, for example, with the following code (example for the Click event, respectively, for your EventClick string content will change, you need a field responsible for your events)
instead of panel1 - respectively, your richTextBox:

FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static| BindingFlags.NonPublic);
object obj = f1.GetValue(panel1);
PropertyInfo pi = panel1.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(panel1, null);
list.RemoveHandler(obj, list[obj]);

in order not to manually write all the keystroke processing after that - you can try to look in the EventHandlerList list for the event handler you need that handles the space, but this is quite complicated, and it’s not a fact that all the logic associated with keystrokes is placed in more than one method.
For a better understanding, see richTextBox sources.

S
Sumor, 2017-07-24
@Sumor

Relevant answer on StackOverflow
The suggestion is to set AcceptsTabs to true and optionally handle the IsInputKey event. In this case, the AcceptButton must be set for the form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question