如何在C#Winform项目中调整大小并移动动态创建的文本框 [英] How to resize and move dynamically created textbox in C# winform project

查看:105
本文介绍了如何在C#Winform项目中调整大小并移动动态创建的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

论坛,

我已经使用Visual Studio 2008 C#开发了一个应用程序,其中在运行时创建了文本框.我有几个正在运行的媒体播放器应用程序,并且创建的文本框数量与正在运行的媒体播放器应用程序的数量相同.
到现在为止,我已经能够创建一个与特定媒体播放器应用程序相对应的文本框,并使用MP应用程序的标题将两者链接起来.以此为参考,我可以在移动表单上的文本框时更改MP应用程序的位置.但是我无法更改MP应用程序的大小,因为我更改了表单上文本框的大小.当我尝试通过拖动来增加文本框的大小时,每次单击鼠标都会使其不断减小.第二件事,我可以通过仅禁用某些代码行来一次更改位置或大小.我的要求是在运行时调整大小或重新放置.

我是C#语言的新手.如果有人可以指导我解决此问题,我将不胜感激.
在此先感谢

NamitaS !!!!!! :-O



我要发布一段代码,其中大小和位置会随着鼠标的移动而变化.

Hi Forum,

I have developed an application using Visual Studio 2008 C#, in which I create text-boxes in run-time. I have several media player applications running and I create same number of text-boxes as the number of media player applications running.
Till now I am able to create a text-box corresponding to a particular Media player application and link the two using the title of the MP application. Taking it as a reference I am able to change the location of the MP application as I move the text-box on the form. But I am not able to change the size of the MP application as I change the size of the text box on the form. When I try to increase the size of the text box by dragging, it keep on decreasing with each mouse click. Second thing I can either change location or size at a time by disabling some lines of the code only. My requirement is to re-size or relocate in run-time.

I am new to C# language. I appreciate in advance if someone can guide me to solve this issue.
Thanking in advance

NamitaS!!!!!! :-O



I am posting a section of the code where size and location change with mouse movements.

 protected virtual void MouseMoveEventHandler(object sender
           , MouseEventArgs evnt_args_mouse)
 {
            
   if (
     !"Core.Windows.Forms.EditableControl"
     .Equals(sender.GetType().ToString()) &&
     _control_moving != null &&
      evnt_args_mouse.Button == MouseButtons.Left
       )
   {
      if (allowResize == true)
      {
       _control_moving.Height = _control_moving.Top + evnt_args_mouse.Y;
       _control_moving.Width = (_control_moving.Left - 170)+            evnt_args_mouse.X;   
        Size sz = new Size(_control_moving.Width * (1024 / 130), _control_moving.Height * (1280 / 170));

        g_Obj.Size = sz;
                                                              
      }
      else
      {
        Point pt = Cursor.Position;
        _control_moving.Left = (this.PointToClient(pt)).X - 395 - x_offset_on_client_control;
        _control_moving.Top = (this.PointToClient(pt)).Y - 25 - y_offset_on_client_control;

        // monement of the Media Player application
        // g_Obj is the handle to the corresponding Media Player App
         
           Point mypoint = new Point((_control_moving.Left * (1280 / 170)), (_control_moving.Top * (1024 / 130)));
           
         g_Obj.Location = mypoint;

      }                                
   }
}

推荐答案

NamitaS写道:
NamitaS wrote:

我是C#语言的新手.

I am new to C# language.



如果您想真正学习C#,那么这对您来说似乎太复杂了.




Then this seems like far too complex a task for you, if you want to really learn C#.


NamitaS写道:
NamitaS wrote:

!"Core.Windows.Forms.EditableControl" .Equals(sender.GetType().ToString())

!"Core.Windows.Forms.EditableControl" .Equals(sender.GetType().ToString())



这是一个不好的方法.比较类型,而不是字符串.

像在代码中一样设置控件的位置和大小是正确的方法.您停留在哪一点?为什么这不起作用?因为您只有一个控件?只需保留一个数组,然后从此处开始在鼠标按下事件中设置_control_moving.



This is a bad way to do it. Compare the types, not the strings.

Setting the control position and size like you''re doing in code, is the right way to go. Which bit are you stuck on ? Why does this not work ? because you have only one control ? Just keep an array and set _control_moving in your mouse down event from there.


谢谢Christian Graus!

克里斯汀·格劳斯(Christian Graus)写道:
您停留在哪一点?为什么这不起作用?因为您只有一个控件?



我使用了一系列控件.我在鼠标按下事件中选择要调整大小或重定位的控件.
我停留在下面提到的部分:

_control_moving.Height = _control_moving.Top + evnt_args_mouse.Y;
_control_moving.Width =(_control_moving.Left-170)+ evnt_args_mouse.X;
Size sz = new Size(_control_moving.Width *(1024/130),_control_moving.Height *(1280/170));
g_Obj.Size = sz;


如果我注释了最后两行,则可以正确调整文本框的大小.
更改表单上相应文本框的大小时,最后两行将更改MP应用程序的大小.

问题是,当我删除注释并运行应用程序时,当注释了最后两行时,无法像我一样顺利地调整文本框的大小.此外,每次尝试增加文本框的大小和MP应用程序的大小时,它们都在不断缩小.
Thank You Christian Graus !

Christian Graus wrote:
Which bit are you stuck on ? Why does this not work ? because you have only one control ?



I have used an array of controls. I select the control to be resized or relocate in the mouse down event.
I stuck on at the section mentioned below:

_control_moving.Height = _control_moving.Top + evnt_args_mouse.Y;
_control_moving.Width = (_control_moving.Left - 170)+ evnt_args_mouse.X;
Size sz = new Size(_control_moving.Width * (1024 / 130), _control_moving.Height * (1280 / 170));
g_Obj.Size = sz;


If I comment the last 2 lines, I can resize the textbox properly.
last 2 lines are to change the size of MP application when size of the corresponding text-box on the form is changed.

The problem is when I remove the comments and run the application, I can not resize the text-box as smoothly as I was able to do, when the last 2 lines were commented. In addition The text-box size and MP application size keep on shrinking with each attempt to increase its size.


这篇关于如何在C#Winform项目中调整大小并移动动态创建的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆