Home > Software engineering >  Fire the result with an event to the textbox in C#
Fire the result with an event to the textbox in C#

Time:07-19

I would like the created string "afterTextbox1" to be output automatically in Form1 and I don't have to click on update manually.

I hope i can be helped

        public void PostCard()
        {
            try
            {
                JsonRead jsonRead = new JsonRead();
                ConfigSet newPost = new ConfigSet();
                newPost.name = Name;
                newPost.desc = Desc;
                newPost.due = Date;
                newPost.labels = Label;
                newPost.idList = jsonRead.ConfigReader("config.json").IdList;
                newPost.idBoard = jsonRead.ConfigReader("config.json").IdBoard;
                newPost.token = jsonRead.ConfigReader("config.json").Token;
                newPost.key = jsonRead.ConfigReader("config.json").ApiKey;

                var json = JsonConvert.SerializeObject(newPost);
                var jsondata = new StringContent(json, Encoding.UTF8, "application/json");
                var url = "https://api.trello.com/1/cards?";
                HttpClient client = new HttpClient();
                var result = client.PostAsync(url, jsondata).Result.Content.ReadAsStringAsync().Result;

                string afterTextbox1 = $"{Name} {Date} {Desc}";
        }

CodePudding user response:

I assume that the method PostCard is called after a button or something is pressed.

If so you have multiple ways to display the string afterTextbox1.

  1. Add a new Winform to your project...

for example, Winform2 and display it as dialog. Add something like a Label to display your string. I would recommend doing this with the editor. Then you can call the following commands for example at the end of PostCard

Form2 dialog = new Form2();
dialog.Label1.Text = afterTextbox1;
dialog.ShowDialog();

You now have a Pop-Up form that has to be closed before the program continues. Also, I would recommend changing PostCard to return a string instead of void and implementing above shown code in a separate method.

  1. Add a Label (label1) or something similar to your main form.

Similar to solution 1) you can change PostCard to return string. Now just write something like

lable1.Text = PostCard();

If this isn't helping we need some more detailed information about your code. And as advice for your next questions - please be more detailed from the beginning.

EDIT: In my opinion, there is no need to use an event. At least your example does not show the need. Otherwise please specify when PostCard() is called and why you think you need an event.

CodePudding user response:

Thank you for your prompt reply. I had it that way, only the result is not what I wanted .

This is how my program starts, it is waiting for a pdf that is processed by the program and posted using api. I wanted to see some of the information in the pdf in the text box after the program had finished

class AweitDokument
{
    private string name { get; set; }   
    public Task AweitOfDokument()
    {
        try
        {
            JsonRead jsonRead = new JsonRead();
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = jsonRead.ConfigReader("config.json").Path;
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                 | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            watcher.Filter = "*.pdf*";
            watcher.Changed  = OnCreate;
            watcher.EnableRaisingEvents = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERROR : FileSystemwatscher"   Environment.NewLine   ex.Message);
        }
        return Task.CompletedTask;
    } 
    private async void OnCreate(object sender, FileSystemEventArgs e)
    {
        if (name != e.Name)
        {
            name = e.Name;
            ExtractDokument ED = new ExtractDokument();
            await ED.ExtractEvent(e.Name);
        }
        return;
    }
}
  • Related