简体   繁体   中英

How do i use 2 buttons to do the same thing

hey im kinda new to coding i was wondering how i would use two buttons to execute the same thing, only the first button works the second doesn't code:

 private void button1_Click(object sender, EventArgs e)
    {
        Send(richTextBox1.Text);
    }


    private void button2_Click(object sender, EventArgs e)
    {
        Send(textBox1.Text);
    }

the Send thing:

void Send(string command)
    {
        try
        {
            callbytes = BitConverter.GetBytes(cbuf_address);
            if(command == "")
            {
                MessageBox.Show("You must enter a command before pressing Send!", "Error", MessageBoxButtons.OK);
            }
            else
            {
                if(cbuf_addtext_alloc == IntPtr.Zero)


                {
                    cbuf_addtext_alloc = VirtualAllocEx(hProcess, IntPtr.Zero, (IntPtr)cbuf_addtext_wrapper.Length, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);
                    commandbytes = System.Text.Encoding.ASCII.GetBytes(command);
                    commandaddress = VirtualAllocEx(hProcess, IntPtr.Zero, (IntPtr)(commandbytes.Length), AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);
                    int bytesWritten = 0;
                    int bytesWritten2 = commandbytes.Length;
                    WriteProcessMemory(hProcess, commandaddress, commandbytes, commandbytes.Length, out bytesWritten2);

                    Array.Copy(BitConverter.GetBytes(commandaddress.ToInt64()), 0, cbuf_addtext_wrapper, 9, 4);
                    Array.Copy(callbytes, 0, cbuf_addtext_wrapper, 16, 4);

                    WriteProcessMemory(hProcess, cbuf_addtext_alloc, cbuf_addtext_wrapper, cbuf_addtext_wrapper.Length, out bytesWritten);

                    IntPtr bytesOut;
                    CreateRemoteThread(hProcess, IntPtr.Zero, 0, cbuf_addtext_alloc, IntPtr.Zero, 0, out bytesOut);

                    if(cbuf_addtext_alloc != IntPtr.Zero && commandaddress != IntPtr.Zero)
                    {
                        VirtualFreeEx(hProcess, cbuf_addtext_alloc, cbuf_addtext_wrapper.Length, FreeType.Release);
                        VirtualFreeEx(hProcess, commandaddress, cbuf_addtext_wrapper.Length, FreeType.Release);
                    }
                }
                cbuf_addtext_alloc = IntPtr.Zero;
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
        }
    }

im really stuff so i would appreciate if someone can help me with this issue its probably something small anyways please reply sooon!

thank you.

Like this

private void button1_Click(object sender, EventArgs e)
{
   if(!String.IsNullOrEmpty(richTextBox1.Text))
   {
      Send(richTextBox1.Text);
   }
   else if(!String.IsNullOrEmpty(textBox1.Text))     
   {    
      Send(textBox1.Text);
   } 
   else
   {
       return;  
   }    
}

You have to make sure you wire up the Click event in order for your code to fire. This gets set for you automatically in the designer code (auto-generated) when you double click the button. But you can also auto generate the method and subscription when looking at the list of events.

Copying an event function or coding it by hand will not automatically do it. However, with the code already in place, you should be able to now select it from the Click event drop down when in the designer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM