Hi everybody
i am trying to implement the 'create new button' function for my desktop replacement but i have a problem with mouse coordinates..
this is the idea:

I have FORM1 which is the main form, where i right click in a point of my choice and capture the X,Y coordinates of the mouse via the mouse_move event in 2 environment variables: my.settings.buttX and my.settings.buttY
these are where, at the end, my new button shall appear.

When i right click on the form1, i get a context menu which asks me 'button or picturebox style button'. I click 'button' and FORM2 appears.
Form2 has 2 textboxes, it is where i give a Title or caption for the new button, and where i chose the program to associate it with via an OFD.
It saves the name and process into a configuration file using a ^ to separate them.
I also add the click event to the button programmatically, and it has this function:
- opens the configuration text
- defines a dictionary where key is the button name and value is the button process
- depending on the key launches the proper value.

When the FORM2 closes, the button should appear at the coorinates i saved, that are relative to the FORM1
But it does not..
I figured it out that since i define the new button programmatically from inside the FORM2, the .TOP and .LEFT properties of the new button are calculated on the FORM2 and not FORM1.
I cannot make it appear in the proper coordinates...

this is the code for FORM1:

Public Class Form1

    Private Sub ButtonToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ButtonToolStripMenuItem.Click
        Form2.Show()
    End Sub


    Private Sub Form1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Label1.Text = e.X
        Label2.Text = e.Y
        Me.Refresh()
    End Sub

    Private Sub Form1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
        My.Settings.buttx = e.X
        My.Settings.butty = e.Y
        My.Settings.Save()
        My.Settings.Reload()
    End Sub
End Class

and this is for FORM2

Imports System
Imports System.IO
Imports System.Text

Public Class Form2
    Dim posX As Integer = My.Settings.buttx
    Dim posY As Integer = My.Settings.butty
    Dim buttText As String
    Dim buttProcess As String
    Dim dict As New Dictionary(Of String, String)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog With {.Filter = "Supported Files|*.exe"}
        On Error Resume Next
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            TextBox2.Text = ofd.FileName
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        My.Settings.butttext = TextBox1.Text
        My.Settings.buttprocess = TextBox2.Text
        My.Settings.Save()
        My.Settings.Reload()

        Dim path As String = My.Application.Info.DirectoryPath & "\custom\custombuttons.txt"
        Dim sw As StreamWriter

        If File.Exists(path) = False Then
            sw = File.CreateText(path)
            sw.WriteLine(TextBox1.Text & "^" & TextBox2.Text)
            sw.Flush()
            sw.Close()
        Else
            sw = File.AppendText(path)
            sw.WriteLine(TextBox1.Text & "^" & TextBox2.Text)
            sw.Flush()
            sw.Close()
        End If

        Dim NewButton As New Button
        With NewButton
            .Width = 80
            .Height = 30
            .Text = My.Settings.butttext
            .Top =  posY
            .Left =  posX
        End With
        AddHandler NewButton.Click, AddressOf newbutton_click
        Me.Close()

        Form1.Controls.Add(NewButton)
    End Sub

    Friend Sub newbutton_click(sender As Object, e As System.Windows.Forms.MouseEventArgs)
        ReadConf(My.Application.Info.DirectoryPath & "\custom\custombuttons.txt")
        Process.Start(My.Settings.buttprocess)
    End Sub

    Private Sub ReadConf(FullPathFileName As String)
        Dim lines As String() = IO.File.ReadAllLines(FullPathFileName)
        For Each line As String In lines
            Dim kv As KeyValuePair(Of String, String) = ToKeyValuePair(line)
            dict.Add(kv.Key, kv.Value)
            'ListBox1.Items.Add(kv.Key)
            If kv.Key = My.Settings.butttext Then
                My.Settings.buttprocess = kv.Value
            End If
        Next

    End Sub
    Public Function ToKeyValuePair(pair As String) As KeyValuePair(Of String, String)
        Dim two As String() = pair.Split("^")
        Return New KeyValuePair(Of String, String)(two(0), two(1))
    End Function

End Class

Can anyone help telling me how i should modify this?
Thank you,
Alex

O.K there are2ways of doing this. 1) create form 2 with exactly the same dimensions as form1 e.g. form2.width = form1.width etc. and in the same position, get thcoordianates of the mouse click and tranfer these to form1. 2) Use percentages to work out the distance from the top of form2 and the distance from the left side of form2 and transfer these to form1. In this case form1 can have different dimensions to form2 because you use percentages.

Why not create the button in the main form and just use the second form to get the parameters?

Private Sub Form1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

    If e.Button = Windows.Forms.MouseButtons.Right Then
        If Form2.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim btn As New Button
            btn.Text = Form2.txtLabel.Text
            btn.Location = e.Location
            AddHandler ...
            .
            .
            .
            Me.Controls.Add(btn)
        End If
    End If

End Sub

Gosh stupid me, i didn't think of that... i am trying it today...

I have tryed both solutions, the first won't be viable because the main form is maximized, the second solution eliminates the error in locating the button but seems to clear the processes so all buttons start the last application i defined as a process :(
I can't figure it out

seems to clear the processes so all buttons start the last application i defined as a process

Can you please clarify with an example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.