Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 两个带有共享滚动条的Gtk TextView小部件_C#_Gtk_Gtk# - Fatal编程技术网

C# 两个带有共享滚动条的Gtk TextView小部件

C# 两个带有共享滚动条的Gtk TextView小部件,c#,gtk,gtk#,C#,Gtk,Gtk#,我想有两个并排的TextView小部件,它们通过一个滚动条一起滚动 我可以将这两个TextView小部件放在Hbox中,然后将它们添加到视口,然后添加到滚动窗口。然而,这将不会工作我想要的。从滚动条滚动将起作用。但是在TextView中发生的操作不会改变滚动位置。(箭头键、向上翻页、向下翻页等),我也无法通过编程更改TextView.ScrollToMark和其他TextView滚动方法的滚动 如何让两个TextView小部件共享一个滚动条并在TextView的更新滚动中执行操作?关键是使用滚动

我想有两个并排的TextView小部件,它们通过一个滚动条一起滚动

我可以将这两个TextView小部件放在Hbox中,然后将它们添加到视口,然后添加到滚动窗口。然而,这将不会工作我想要的。从滚动条滚动将起作用。但是在TextView中发生的操作不会改变滚动位置。(箭头键、向上翻页、向下翻页等),我也无法通过编程更改TextView.ScrollToMark和其他TextView滚动方法的滚动


如何让两个TextView小部件共享一个滚动条并在TextView的更新滚动中执行操作?

关键是使用滚动窗口而不是视口,并在滚动窗口之间共享调整。视口将只在一个太大而无法放入容器的小部件上滚动。另一方面,ScrolledWindow将其调整链接到TextView调整,这些调整受光标移动的影响

我通过链接滚动窗口之间的调整,隐藏它们的滚动条,然后将它们放在一个只控制所有子窗口滚动条(未显示)的大滚动窗口中,实现了这一点

下面是Python的完整代码解决方案。你应该能够很容易地将其适应C

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

if __name__ == "__main__":
    window = gtk.Window()

    box = gtk.HBox()

    textview1 = gtk.TextView()
    textview2 = gtk.TextView()

    hadjustment = None
    vadjustment = None

    for textview in (textview1, textview2):
        sw = gtk.ScrolledWindow()
        # don't show the scrollbars on these sub-scrolledwindows
        sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
        sw.add(textview)

        # use the first scrolledwindow's adjustments
        if hadjustment is None:
            hadjustment = sw.get_hadjustment()
        else:
            sw.set_hadjustment(hadjustment)
        if vadjustment is None:
            vadjustment = sw.get_vadjustment()
        else:
            sw.set_vadjustment(vadjustment)

        box.pack_start(sw, padding=5)

        buffer = textview.get_buffer()
        buffer.set_text("If a widget has native scrolling abilities,\n"
                        " it can be added to the gtk.ScrolledWindow\n"
                        "with the gtk.Container.add() method. If a\n"
                        "widget does not, you must first add the\n"
                        "widget to a gtk.Viewport, then add the\n"
                        "gtk.Viewport to the scrolled window. The\n"
                        "convenience method add_with_viewport() does\n"
                        "exactly this, so you can ignore the presence\n"
                        "of the viewport.")

    # this scrolled window will encompass the rest and have scrollbars
    main_sw = gtk.ScrolledWindow(hadjustment, vadjustment)
    main_sw.set_policy(gtk.POLICY_ALWAYS, gtk.POLICY_ALWAYS)
    main_sw.add(box)
    window.add(main_sw)

    window.show_all()
    gtk.main()